Last active
May 13, 2016 13:36
-
-
Save dhh/10023987 to your computer and use it in GitHub Desktop.
Partial refactoring of https://github.com/redmine/redmine/blob/master/app/controllers/timelog_controller.rb#L104. That whole controller would need a serious amount of work to be whipped into shape, but this is a start.
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
class TimeEntriesController < ApplicationController | |
before_action :set_project, :set_issue | |
def create | |
@time_entry = container.time_entries.build time_entry_params.merge(user: User.current) | |
if @time_entry.save | |
respond_to do |format| | |
format.html { redirect_back_or_default created_time_entry_url, notice: l(:notice_successful_create) } | |
format.api { render :show, status: :created, location: @time_entry } | |
end | |
else | |
respond_to do |format| | |
format.html { render :new } | |
format.api { render_validation_errors @time_entry } | |
end | |
end | |
end | |
private | |
def container | |
@issue || @project | |
end | |
def created_time_entry_url | |
if params[:continue] | |
polymorphic_url [ @project, @issue, TimeEntry.new ], | |
time_entry_id: @time_entry.id, back_url: params[:back_url] | |
else | |
project_time_entries_url @project | |
end | |
end | |
end | |
# Don't pass in what can be derived from other assignments | |
class TimeEntry < ActiveRecord::Base | |
before_save { self.spent_on ||= user.today } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment