Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved the save_time_entry_from_params from the BulkTimeEntriesController to the TimeEntry (patched via the plugin). Also includes some additional fixes: * Added some tests for the behavior * Fixed a bug the method body, was using next but not inside a loop * Fixed a bug in #allowed_project?, wasn't using :conditions
- Loading branch information
Showing
4 changed files
with
73 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module BulkTimeEntryPlugin | ||
module Patches | ||
module TimeEntryPatch | ||
def self.included(base) | ||
base.extend ClassMethods | ||
end | ||
|
||
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 | ||
time_entry.user = User.current | ||
time_entry | ||
end | ||
|
||
end | ||
|
||
end | ||
end | ||
end |
40 changes: 40 additions & 0 deletions
40
test/unit/lib/bulk_time_entry_plugin/patches/time_entry_patch_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'test_helper' | ||
|
||
class BulkTimeEntryPlugin::Patches::TimeEntryPatchTest < ActiveSupport::TestCase | ||
context "TimeEntry#create_bulk_time_entry" do | ||
setup do | ||
User.current = @user = User.generate_with_protected! | ||
@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} | ||
end | ||
|
||
should "return false 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) | ||
end | ||
|
||
context "saving a valid record" do | ||
should "save a new Time Entry record" | ||
|
||
should "set the project of the new record" do | ||
@entry = TimeEntry.create_bulk_time_entry(@valid_params) | ||
assert_equal @project, @entry.project | ||
end | ||
|
||
should "assign the current user" do | ||
@entry = TimeEntry.create_bulk_time_entry(@valid_params) | ||
assert_equal @user, @entry.user | ||
end | ||
|
||
should "set hours to nil if they are passed in as 0" do | ||
@entry = TimeEntry.create_bulk_time_entry(@valid_params.merge(:hours => 0)) | ||
assert_equal @user, @entry.user | ||
end | ||
|
||
end | ||
|
||
end | ||
end |