Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Based on the original post (http://robots.thoughtbot.com/code-show-and-tell-polymorphic-finder), I think this would have been a simpler refactor.
class ApplicationController < ActionController::Base
private
def requested_purchaseable
RequestPurchase.new(params).find
end
end
RequestPurchase = Struct.new(:params) do
def find
product || individual_plan || team_plan || section || raise_unknown
end
private
def product
if id = params.slice(:product_id, :screencast_id, :book_id, :show_id).compact.first
Product.find(id)
end
end
def individual_plan
if id = params[:individual_plan_id]
IndividualPlan.where(sku: id).first!
end
end
def team_plan
if id = params[:team_plan_id]
TeamPlan.where(sku: id).first!
end
end
def section
if id = params[:section_id]
Section.find(id)
end
end
def raise_unknown
raise "Could not find a purchaseable object from given params: #{params}"
end
end
@stevebooks

This comment has been minimized.

Copy link

commented Jul 29, 2014

In what directory would you normally store request_purchase.rb?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.