From 4acd990ee2950a575a35b32bdfd8a75f04e958b0 Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Fri, 8 Oct 2010 15:39:39 +0000 Subject: Refactor: extract TimelogController#create from TimelogController#edit git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4244 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/timelog_controller.rb | 17 ++++++++++++++++- app/views/timelog/edit.rhtml | 2 +- config/routes.rb | 3 ++- lib/redmine.rb | 6 +++--- test/functional/timelog_controller_test.rb | 4 ++-- test/integration/routing_test.rb | 1 + 6 files changed, 25 insertions(+), 8 deletions(-) diff --git a/app/controllers/timelog_controller.rb b/app/controllers/timelog_controller.rb index d15ea6a54..b9f8724a0 100644 --- a/app/controllers/timelog_controller.rb +++ b/app/controllers/timelog_controller.rb @@ -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 } @@ -93,6 +93,21 @@ class TimelogController < ApplicationController 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) diff --git a/app/views/timelog/edit.rhtml b/app/views/timelog/edit.rhtml index 00d0a77c0..79a95b572 100644 --- a/app/views/timelog/edit.rhtml +++ b/app/views/timelog/edit.rhtml @@ -1,6 +1,6 @@

<%= l(:label_spent_time) %>

-<% 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 %> diff --git a/config/routes.rb b/config/routes.rb index 34ee875ea..702bcf89e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -40,7 +40,8 @@ ActionController::Routing::Routes.draw do |map| 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 diff --git a/lib/redmine.rb b/lib/redmine.rb index 85e07fd2e..10bdf4c24 100644 --- a/lib/redmine.rb +++ b/lib/redmine.rb @@ -84,10 +84,10 @@ Redmine::AccessControl.map do |map| 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 diff --git a/test/functional/timelog_controller_test.rb b/test/functional/timelog_controller_test.rb index 823650257..91b8ca47c 100644 --- a/test/functional/timelog_controller_test.rb +++ b/test/functional/timelog_controller_test.rb @@ -72,11 +72,11 @@ class TimelogControllerTest < ActionController::TestCase 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', diff --git a/test/integration/routing_test.rb b/test/integration/routing_test.rb index 3ba01cdc1..8f96745bb 100644 --- a/test/integration/routing_test.rb +++ b/test/integration/routing_test.rb @@ -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 -- cgit v1.2.3