Skip to content

Instantly share code, notes, and snippets.

@libin0120
Created November 17, 2011 02:14
Show Gist options
  • Save libin0120/1372180 to your computer and use it in GitHub Desktop.
Save libin0120/1372180 to your computer and use it in GitHub Desktop.
Able to have CANCAN's new record attributes initialization feature when using inherit_resources
CanCan::InheritedResource.class_eval do
# Copied from CACAN added attributes initialization for new_actions
def load_resource_instance
if parent?
@controller.send :association_chain
@controller.instance_variable_get("@#{instance_name}")
elsif new_actions.include? @params[:action].to_sym
# [[[ - initialize new object attributes according capability.
resource = @controller.send :build_resource
initial_attributes.each do |attr_name, value|
resource.send("#{attr_name}=", value)
end
@controller.send :resource
# ]]]
else
@controller.send :resource
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment