Skip to content

Commit

Permalink
Refactor: Changed TimeEntry#create_bulk_time_entry so it will actully…
Browse files Browse the repository at this point in the history
… save the record.
  • Loading branch information
edavis10 committed Jan 28, 2010
1 parent 2ee973d commit d60f9c8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/controllers/bulk_time_entries_controller.rb
Expand Up @@ -36,7 +36,7 @@ def save
render :update do |page|
@time_entries.each_pair do |html_id, entry|
@time_entry = TimeEntry.create_bulk_time_entry(entry)
unless @time_entry && @time_entry.save
if @time_entry.new_record?
page.replace "entry_#{html_id}", :partial => 'time_entry', :object => @time_entry
else
time_entry_target = if @time_entry.issue
Expand Down
6 changes: 4 additions & 2 deletions lib/bulk_time_entry_plugin/patches/time_entry_patch.rb
Expand Up @@ -8,11 +8,13 @@ def self.included(base)
module ClassMethods

def create_bulk_time_entry(entry)
return false unless BulkTimeEntriesController.allowed_project?(entry[:project_id])
time_entry = TimeEntry.new(entry)
time_entry.hours = nil if time_entry.hours.blank? or time_entry.hours <= 0
time_entry.project_id = entry[:project_id] # project_id is protected from mass assignment
if BulkTimeEntriesController.allowed_project?(entry[:project_id])
time_entry.project_id = entry[:project_id] # project_id is protected from mass assignment
end
time_entry.user = User.current
time_entry.save
time_entry
end

Expand Down
Expand Up @@ -7,17 +7,26 @@ class BulkTimeEntryPlugin::Patches::TimeEntryPatchTest < ActiveSupport::TestCase
@project = Project.generate!
@role = Role.generate!(:permissions => Redmine::AccessControl.permissions.collect(&:name))
Member.generate!(:project => @project, :roles => [@role], :user_id => @user.id)
@valid_params = {:project_id => @project.id, :hours => 5}
@valid_params = {:project_id => @project.id, :hours => 5, :activity_id => TimeEntryActivity.generate!.id, :spent_on => Date.today.to_s}
end

should "return false if the current user is not allowed to log time to the project" do
should "return the unsaved TimeEntry if the current user is not allowed to log time to the project" do
@role.update_attributes(:permissions => [])

assert_equal false, TimeEntry.create_bulk_time_entry(@valid_params)
assert_no_difference('TimeEntry.count') do
@entry = TimeEntry.create_bulk_time_entry(@valid_params)
end
assert_equal false, @entry.valid?
end

context "saving a valid record" do
should "save a new Time Entry record"
should "save a new Time Entry record" do
assert_difference('TimeEntry.count') do
@entry = TimeEntry.create_bulk_time_entry(@valid_params)
end

assert @entry.is_a? TimeEntry
end

should "set the project of the new record" do
@entry = TimeEntry.create_bulk_time_entry(@valid_params)
Expand Down

0 comments on commit d60f9c8

Please sign in to comment.