Skip to content

Commit

Permalink
Merge pull request #5 from sbilharz/initial_attributes
Browse files Browse the repository at this point in the history
Enable record attribute initiation with rules from context
  • Loading branch information
galetahub committed Jan 13, 2016
2 parents d4b3181 + 37f82c4 commit 87b5ef9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/cancan_namespace/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ def model_adapter(model_class, action, context = nil)
def has_block?(action, subject, context = nil)
relevant_rules(action, subject, context).any?(&:only_block?)
end

def attributes_for(action, subject, context = nil)
attributes = {}
relevant_rules(action, subject, context).each do |rule|
attributes.merge!(rule.attributes_from_conditions) if rule.base_behavior
end
attributes
end

private

Expand Down
7 changes: 7 additions & 0 deletions lib/cancan_namespace/controller_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def self.extended(base)
alias_method :authorize_resource, :authorize_resource_with_context
alias_method :load_collection?, :load_collection_with_context?
alias_method :load_collection, :load_collection_with_context
alias_method :initial_attributes, :initial_attributes_with_context
end
end
end
Expand All @@ -34,6 +35,12 @@ def load_collection_with_context?
def load_collection_with_context
resource_base.accessible_by(current_ability, authorization_action, @options[:context] || @controller.get_context)
end

def initial_attributes_with_context
current_ability.attributes_for(@params[:action].to_sym, resource_class, @options[:context] || @controller.get_context).delete_if do |key, value|
resource_params && resource_params.include?(key)
end
end
end
end
end
Expand Down

0 comments on commit 87b5ef9

Please sign in to comment.