Skip to content

Commit

Permalink
Refactor: extract TimelogController#create from TimelogController#edit
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4244 e93f8b46-1217-0410-a6f0-8f06a7374b81
  • Loading branch information
edavis10 committed Oct 8, 2010
1 parent 84ebd78 commit 4acd990
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 8 deletions.
17 changes: 16 additions & 1 deletion app/controllers/timelog_controller.rb
Expand Up @@ -17,7 +17,7 @@

class TimelogController < ApplicationController
menu_item :issues
before_filter :find_project, :authorize, :only => [:new, :edit, :destroy]
before_filter :find_project, :authorize, :only => [:new, :create, :edit, :destroy]
before_filter :find_optional_project, :only => [:index]

verify :method => :post, :only => :destroy, :redirect_to => { :action => :index }
Expand Down Expand Up @@ -93,6 +93,21 @@ def new
call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
render :action => 'edit'
end

verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
def create
@time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
@time_entry.attributes = params[:time_entry]

call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })

if @time_entry.save
flash[:notice] = l(:notice_successful_update)
redirect_back_or_default :action => 'index', :project_id => @time_entry.project
else
render :action => 'edit'
end
end

def edit
(render_403; return) if @time_entry && !@time_entry.editable_by?(User.current)
Expand Down
2 changes: 1 addition & 1 deletion app/views/timelog/edit.rhtml
@@ -1,6 +1,6 @@
<h2><%= l(:label_spent_time) %></h2>

<% labelled_tabular_form_for :time_entry, @time_entry, :url => {:action => 'edit', :id => @time_entry, :project_id => @time_entry.project} do |f| %>
<% labelled_tabular_form_for :time_entry, @time_entry, :url => {:action => (@time_entry.new_record? ? 'create' : 'edit'), :id => @time_entry, :project_id => @time_entry.project} do |f| %>
<%= error_messages_for 'time_entry' %>
<%= back_url_hidden_field_tag %>

Expand Down
3 changes: 2 additions & 1 deletion config/routes.rb
Expand Up @@ -40,7 +40,8 @@
timelog.with_options :action => 'new', :conditions => {:method => :get} do |time_edit|
time_edit.connect 'issues/:issue_id/time_entries/new'
end


timelog.connect 'projects/:project_id/timelog/edit', :action => 'create', :conditions => {:method => :post}
timelog.connect 'time_entries/:id/destroy', :action => 'destroy', :conditions => {:method => :post}
end

Expand Down
6 changes: 3 additions & 3 deletions lib/redmine.rb
Expand Up @@ -84,10 +84,10 @@
end

map.project_module :time_tracking do |map|
map.permission :log_time, {:timelog => :edit}, :require => :loggedin
map.permission :log_time, {:timelog => [:new, :create, :edit]}, :require => :loggedin
map.permission :view_time_entries, :timelog => [:index], :time_entry_reports => [:report]
map.permission :edit_time_entries, {:timelog => [:new, :edit, :destroy]}, :require => :member
map.permission :edit_own_time_entries, {:timelog => [:new, :edit, :destroy]}, :require => :loggedin
map.permission :edit_time_entries, {:timelog => [:new, :create, :edit, :destroy]}, :require => :member
map.permission :edit_own_time_entries, {:timelog => [:new, :create, :edit, :destroy]}, :require => :loggedin
map.permission :manage_project_activities, {:project_enumerations => [:update, :destroy]}, :require => :member
end

Expand Down
4 changes: 2 additions & 2 deletions test/functional/timelog_controller_test.rb
Expand Up @@ -72,11 +72,11 @@ def test_get_edit_with_an_existing_time_entry_with_inactive_activity
assert_tag :tag => 'option', :content => '--- Please select ---'
end

def test_post_edit
def test_post_create
# TODO: should POST to issues’ time log instead of project. change form
# and routing
@request.session[:user_id] = 3
post :edit, :project_id => 1,
post :create, :project_id => 1,
:time_entry => {:comments => 'Some work on TimelogControllerTest',
# Not the default activity
:activity_id => '11',
Expand Down
1 change: 1 addition & 0 deletions test/integration/routing_test.rb
Expand Up @@ -238,6 +238,7 @@ class RoutingTest < ActionController::IntegrationTest
should_route :get, "/projects/ecookbook/issues/567/time_entries/new", :controller => 'timelog', :action => 'new', :project_id => 'ecookbook', :issue_id => '567'
should_route :get, "/time_entries/22/edit", :controller => 'timelog', :action => 'edit', :id => '22'

should_route :post, "/projects/ecookbook/timelog/edit", :controller => 'timelog', :action => 'create', :project_id => 'ecookbook'
should_route :post, "/time_entries/22/edit", :controller => 'timelog', :action => 'edit', :id => '22'
should_route :post, "/time_entries/55/destroy", :controller => 'timelog', :action => 'destroy', :id => '55'
end
Expand Down

0 comments on commit 4acd990

Please sign in to comment.