Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 47 lines (38 sloc) 1.515 kb
43dd34c @mlc preliminary legwork for picklists
mlc authored
1 # A lot of PBCore fields are extensible picklists.
2 module Picklist
73e232c @mlc get rid of custom code which does exactly what rails already does
mlc authored
3
fbf1fac @mlc reduce the number of ActiveRecord objects generated when loading the Ass...
mlc authored
4 module ClassMethods
5 # avoid instantiating lots and lots of ActiveRecord objects when
6 # all we need is a select box
6c11372 @mlc sane UI if you have many, mayn subject headings
mlc authored
7 def quick_load_for_select(conditions = nil, limit = nil)
7ac6c49 @mlc more optimization and bugfixes
mlc authored
8 columns = (["#{get_quick_column} AS quick"] + (extra_quick_columns.to_a || []) + ["id"]).join(", ")
9 sql = "SELECT #{columns} FROM #{connection.quote_table_name(table_name)}"
fbf1fac @mlc reduce the number of ActiveRecord objects generated when loading the Ass...
mlc authored
10 sanitized_conditions = sanitize_sql_for_conditions(conditions)
11 sql << " WHERE #{sanitized_conditions}" if sanitized_conditions
12 sql << " ORDER BY quick ASC"
6c11372 @mlc sane UI if you have many, mayn subject headings
mlc authored
13 sql << " LIMIT #{limit.to_i}" if limit
7ac6c49 @mlc more optimization and bugfixes
mlc authored
14
fbf1fac @mlc reduce the number of ActiveRecord objects generated when loading the Ass...
mlc authored
15 result = connection.execute(sql)
16 rows = []
7ac6c49 @mlc more optimization and bugfixes
mlc authored
17 result.each{|row| row[-1] = row[-1].to_i; rows << row}
fbf1fac @mlc reduce the number of ActiveRecord objects generated when loading the Ass...
mlc authored
18 result.free
19
20 rows
21 end
22
23 # set the column or other SQL expression which will be the title
24 # in quickly-loaded sqlect boxes
25 def quick_column(column)
26 write_inheritable_attribute("quick_column", column)
27 end
28
7ac6c49 @mlc more optimization and bugfixes
mlc authored
29 def get_quick_column # :nodoc:
fbf1fac @mlc reduce the number of ActiveRecord objects generated when loading the Ass...
mlc authored
30 read_inheritable_attribute("quick_column")
31 end
7ac6c49 @mlc more optimization and bugfixes
mlc authored
32
33 # specify one or more extra columns to be returned by quick_load
34 def extra_quick_column(*columns)
35 write_inheritable_attribute("extra_quick_column", Set.new(columns.map(&:to_s)) + (extra_quick_columns || []))
36 end
37
38 def extra_quick_columns # :nodoc:
39 read_inheritable_attribute("extra_quick_column")
40 end
fbf1fac @mlc reduce the number of ActiveRecord objects generated when loading the Ass...
mlc authored
41 end
42
43 def self.included(base)
44 base.extend(ClassMethods)
45 end
43dd34c @mlc preliminary legwork for picklists
mlc authored
46 end
Something went wrong with that request. Please try again.