Refactor and add filtering to item_component.rb

This commit is contained in:
Mir Bhatia
2023-04-25 18:51:52 +02:00
parent 3ddffb8653
commit f67dcaa388
2 changed files with 27 additions and 19 deletions
+23 -18
View File
@@ -41,7 +41,7 @@ class Activities::ItemComponent < ViewComponent::Base
end
def project_suffix
return if project_activity?
return if activity?(Project)
return if activity_is_from_current_project?
kind = activity_is_from_subproject? ? 'subproject' : 'project'
@@ -60,42 +60,31 @@ class Activities::ItemComponent < ViewComponent::Base
end
def rendered_details
@rendered_details ||=
@event.journal
.details
.filter_map { |detail| @event.journal.render_detail(detail, activity_page: @activity_page) }
filter_details.filter_map { |detail| @event.journal.render_detail(detail, activity_page: @activity_page) }
end
def comment
return unless work_package_activity?
return unless activity?(WorkPackage)
@event.event_description
end
def description
return if work_package_activity?
return if activity?(WorkPackage) || activity?(TimeEntry)
@event.event_description
end
def time_entry_url
return unless time_entry_activity?
return unless activity?(TimeEntry)
@event.event_url
end
private
def work_package_activity?
@event.journal.journable_type == 'WorkPackage'
end
def project_activity?
@event.journal.journable_type == 'Project'
end
def time_entry_activity?
@event.journal.journable_type == 'TimeEntry'
def activity?(type)
@event.journal.journable_type == type.to_s
end
def activity_is_from_current_project?
@@ -105,4 +94,20 @@ class Activities::ItemComponent < ViewComponent::Base
def activity_is_from_subproject?
@current_project && (@event.project != @current_project)
end
def filter_details
details = @event.journal.details
details.delete(:user_id) if details[:logged_by_id] == details[:user_id]
delete_detail(details, :work_package_id)
delete_detail(details, :comments)
delete_detail(details, :activity_id)
delete_detail(details, :spent_on)
details
end
def delete_detail(details, field)
details.delete(field) if details[field] && details[field].first.nil?
end
end
+4 -1
View File
@@ -58,8 +58,11 @@ class TimeEntry < ApplicationRecord
before_save :update_costs
register_journal_formatted_fields(:time_entry_hours, 'hours')
register_journal_formatted_fields(:named_association, 'work_package_id')
register_journal_formatted_fields(:time_entry_named_association, 'user_id')
register_journal_formatted_fields(:named_association, 'work_package_id')
register_journal_formatted_fields(:named_association, 'activity_id')
register_journal_formatted_fields(:plaintext, 'comments')
register_journal_formatted_fields(:plaintext, 'spent_on')
def self.update_all(updates, conditions = nil, options = {})
# instead of a update_all, perform an individual update during work_package#move