Skip to content

Commit

Permalink
reduce the number of ActiveRecord objects generated when loading the …
Browse files Browse the repository at this point in the history
…Asset editing form
  • Loading branch information
mlc committed Oct 24, 2009
1 parent 6884e0c commit fbf1fac
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 8 deletions.
1 change: 1 addition & 0 deletions app/models/contributor_role.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class ContributorRole < ActiveRecord::Base
include Picklist
has_many :contributors
quick_column :name

def safe_to_delete?
contributors.size == 0
Expand Down
1 change: 1 addition & 0 deletions app/models/creator_role.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class CreatorRole < ActiveRecord::Base
include Picklist
has_many :creators
quick_column :name

def safe_to_delete?
creators.size == 0
Expand Down
1 change: 1 addition & 0 deletions app/models/publisher_role.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class PublisherRole < ActiveRecord::Base
include Picklist
has_many :publishers
quick_column :name

def safe_to_delete?
publishers.size == 0
Expand Down
3 changes: 3 additions & 0 deletions app/models/subject.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
class Subject < ActiveRecord::Base
include PbcoreXmlElement
include Picklist

has_and_belongs_to_many :assets
quick_column 'CONCAT(subject, " (", subject_authority, ")")'
xml_string "subject", :subject
xml_string "subjectAuthorityUsed", :subject_authority

Expand Down
4 changes: 2 additions & 2 deletions app/views/assets/_contributor.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<div class="contributor">
<p>
<%= f.label(:contributor_role_id, "Contributor Role: ") %>
<%= f.select :contributor_role_id, ContributorRole.all(:conditions => ["visible = 1 OR id = ?", contributor.contributor_role_id]).map{|i| [i.name, i.id]} %>
<%= f.select :contributor_role_id, ContributorRole.quick_load_for_select(["visible = 1 OR id = ?", contributor.contributor_role_id]) %>
&nbsp;&nbsp;
<%= f.label(:contributor, "Contributor:") %> <%= f.text_field :contributor %>
<%= f.remove_link "remove" %>
</p>
</div>
</div>
4 changes: 2 additions & 2 deletions app/views/assets/_creator.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<div class="creator">
<p>
<%= f.label(:creator_role_id, "Creator Role: ") %>
<%= f.select :creator_role_id, CreatorRole.all(:conditions => ["visible = 1 OR id = ?", creator.creator_role_id]).map{|i| [i.name, i.id]} %>
<%= f.select :creator_role_id, CreatorRole.quick_load_for_select(["visible = 1 OR id = ?", creator.creator_role_id]) %>
&nbsp;&nbsp;
<%= f.label(:creator, "Creator:") %> <%= f.text_field :creator %>
<%= f.remove_link "remove" %>
</p>
</div>
</div>
2 changes: 1 addition & 1 deletion app/views/assets/_publisher.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="publisher">
<p>
<%= f.label(:publisher_role_id, "Publisher Role: ") %>
<%= f.select :publisher_role_id, PublisherRole.all(:conditions => ["visible = 1 OR id = ?", publisher.publisher_role_id]).map{|i| [i.name, i.id]} %>
<%= f.select :publisher_role_id, PublisherRole.quick_load_for_select(["visible = 1 OR id = ?", publisher.publisher_role_id]) %>
&nbsp;&nbsp;
<%= f.label(:publisher, "Publisher:") %> <%= f.text_field :publisher %>
<%= f.remove_link "remove" %>
Expand Down
6 changes: 3 additions & 3 deletions app/views/assets/_subject.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<div class="subject">
<p>
<select name="asset[subject_ids][]">
<%= options_for_select(Subject.all(:conditions => (subject.nil? ?
<%= options_for_select(Subject.quick_load_for_select(subject.nil? ?
"visible = 1" : ["visible = 1 OR id = ?", subject.id]),
:order => "subject ASC").map{|s| ["#{s.subject} (#{s.subject_authority})", s.id]}, subject.id) %>
subject.id) %>
</select>
<%= f.remove_link "remove" %>
</p>
</div>
</div>
30 changes: 30 additions & 0 deletions lib/picklist.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
# A lot of PBCore fields are extensible picklists.
module Picklist

module ClassMethods
# avoid instantiating lots and lots of ActiveRecord objects when
# all we need is a select box
def quick_load_for_select(conditions = nil)
sql = "SELECT #{get_quick_column} AS quick, id FROM #{connection.quote_table_name(table_name)}"
sanitized_conditions = sanitize_sql_for_conditions(conditions)
sql << " WHERE #{sanitized_conditions}" if sanitized_conditions
sql << " ORDER BY quick ASC"
result = connection.execute(sql)
rows = []
result.each{|row| rows << row}
result.free

rows
end

# set the column or other SQL expression which will be the title
# in quickly-loaded sqlect boxes
def quick_column(column)
write_inheritable_attribute("quick_column", column)
end

def get_quick_column #:nodoc:
read_inheritable_attribute("quick_column")
end
end

def self.included(base)
base.extend(ClassMethods)
end
end

0 comments on commit fbf1fac

Please sign in to comment.