Avoid lots of CustomField.find_by_id calls when displaying an issue history with custom fields (#15072).
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@12217 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
1 parent
60d6e16978
commit
7bea176cdb
5 files changed
+56
-11
No files matched your search
+17
-3
@@ -58,9 +58,7 @@ class Journal < ActiveRecord::Base
|
||||
def visible_details(user=User.current)
|
||||
details.select do |detail|
|
||||
if detail.property == 'cf'
|
||||
field_id = detail.prop_key
|
||||
field = CustomField.find_by_id(field_id)
|
||||
field && field.visible_by?(project, user)
|
||||
detail.custom_field && detail.custom_field.visible_by?(project, user)
|
||||
elsif detail.property == 'relation'
|
||||
Issue.find_by_id(detail.value || detail.old_value).try(:visible?, user)
|
||||
else
|
||||
@@ -146,6 +144,22 @@ class Journal < ActiveRecord::Base
|
||||
notified_watchers.map(&:mail)
|
||||
end
|
||||
|
||||
# Sets @custom_field instance variable on journals details using a single query
|
||||
def self.preload_journals_details_custom_fields(journals)
|
||||
field_ids = journals.map(&:details).flatten.select {|d| d.property == 'cf'}.map(&:prop_key).uniq
|
||||
if field_ids.any?
|
||||
fields_by_id = CustomField.find_all_by_id(field_ids).inject({}) {|h, f| h[f.id] = f; h}
|
||||
journals.each do |journal|
|
||||
journal.details.each do |detail|
|
||||
if detail.property == 'cf'
|
||||
detail.instance_variable_set "@custom_field", fields_by_id[detail.prop_key.to_i]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
journals
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def split_private_notes
|
||||
|
||||
@@ -19,6 +19,12 @@ class JournalDetail < ActiveRecord::Base
|
||||
belongs_to :journal
|
||||
before_save :normalize_values
|
||||
|
||||
def custom_field
|
||||
if property == 'cf'
|
||||
@custom_field ||= CustomField.find_by_id(prop_key)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def normalize_values
|
||||
|
||||
Reference in new issue
Block a user