diff --git a/app/models/project.rb b/app/models/project.rb index c438be16d..5ebae22a2 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -382,6 +382,7 @@ def reload(*args) @override_members = nil @assignable_users = nil @last_activity_date = nil + self.class.clear_last_activities base_reload(*args) end @@ -1009,18 +1010,16 @@ def visible_custom_field_values(user = nil) end end - def last_activity_date - @last_activity_date || fetch_last_activity_date + def self.last_activities + @last_activities ||= Redmine::Activity::Fetcher.new(User.current).events(nil, nil, :last_by_project => true).to_h end - # Preloads last activity date for a collection of projects - def self.load_last_activity_date(projects, user=User.current) - if projects.any? - last_activities = Redmine::Activity::Fetcher.new(User.current).events(nil, nil, :last_by_project => true).to_h - projects.each do |project| - project.instance_variable_set(:@last_activity_date, last_activities[project.id]) - end - end + def self.clear_last_activities + @last_activities = nil + end + + def last_activity_date + self.class.last_activities[id] || fetch_last_activity_date end private diff --git a/app/models/project_query.rb b/app/models/project_query.rb index a1bb52a2c..4a054ed5a 100644 --- a/app/models/project_query.rb +++ b/app/models/project_query.rb @@ -165,12 +165,6 @@ def results_scope(options={}) if has_column?(:parent_id) scope = scope.preload(:parent) end - - projects = scope.to_a - if has_column?(:last_activity_date) - Project.load_last_activity_date(scope) - end - - projects + scope end end