Project

General

Profile

Feature #23954 » 0001-Adds-last-activity-column-to-projects-list.patch

Marius BĂLTEANU, 2020-04-05 12:17

View differences:

app/controllers/projects_controller.rb
end
retrieve_project_query
scope = project_scope
respond_to do |format|
format.html {
# TODO: see what to do with the board view and pagination
if @query.display_type == 'board'
@entries = scope.to_a
@entries = project_scope.to_a
else
@entry_count = scope.count
@entry_count = @query.result_count
@entry_pages = Paginator.new @entry_count, per_page_option, params['page']
@entries = scope.offset(@entry_pages.offset).limit(@entry_pages.per_page).to_a
@entries = project_scope(:offset => @entry_pages.offset, :limit => @entry_pages.per_page).to_a
end
}
format.api {
@offset, @limit = api_offset_and_limit
@project_count = scope.count
@projects = scope.offset(@offset).limit(@limit).to_a
@project_count = @query.result_count
@projects = project_scope(:offset => @offset, :limit => @limit)
}
format.atom {
projects = scope.reorder(:created_on => :desc).limit(Setting.feeds_limit.to_i).to_a
projects = project_scope(:order => {:created_on => :desc}, :limit => Setting.feeds_limit.to_i).to_a
render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
}
format.csv {
# Export all entries
@entries = scope.to_a
@entries = project_scope.to_a
send_data(query_to_csv(@entries, @query, params), :type => 'text/csv; header=present', :filename => 'projects.csv')
}
end
app/models/project.rb
@due_date = nil
@override_members = nil
@assignable_users = nil
@last_activity_date = nil
base_reload(*args)
end
......
end
end
def last_activity_date
@last_activity_date || fetch_last_activity_date
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
end
private
def update_inherited_members
......
end
update_attribute :status, STATUS_ARCHIVED
end
def fetch_last_activity_date
latest_activities = Redmine::Activity::Fetcher.new(User.current, :project => self).events(nil, nil, :last_by_project => true)
latest_activities.empty? ? nil : latest_activities.to_h[self.id]
end
end
app/models/project_query.rb
QueryColumn.new(:identifier, :sortable => "#{Project.table_name}.identifier"),
QueryColumn.new(:parent_id, :sortable => "#{Project.table_name}.lft ASC", :default_order => 'desc', :caption => :field_parent),
QueryColumn.new(:is_public, :sortable => "#{Project.table_name}.is_public", :groupable => true),
QueryColumn.new(:created_on, :sortable => "#{Project.table_name}.created_on", :default_order => 'desc')
QueryColumn.new(:created_on, :sortable => "#{Project.table_name}.created_on", :default_order => 'desc'),
QueryColumn.new(:last_activity_date)
]
def initialize(attributes=nil, *args)
......
Project.visible.where(statement)
end
# Returns the project count
def result_count
base_scope.count
rescue ::ActiveRecord::StatementInvalid => e
raise StatementInvalid, e.message
end
def results_scope(options={})
order_option = [group_by_sort_order, (options[:order] || sort_clause)].flatten.reject(&:blank?)
......
scope = scope.preload(:parent)
end
scope
projects = scope.to_a
if has_column?(:last_activity_date)
Project.load_last_activity_date(scope)
end
projects
end
end
config/locales/en.yml
field_downloads: Downloads
field_author: Author
field_created_on: Created
field_last_activity_date: Last activity
field_updated_on: Updated
field_closed_on: Closed
field_field_format: Format
config/locales/pt-BR.yml
field_downloads: Downloads
field_author: Autor
field_created_on: Criado em
field_last_activity_date: Última atividade
field_updated_on: Alterado em
field_field_format: Formato
field_is_for_all: Para todos os projetos
lib/plugins/acts_as_activity_provider/lib/acts_as_activity_provider.rb
scope = (provider_options[:scope] || self)
if from && to
scope = scope.where("#{provider_options[:timestamp]} BETWEEN ? AND ?", from, to)
end
scope = scope.where("#{provider_options[:timestamp]} >= ?", from) if from
scope = scope.where("#{provider_options[:timestamp]} <= ?", to) if to
if options[:author]
return [] if provider_options[:author_key].nil?
......
scope = scope.where(Project.allowed_to_condition(user, "view_#{self.name.underscore.pluralize}".to_sym, options))
end
if options[:last_by_project]
scope = scope.group("#{Project.table_name}.id").maximum(provider_options[:timestamp])
end
scope.to_a
end
end
lib/redmine/activity/fetcher.rb
def events(from = nil, to = nil, options={})
e = []
@options[:limit] = options[:limit]
@options[:last_by_project] = options[:last_by_project] if options[:last_by_project]
@scope.each do |event_type|
constantized_providers(event_type).each do |provider|
......
end
end
e.sort! {|a,b| b.event_datetime <=> a.event_datetime}
if options[:last_by_project]
e.sort!
else
e.sort! {|a,b| b.event_datetime <=> a.event_datetime}
if options[:limit]
e = e.slice(0, options[:limit])
if options[:limit]
e = e.slice(0, options[:limit])
end
end
e
end
test/functional/projects_controller_test.rb
assert_select ".total-for-cf-#{field.id} span.value", :text => '9'
end
def test_index_with_last_activity_date_column
with_settings :project_list_defaults => {'column_names' => %w(name short_description last_activity_date)} do
get :index, :params => {
:display_type => 'list'
}
assert_response :success
end
assert_equal ['Name', 'Description', 'Last activity'], columns_in_list
assert_select 'tr#project-1 td.last_activity_date', :text => format_time(Journal.find(3).created_on)
assert_select 'tr#project-4 td.last_activity_date', :text => ''
end
def test_autocomplete_js
get(
:autocomplete,
test/unit/project_test.rb
assert_equal 'valuea', project.custom_field_value(cf1)
assert_nil project.custom_field_value(cf2)
end
def test_last_activity_date
# Note with id 3 is the last activity on Project 1
assert_equal Journal.find(3).created_on, Project.find(1).last_activity_date
# Project without activity should return nil
assert_equal nil, Project.find(4).last_activity_date
end
end
(9-9/9)