Refactor: extract TimelogController#create from TimelogController#edit

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4244 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Eric Davis 2010-10-08 15:39:39 +00:00
parent 84ebd786d6
commit 4acd990ee2
6 changed files with 25 additions and 8 deletions

View File

@ -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)

View File

@ -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 %>

View File

@ -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

View File

@ -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

View File

@ -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',

View File

@ -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