Skip to content

Instantly share code, notes, and snippets.

@mrmemes-eth
Created March 28, 2011 23:02
Show Gist options
  • Save mrmemes-eth/891504 to your computer and use it in GitHub Desktop.
Save mrmemes-eth/891504 to your computer and use it in GitHub Desktop.
A default exposure for decent_exposure that assigns instance variables (to make cancan happy)
default_exposure do |name|
collection = name.to_s.pluralize
if respond_to?(collection) && collection != name.to_s && send(collection).respond_to?(:scoped)
proxy = send(collection)
else
proxy = name.to_s.classify.constantize
end
instance_variable_set("@#{name}") = if id = params["#{name}_id"] || params[:id]
proxy.find(id).tap do |r|
r.attributes = params[name] unless request.get?
end
else
proxy.new(params[name])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment