| Class | ContentItem |
| In: |
app/models/content_item.rb
|
| Parent: | ActiveRecord::Base |
API for interaction with the content data model and the corresponding content database tables, for example the table ‘content_type_1’.
Determine if a content item is downloadable.
# File app/models/content_item.rb, line 14
14: def self.available_for_download (
15: item
16: )
17: # There is no need to test if item[:file_name] is readable here, because
18: # the file_name attribute of the item object will not be populated (when
19: # the item object is created) if the file is not readable.
20: if (item && item[:file_name] && item.attributes['allow_download'] &&
21: 'TRUE' == item.allow_download)
22: return TRUE
23: else
24: return FALSE
25: end
26: end
Retrieve a single content item.
# File app/models/content_item.rb, line 32
32: def self.get (
33: project_id,
34: project_host_id,
35: content_type_id,
36: id
37: )
38: set_table(content_type_id)
39:
40:
41: # Proceed only if the requested item exists.
42: if (item = find_by_id_and_active(id, 'TRUE'))
43: # Process all callbacks for this content type, converting callback fields
44: # and their results into attributes and values for this content item.
45: for callback in ContentCallback.get(project_id,
46: project_host_id,
47: content_type_id,
48: item)
49: # If this content table contains a column with the same field name
50: # as this callback and the value of this field is non-empty then use
51: # it, else use the callback value if it is non-empty.
52: field = callback[0]
53: value = callback[1]
54: item["#{field}""#{field}"] = value if (!e(item["#{field}""#{field}"]) && e(value))
55: end
56:
57:
58: # Construct human readable versions for all date fields.
59: for d in ['date', 'end_date', 'created_on', 'updated_on']
60: item["#{d}_readable""#{d}_readable"] = item["#{d}""#{d}"].to_readable_date if item["#{d}""#{d}"]
61: end
62:
63:
64: # Flag this item if it should be available for download requests.
65: if (available_for_download(item))
66: item[:available_for_download] = TRUE
67: else
68: item[:available_for_download] = nil
69: end
70:
71:
72: # Determine if this item has any child groups mapped to it, if so then
73: # construct a template reference for each child group associated with this
74: # item. Note that content type database tables are prohibited from
75: # using the reserved word 'groups' as a column name.
76: groups = ''
77: for group in ContentGroupToItemMap.find_all_by_project_id_and_parent_content_type_id_and_parent_content_item_id(project_id, content_type_id, id)
78: groups += reference_assemble_tag(CONTENT_GROUP,
79: group.content_groups[0].content_type_id,
80: group.content_groups[0].id)
81: end
82:
83:
84: # Populate the child groups attribute for this item, iff it is not empty.
85: (groups.size > 0) ? (item[:groups] = groups) : (item[:groups] = nil)
86:
87:
88: return item
89: else
90: return FALSE
91: end
92: end
Retrieve the item immediately before or after a specific content item, searching using any field, sorting by date or precedence.
# File app/models/content_item.rb, line 99
99: def self.get_adjacent_id (
100: content_type_id,
101: start_value,
102: equality,
103: order_by
104: )
105: # Determine if the 'adjacent item' is before or after the current item.
106: if ('next' == equality)
107: equality = '>'
108: direction = 'ASC'
109: elsif ('previous' == equality)
110: equality = '<'
111: direction = 'DESC'
112: end
113:
114:
115: # Encase all non-Fixnum values in quotes.
116: if (!FIXNUM_CONTENT_FIELDS.has(order_by))
117: if DATETIME_CONTENT_FIELDS.has(order_by)
118: start_value = start_value.to_mysql_datetime
119: end
120:
121: start_value = '"' + start_value + '"'
122: end
123:
124:
125: # Retrieve the adjacent item's row id.
126: content_table = set_table(content_type_id.to_s)
127: adjacent_item = find_by_sql("SELECT id
128: FROM #{content_table}
129: WHERE active = 'TRUE'
130: AND #{order_by} #{equality} #{start_value}
131: ORDER BY #{order_by} #{direction}
132: LIMIT 1")
133:
134: # If no adjacent item is found then try to find one by wrapping to the
135: # other end of the data set.
136: if (!adjacent_item[0])
137: adjacent_item = find_by_sql("SELECT id
138: FROM #{content_table}
139: WHERE active = 'TRUE'
140: ORDER BY #{order_by} #{direction}
141: LIMIT 1")
142: end
143:
144: (adjacent_item[0]) ? (adjacent_item[0].id) : (FALSE)
145: end
If a content item id is dynamic then obtain it‘s database row id.
# File app/models/content_item.rb, line 151
151: def self.get_dynamic_id (
152: id
153: )
154: case id
155: when 'oldest'
156: return ContentItem.get_oldest_id
157: when 'newest'
158: return ContentItem.get_newest_id
159: when 'random'
160: return ContentItem.get_random_id
161: else
162: return id
163: end
164: end
Retrieve the database row id of the newest active item for the current content type.
# File app/models/content_item.rb, line 193
193: def self.get_newest_id
194: find(:first, :conditions => ["active = 'TRUE'"], :order => 'date DESC').id
195: end
Retrieve the database row id of the oldest active item for the current content type.
# File app/models/content_item.rb, line 202
202: def self.get_oldest_id
203: find(:first, :conditions => ["active = 'TRUE'"], :order => 'date ASC').id
204: end
Retrieve the database row id of a random active item for the current content type.
# File app/models/content_item.rb, line 211
211: def self.get_random_id
212: # Obtain the number of active items for this content type.
213: #
214: # Use cache data if it exists.
215: if ($cache['row_counts'].has_key?(table_name))
216: row_count = $cache['row_counts'][table_name]
217: # Otherwise obtain the row count and cache it.
218: else
219: row_count = count_by_sql("SELECT count(*)
220: FROM " + table_name + "
221: WHERE active = 'TRUE'")
222: $cache['row_counts'][table_name] = row_count
223: end
224:
225: # There are no active items.
226: if (row_count < 1)
227: return FALSE
228: # There is one active item.
229: elsif (1 == row_count)
230: return find(:first).id
231: # There are multiple active items.
232: else
233: rows = find_by_sql("SELECT id
234: FROM " + table_name + "
235: WHERE active = 'TRUE'")
236:
237: # Construct an array of all active content item ids.
238: all_ids = []
239: rows.each { |row| all_ids << row.id }
240:
241: # Sort the array randomly and return the id of the first element.
242: return all_ids.sort_by{ rand }[0]
243: end
244: end
Construct a table name, based on a content type id.
# File app/models/content_item.rb, line 250
250: def self.get_table_name (
251: content_type_id
252: )
253: "content_type_" << content_type_id.to_s
254: end
Determine the maximum value of the ‘updated_on’ field, comparing the values for all content item tables for this project host.
# File app/models/content_item.rb, line 171
171: def self.last_modified (
172: content_type_ids
173: )
174: max = Time.at(0)
175:
176: for content_type_id in content_type_ids
177: set_table(content_type_id)
178: item = ContentItem.find_by_sql("SELECT updated_on
179: FROM #{table_name}
180: ORDER BY updated_on
181: DESC LIMIT 1")[0]
182: max = item.updated_on if (item && item.updated_on && item.updated_on>max)
183: end
184:
185: max
186: end