Skip to content

Instantly share code, notes, and snippets.

@andrzejkrzywda
Created February 2, 2014 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrzejkrzywda/8771276 to your computer and use it in GitHub Desktop.
Save andrzejkrzywda/8771276 to your computer and use it in GitHub Desktop.
def create
CreateTimeEntryService.new(self).call()
end
class CreateTimeEntryService < SimpleDelegator
def initialize(parent)
super(parent)
end
def call
project = nil
begin
project_id = (params[:project_id] || params[:time_entry] && params[:time_entry][:project_id])
if project_id.present?
project = Project.find(project_id)
end
issue_id = (params[:issue_id] || params[:time_entry] && params[:time_entry][:issue_id])
if issue_id.present?
issue = Issue.find(issue_id)
project ||= issue.project
end
rescue ActiveRecord::RecordNotFound
render_404 and return
end
if project.nil?
render_404
return
end
allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, project, :global => false)
if ! allowed
if project.archived?
render_403 :message => :notice_not_authorized_archived_project
return false
else
deny_access
return false
end
end
time_entry ||= TimeEntry.new(:project => project, :issue => issue, :user => User.current, :spent_on => User.current.today)
time_entry.safe_attributes = params[:time_entry]
call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => time_entry })
if time_entry.save
respond_to do |format|
format.html {
flash[:notice] = l(:notice_successful_create)
if params[:continue]
if params[:project_id]
options = {
:time_entry => {:issue_id => time_entry.issue_id, :activity_id => time_entry.activity_id},
:back_url => params[:back_url]
}
if time_entry.issue
redirect_to new_project_issue_time_entry_path(time_entry.project, time_entry.issue, options)
else
redirect_to new_project_time_entry_path(time_entry.project, options)
end
else
options = {
:time_entry => {:project_id => time_entry.project_id, :issue_id => time_entry.issue_id, :activity_id => time_entry.activity_id},
:back_url => params[:back_url]
}
redirect_to new_time_entry_path(options)
end
else
redirect_back_or_default project_time_entries_path(time_entry.project)
end
}
format.api { render 'show', :status => :created, :location => time_entry_url(time_entry), :locals => {:time_entry => time_entry} }
end
else
respond_to do |format|
format.html { render :new, :locals => {:time_entry => time_entry, :project => project} }
format.api { render_validation_errors(time_entry) }
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment