Skip to content

Instantly share code, notes, and snippets.

@dhh
Last active May 13, 2016 13:36
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhh/10023987 to your computer and use it in GitHub Desktop.
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.
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