]> source.dussan.org Git - redmine.git/commitdiff
Evaluate acts_as_activity_provider's scope lazily (#33664).
authorGo MAEDA <maeda@farend.jp>
Sat, 17 Oct 2020 02:02:50 +0000 (02:02 +0000)
committerGo MAEDA <maeda@farend.jp>
Sat, 17 Oct 2020 02:02:50 +0000 (02:02 +0000)
Patch by Pavel Rosický.

git-svn-id: http://svn.redmine.org/redmine/trunk@20148 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/attachment.rb
app/models/changeset.rb
app/models/document.rb
app/models/issue.rb
app/models/journal.rb
app/models/message.rb
app/models/news.rb
app/models/time_entry.rb
app/models/wiki_content_version.rb
extra/sample_plugin/app/models/meeting.rb
lib/plugins/acts_as_activity_provider/lib/acts_as_activity_provider.rb

index 112232c6402076c17e227574947640e6cae050a8..d6c272453c8d4ad94aaa451b0aa30185b917f912 100644 (file)
@@ -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")
index d35d387ef123a724a3f803a8728a9c356a3b71a8..5a0161019677be63c1257eabfef6505bfddf84fa 100644 (file)
@@ -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
index 98a5579716561d9d0e4c6a2106530fbecd3283d4..19f09efd80d197b93a2d4bcd19ff882b76e5451d 100644 (file)
@@ -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
index 8c3146137f0be3e661ce610bcf9e128573ebf52a..3ccace46e9c1366da4d8d23bf8dc31a560c803d4 100644 (file)
@@ -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)
index 64b16108e8b667f10f7c3a81cffd9e9d46266b31..2843e8faff8a3284e987dbfcfd7a81c19083f3d5 100644 (file)
@@ -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
index 8bf35dc97385e7f25831b824df85a312915e7160..87481ac61c61e8db8e311ae37c5dbe503b299173 100644 (file)
@@ -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
 
index a96b60224c8b7fe84871b6e959cf63fbdeaceaae..75656f60a40efe7e6b0fd25131a7d17b154806b0 100644 (file)
@@ -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
 
index 130f398f49e8e627dfce1e61af75405fc6883f0d..b2d3f58e463c5a029d546d0aec133b0052dc3029 100644 (file)
@@ -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') }
index 0aa4a104929dff2f697145589b335b7c0412c78b..df00aa2d4f2c1f63f186547e54d332df9e91ba9e 100644 (file)
@@ -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
 
index ca8af1021240f3b45f763cf482d5bfe2ab6f9025..251892ece687879b995853f75dd26638d2e641ae 100644 (file)
@@ -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
index 21e1ee4734b107f71ef7f66dc116e0c812843361..5e98a0feee264ccb393750c0ae3224c8a0c6d399 100644 (file)
@@ -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)