| Class | ContentGroup |
| In: |
app/models/content_group.rb
|
| Parent: | ActiveRecord::Base |
API for interaction with the database table ‘content_groups’, and it‘s related data model.
| item_ids | [RW] | Accessor methods. |
Retrieve a group from the database.
# File app/models/content_group.rb, line 18
18: def self.get (
19: project_id,
20: id
21: )
22: find_by_id_and_project_id_and_active(id, project_id, 'TRUE')
23: end
Retrieve all available group ids for a project host.
# File app/models/content_group.rb, line 29
29: def self.get_all_ids (
30: content_type_ids
31: )
32: ids = []
33:
34: if (content_type_ids.size > 0)
35: groups = find_by_sql("SELECT id
36: FROM #{table_name}
37: WHERE content_type_id in (#{content_type_ids.join(',')})")
38: groups.each { |group| ids << group.id }
39: end
40:
41: ids
42: end
Retrieve a list of all content item ids within a content group.
Note that the list of content items belonging to a group is obtained at runtime for all span types, this is because item mapping information and content item information may be updated at any time.
# File app/models/content_group.rb, line 52
52: def self.get_item_ids (
53: project_id,
54: group_id,
55: content_type_id
56: )
57: # An array listing content item ids for this group.
58: items_in_group = []
59: return items_in_group if (!group = find_by_id_and_active(group_id, 'TRUE'))
60:
61:
62: # Set the number of elements that will be retrieved by the SQL
63: # content retrieval query.
64: (group.magnitude.to_i>0) ? (select_limit=group.magnitude) : (select_limit=1)
65:
66:
67: # Define the table containing content items, and ensure that it contains
68: # available items.
69: content_table = ContentItem.set_table(content_type_id.to_s)
70:
71: # Obtain the number of active items for this content type.
72: #
73: # Use cache data if it exists.
74: if ($cache['row_counts'].has_key?(content_table))
75: row_count = $cache['row_counts'][content_table]
76: # Otherwise obtain the row count and cache it.
77: else
78: row_count = count_by_sql("SELECT count(*)
79: FROM #{content_table}
80: WHERE active = 'TRUE'")
81: $cache['row_counts'][content_table] = row_count
82: end
83:
84:
85: return items_in_group if (0 == row_count)
86:
87:
88: # Construct an SQL condition based on the minimum and maximum id values
89: # for this group. If either is set then the SQL query used to retrieve
90: # content items will be bounded at the bottom by min_id and at the top by
91: # max_id.
92: if (group.min_id && group.max_id)
93: minmax_condition = " AND id >= #{group.min_id} AND id <= #{group.max_id} "
94: elsif (group.min_id)
95: minmax_condition = " AND id >= #{group.min_id} "
96: elsif (group.max_id)
97: minmax_condition = " AND id <= #{group.max_id}"
98: else
99: minmax_condition = ''
100: end
101:
102:
103: # This group is a set of arbitrarily selected items.
104: if ('random' == group.span)
105: # If the number of rows available is less the number of rows required
106: # then unflag all rows in this table, in order that they all be included
107: # in this random selection.
108: row_count = 0
109: if (row_count <= select_limit)
110: #ContentItem.update_all("random = 'FALSE' WHERE active = 'TRUE'")
111: rows = find_by_sql("SELECT id
112: FROM #{content_table}
113: WHERE active = 'TRUE'
114: #{minmax_condition}
115: AND random = 'FALSE'")
116: row_count = rows.size
117: end
118:
119: # Obtain a list of all active content item ids that have not already
120: # been randomly displayed.
121: all_ids = []
122: rows.each { |row| all_ids << row.id }
123:
124: # If there are less rows than requested then return all data.
125: if (select_limit >= all_ids.size)
126: items_in_group = all_ids
127: # Obtain a random list of ids from the list of all ids.
128: else
129: all_ids = all_ids.sort_by { rand }
130: (0..select_limit-1).each { |index| items_in_group << all_ids[index] }
131: end
132:
133: # Otherwise the group is a defined set of items.
134: else
135: # Apply sorting instructions.
136: if (group.sort_order)
137: sort_order = group.sort_order
138: else
139: sort_order = 'ASC'
140: end
141:
142: if (group.sort_field)
143: sort_field = group.sort_field
144: else
145: sort_field = 'updated_on'
146: end
147:
148: # Obtain a list of all content items ids within this group.
149: case group.span
150: # A specific list of content ids.
151: when 'list'
152: # Retrieve the ids of all items that are mapped to this group.
153: f = 'content_item_id'
154: g = ContentItemToGroupMap.find_all_by_project_id_and_content_group_id(
155: project_id,
156: group.id)
157:
158: # A list of content ids that share the same date.
159: when 'date'
160: g = find_by_sql("SELECT id
161: FROM #{content_table}
162: WHERE date LIKE '#{group.date.to_datestamp}%'
163: AND active = 'TRUE'
164: #{minmax_condition}
165: ORDER BY #{sort_field} #{sort_order}")
166:
167: # Every active item.
168: when 'all'
169: g = find_by_sql("SELECT id
170: FROM #{content_table}
171: WHERE active = 'TRUE'
172: #{minmax_condition}
173: ORDER BY #{sort_field} #{sort_order}")
174:
175: # The oldest active item.
176: when 'oldest'
177: g = find_by_sql("SELECT id
178: FROM #{content_table}
179: WHERE active = 'TRUE'
180: ORDER BY #{sort_field} #{sort_order}
181: LIMIT #{select_limit}")
182:
183: # The newest active item.
184: when "newest"
185: g = find_by_sql("SELECT id
186: FROM #{content_table}
187: WHERE active = 'TRUE'
188: ORDER BY #{sort_field} DESC
189: LIMIT #{select_limit}")
190: end
191:
192: # Enumerate all content item ids for this group.
193: f ||= 'id'
194: g.each { |row| items_in_group << row["#{f}""#{f}"] }
195: end
196:
197:
198: items_in_group
199: end
Determine the maximum value of the ‘updated_on’ field for this table, # comparing the values of all rows for this project host.
# File app/models/content_group.rb, line 205
205: def self.last_modified (
206: content_type_ids
207: )
208: max = Time.at(0)
209:
210: for content_type_id in content_type_ids
211: for group in find_all_by_content_type_id_and_active(content_type_id, TRUE)
212: max = group.updated_on if (group.updated_on > max)
213: end
214: end
215:
216: max
217: end