Skip to content

Instantly share code, notes, and snippets.

@aharpole
Created September 5, 2014 05:53
Show Gist options
  • Save aharpole/113ae8f4dda72a9b9efd to your computer and use it in GitHub Desktop.
Save aharpole/113ae8f4dda72a9b9efd to your computer and use it in GitHub Desktop.
before/after a massive controller refactoring
def update
p "Update is being called for out_of_money for #{params[:opportunity]}" if !params[:out_of_money].blank?
@opportunity = Opportunity.find(params[:id])
@opportunity.talking_points_boilerplate = params[:talking_points_boilerplate] if params[:talking_points_boilerplate]
@opportunity.description_link_boilerplate = params[:description_link_boilerplate] if params[:description_link_boilerplate]
@opportunity.annotation_boilerplate = params[:annotation_boilerplate] if params[:annotation_boilerplate]
demographics_to_save = []
params.fetch(:demographics,{}).each do |d|
demographics_to_save << d[1]
end
@opportunity.targeted_demographics = demographics_to_save
@opportunity.geotargeted_countries = (params[:opportunity].delete(:geotargeted_countries) || {}).keys
params[:opportunity].delete(:targeted_views)
params[:opportunity].delete(:cpv_specific_countries)
old_budget = @opportunity.budget
last_start_date = @opportunity.start_time
@opportunity.talking_points = []
@opportunity.talking_points = params[:talking_points].values if params[:talking_points].present?
@opportunity.talking_points.compact!
@opportunity.talking_points.delete_if { |tp| tp == '' }
old_link = @opportunity.description_link
@opportunity.save
@opportunity.update_attributes(params[:opportunity])
@opportunity.description_link = BitlyHelper.bitly_url(@opportunity.description_link) if (@opportunity.description_link.present? && @opportunity.description_link != @opportunity.description_link_was)
case
when @opportunity.budget > old_budget
@opportunity.budget_increased(old_budget)
Resque.enqueue(AdvertiserAddedFunds, @opportunity.id.to_s)
when @opportunity.budget < old_budget
@opportunity.budget_decreased(old_budget)
end
if @opportunity.save
redirect_to @opportunity, notice: 'Opportunity was successfully updated.'
else
render action: 'edit'
end
end
def update
@opportunity.assign_attributes params[:opportunity]
if @opportunity.save
redirect_to edit_opportunity_path(@opportunity), notice: 'Opportunity was successfully updated.'
else
@opportunity.build_photo unless @opportunity.photo
@opportunity.images.build
@opportunity.videos.build
render action: 'edit', layout: 'opportunity'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment