Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

treat ActiveRecord::Relation as Array #917

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/cancan/controller_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,24 @@ def name
def resource_params
if @options[:class]
params_key = extract_key(@options[:class])
return @params[params_key] if @params[params_key]
if params = fetch_params(params_key)
return params
end
end

resource_params_by_namespaced_name
end

def resource_params_by_namespaced_name
@params[extract_key(namespaced_name)]
fetch_params extract_key(namespaced_name)
end

def fetch_params(key)
@controller.respond_to?(params_method(key), true) ? @controller.send(params_method(key)) : @params[key]
end

def params_method(key)
"#{key}_params".to_sym
end

def namespace
Expand Down
4 changes: 2 additions & 2 deletions lib/cancan/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
ability.can?(*args)
end

failure_message_for_should do |ability|
failure_message do |ability|
"expected to be able to #{args.map(&:inspect).join(" ")}"
end

failure_message_for_should_not do |ability|
failure_message_when_negated do |ability|
"expected not to be able to #{args.map(&:inspect).join(" ")}"
end
end
2 changes: 1 addition & 1 deletion lib/cancan/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def matches_conditions_hash?(subject, conditions = @conditions)
else
attribute = subject.send(name)
if value.kind_of?(Hash)
if attribute.kind_of? Array
if attribute.kind_of?(Array) || attribute.kind_of?(ActiveRecord::Relation)
attribute.any? { |element| matches_conditions_hash? element, value }
else
!attribute.nil? && matches_conditions_hash?(attribute, value)
Expand Down