the new and create actions will now build the resource with attributes based on ability conditions hash - closes #114
This commit is contained in:
@@ -206,6 +206,14 @@ module CanCan
|
||||
message.blank? ? nil : message
|
||||
end
|
||||
|
||||
def attributes_for(action, subject)
|
||||
attributes = {}
|
||||
relevant_can_definitions(action, subject).map do |can_definition|
|
||||
attributes.merge!(can_definition.new_attributes) if can_definition.base_behavior
|
||||
end
|
||||
attributes
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def unauthorized_message_keys(action, subject)
|
||||
|
||||
@@ -67,6 +67,14 @@ module CanCan
|
||||
hash
|
||||
end
|
||||
|
||||
def new_attributes
|
||||
attributes = {}
|
||||
@conditions.each do |key, value|
|
||||
attributes[key] = value unless [Array, Range, Hash].include? value.class
|
||||
end
|
||||
attributes
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def subject_class?(subject)
|
||||
|
||||
@@ -51,7 +51,15 @@ module CanCan
|
||||
|
||||
def build_resource
|
||||
method_name = @options[:singleton] ? "build_#{name}" : "new"
|
||||
resource_base.send(*[method_name, @params[name]].compact)
|
||||
resource = resource_base.send(*[method_name, @params[name]].compact)
|
||||
initial_attributes.each do |name, value|
|
||||
resource.send("#{name}=", value)
|
||||
end
|
||||
resource
|
||||
end
|
||||
|
||||
def initial_attributes
|
||||
@controller.current_ability.attributes_for(@params[:action].to_sym, resource_class)
|
||||
end
|
||||
|
||||
def find_resource
|
||||
|
||||
Reference in New Issue
Block a user