From: Go MAEDA Date: Sat, 17 Oct 2020 02:02:50 +0000 (+0000) Subject: Evaluate acts_as_activity_provider's scope lazily (#33664). X-Git-Tag: 4.2.0~669 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=e48ff4882010df59f467433979fe27ebec39c0ee;p=redmine.git Evaluate acts_as_activity_provider's scope lazily (#33664). Patch by Pavel Rosický. git-svn-id: http://svn.redmine.org/redmine/trunk@20148 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 112232c64..d6c272453 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -38,16 +38,20 @@ class Attachment < ActiveRecord::Base acts_as_activity_provider :type => 'files', :permission => :view_files, :author_key => :author_id, - :scope => select("#{Attachment.table_name}.*"). + :scope => proc { + select("#{Attachment.table_name}.*"). joins("LEFT JOIN #{Version.table_name} ON #{Attachment.table_name}.container_type='Version' AND #{Version.table_name}.id = #{Attachment.table_name}.container_id " + "LEFT JOIN #{Project.table_name} ON #{Version.table_name}.project_id = #{Project.table_name}.id OR ( #{Attachment.table_name}.container_type='Project' AND #{Attachment.table_name}.container_id = #{Project.table_name}.id )") + } acts_as_activity_provider :type => 'documents', :permission => :view_documents, :author_key => :author_id, - :scope => select("#{Attachment.table_name}.*"). + :scope => proc { + select("#{Attachment.table_name}.*"). joins("LEFT JOIN #{Document.table_name} ON #{Attachment.table_name}.container_type='Document' AND #{Document.table_name}.id = #{Attachment.table_name}.container_id " + "LEFT JOIN #{Project.table_name} ON #{Document.table_name}.project_id = #{Project.table_name}.id") + } cattr_accessor :storage_path @@storage_path = Redmine::Configuration['attachments_storage_path'] || File.join(Rails.root, "files") diff --git a/app/models/changeset.rb b/app/models/changeset.rb index d35d387ef..5a0161019 100644 --- a/app/models/changeset.rb +++ b/app/models/changeset.rb @@ -43,7 +43,7 @@ class Changeset < ActiveRecord::Base acts_as_activity_provider :timestamp => "#{table_name}.committed_on", :author_key => :user_id, - :scope => preload(:user, {:repository => :project}) + :scope => proc { preload(:user, {:repository => :project}) } validates_presence_of :repository_id, :revision, :committed_on, :commit_date validates_uniqueness_of :revision, :scope => :repository_id diff --git a/app/models/document.rb b/app/models/document.rb index 98a557971..19f09efd8 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -29,7 +29,7 @@ class Document < ActiveRecord::Base acts_as_event :title => Proc.new {|o| "#{l(:label_document)}: #{o.title}"}, :author => Proc.new {|o| o.attachments.reorder("#{Attachment.table_name}.created_on ASC").first.try(:author) }, :url => Proc.new {|o| {:controller => 'documents', :action => 'show', :id => o.id}} - acts_as_activity_provider :scope => preload(:project) + acts_as_activity_provider :scope => proc { preload(:project) } validates_presence_of :project, :title, :category validates_length_of :title, :maximum => 255 diff --git a/app/models/issue.rb b/app/models/issue.rb index 8c3146137..3ccace46e 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -51,7 +51,7 @@ class Issue < ActiveRecord::Base :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}}, :type => Proc.new {|o| 'issue' + (o.closed? ? '-closed' : '')} - acts_as_activity_provider :scope => preload(:project, :author, :tracker, :status), + acts_as_activity_provider :scope => proc { preload(:project, :author, :tracker, :status) }, :author_key => :author_id DONE_RATIO_OPTIONS = %w(issue_field issue_status) diff --git a/app/models/journal.rb b/app/models/journal.rb index 64b16108e..2843e8faf 100644 --- a/app/models/journal.rb +++ b/app/models/journal.rb @@ -38,10 +38,12 @@ class Journal < ActiveRecord::Base acts_as_activity_provider :type => 'issues', :author_key => :user_id, - :scope => preload({:issue => :project}, :user). + :scope => proc { + preload({:issue => :project}, :user). joins("LEFT OUTER JOIN #{JournalDetail.table_name} ON #{JournalDetail.table_name}.journal_id = #{Journal.table_name}.id"). where("#{Journal.table_name}.journalized_type = 'Issue' AND" + " (#{JournalDetail.table_name}.prop_key = 'status_id' OR #{Journal.table_name}.notes <> '')").distinct + } before_create :split_private_notes after_create_commit :send_notification diff --git a/app/models/message.rb b/app/models/message.rb index 8bf35dc97..87481ac61 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -45,7 +45,7 @@ class Message < ActiveRecord::Base end) } - acts_as_activity_provider :scope => preload({:board => :project}, :author), + acts_as_activity_provider :scope => proc { preload({:board => :project}, :author) }, :author_key => :author_id acts_as_watchable diff --git a/app/models/news.rb b/app/models/news.rb index a96b60224..75656f60a 100644 --- a/app/models/news.rb +++ b/app/models/news.rb @@ -32,7 +32,7 @@ class News < ActiveRecord::Base acts_as_searchable :columns => ['title', 'summary', "#{table_name}.description"], :preload => :project acts_as_event :url => Proc.new {|o| {:controller => 'news', :action => 'show', :id => o.id}} - acts_as_activity_provider :scope => preload(:project, :author), + acts_as_activity_provider :scope => proc { preload(:project, :author) }, :author_key => :author_id acts_as_watchable diff --git a/app/models/time_entry.rb b/app/models/time_entry.rb index 130f398f4..b2d3f58e4 100644 --- a/app/models/time_entry.rb +++ b/app/models/time_entry.rb @@ -41,7 +41,7 @@ class TimeEntry < ActiveRecord::Base acts_as_activity_provider :timestamp => "#{table_name}.created_on", :author_key => :user_id, - :scope => joins(:project).preload(:project) + :scope => proc { joins(:project).preload(:project) } validates_presence_of :author_id, :user_id, :activity_id, :project_id, :hours, :spent_on validates_presence_of :issue_id, :if => lambda { Setting.timelog_required_fields.include?('issue_id') } diff --git a/app/models/wiki_content_version.rb b/app/models/wiki_content_version.rb index 0aa4a1049..df00aa2d4 100644 --- a/app/models/wiki_content_version.rb +++ b/app/models/wiki_content_version.rb @@ -34,13 +34,15 @@ class WikiContentVersion < ActiveRecord::Base :timestamp => "#{table_name}.updated_on", :author_key => "#{table_name}.author_id", :permission => :view_wiki_edits, - :scope => select("#{table_name}.updated_on, #{table_name}.comments, " + + :scope => proc { + select("#{table_name}.updated_on, #{table_name}.comments, " + "#{table_name}.version, #{WikiPage.table_name}.title, " + "#{table_name}.page_id, #{table_name}.author_id, " + "#{table_name}.id"). joins("LEFT JOIN #{WikiPage.table_name} ON #{WikiPage.table_name}.id = #{table_name}.page_id " + "LEFT JOIN #{Wiki.table_name} ON #{Wiki.table_name}.id = #{WikiPage.table_name}.wiki_id " + "LEFT JOIN #{Project.table_name} ON #{Project.table_name}.id = #{Wiki.table_name}.project_id") + } after_destroy :page_update_after_destroy diff --git a/extra/sample_plugin/app/models/meeting.rb b/extra/sample_plugin/app/models/meeting.rb index ca8af1021..251892ece 100644 --- a/extra/sample_plugin/app/models/meeting.rb +++ b/extra/sample_plugin/app/models/meeting.rb @@ -7,6 +7,6 @@ class Meeting < ActiveRecord::Base :url => Proc.new {|o| {:controller => 'meetings', :action => 'show', :id => o.id}} acts_as_activity_provider :timestamp => 'scheduled_on', - :scope => includes(:project), + :scope => proc { includes(:project) }, :permission => nil end diff --git a/lib/plugins/acts_as_activity_provider/lib/acts_as_activity_provider.rb b/lib/plugins/acts_as_activity_provider/lib/acts_as_activity_provider.rb index 21e1ee473..5e98a0fee 100644 --- a/lib/plugins/acts_as_activity_provider/lib/acts_as_activity_provider.rb +++ b/lib/plugins/acts_as_activity_provider/lib/acts_as_activity_provider.rb @@ -55,7 +55,14 @@ module Redmine provider_options = activity_provider_options[event_type] raise "#{self.name} can not provide #{event_type} events." if provider_options.nil? - scope = (provider_options[:scope] || self) + scope = provider_options[:scope] + if !scope + scope = self + elsif scope.respond_to?(:call) + scope = scope.call + else + ActiveSupport::Deprecation.warn "acts_as_activity_provider with implicit :scope option is deprecated. Please pass a scope on the #{self.name} as a proc." + end if from && to scope = scope.where("#{provider_options[:timestamp]} BETWEEN ? AND ?", from, to)