def initialize(attributes=nil, *args)
super
if new_record? && self.activity.nil?
- if default_activity = TimeEntryActivity.default
+ if default_activity = TimeEntryActivity.default(self.project)
self.activity_id = default_activity.id
end
self.hours = nil if hours == 0
OptionName = :enumeration_activities
+ def self.default(project=nil)
+ default_activity = super()
+
+ if default_activity.nil? || project.nil? || project.activities.blank? || project.activities.include?(default_activity)
+ return default_activity
+ end
+
+ project.activities.detect { |activity| activity.parent_id == default_activity.id }
+ end
+
def option_name
OptionName
end
)
end
end
+
+ def test_default_should_return_default_activity_if_default_activity_is_included_in_the_project_activities
+ project = Project.find(1)
+ assert_equal TimeEntryActivity.default(project).id, 10
+ end
+
+ def test_default_should_return_project_specific_default_activity_if_default_activity_is_not_included_in_the_project_activities
+ project = Project.find(1)
+ project_specific_default_activity = TimeEntryActivity.create!(name: 'Development', parent_id: 10, project_id: project.id, is_default: false)
+ assert_not_equal TimeEntryActivity.default(project).id, 10
+ assert_equal TimeEntryActivity.default(project).id, project_specific_default_activity.id
+ end
end
end
end
+ def test_activity_id_should_default_activity_id
+ project = Project.find(1)
+ default_activity = TimeEntryActivity.find(10)
+ entry = TimeEntry.new(project: project)
+ assert_equal entry.activity_id, default_activity.id
+
+ # If there are project specific activities
+ project_specific_default_activity = TimeEntryActivity.create!(name: 'Development', parent_id: 10, project_id: project.id, is_default: false)
+ entry = TimeEntry.new(project: project)
+ assert_not_equal entry.activity_id, default_activity.id
+ assert_equal entry.activity_id, project_specific_default_activity.id
+ end
+
def test_should_accept_future_dates
entry = TimeEntry.generate
entry.spent_on = User.current.today + 1