Skip to content

Commit 1b330ef

Browse files
committed
Fix rubocop
1 parent d5365b1 commit 1b330ef

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

lib/cancan/model_adapters/conditions_normalizer.rb

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,38 @@
55
module CanCan
66
module ModelAdapters
77
class ConditionsNormalizer
8-
def self.normalize(model_class, rules)
9-
rules.each { |rule| rule.conditions = normalize_conditions(model_class, rule.conditions) }
10-
end
8+
class << self
9+
def normalize(model_class, rules)
10+
rules.each { |rule| rule.conditions = normalize_conditions(model_class, rule.conditions) }
11+
end
1112

12-
def self.normalize_conditions(model_class, conditions)
13-
return conditions unless conditions.is_a? Hash
13+
def normalize_conditions(model_class, conditions)
14+
return conditions unless conditions.is_a? Hash
1415

15-
conditions.each_with_object({}) do |(key, value), result_hash|
16-
if value.is_a? Hash
17-
result_hash.merge!(calculate_result_hash(model_class, key, value))
18-
else
19-
result_hash[key] = value
16+
conditions.each_with_object({}) do |(key, value), result_hash|
17+
if value.is_a? Hash
18+
result_hash.merge!(calculate_result_hash(model_class, key, value))
19+
else
20+
result_hash[key] = value
21+
end
22+
result_hash
2023
end
21-
result_hash
2224
end
23-
end
2425

25-
private
26+
private
2627

27-
def self.calculate_result_hash(model_class, key, value)
28-
reflection = model_class.reflect_on_association(key)
29-
raise WrongAssociationName, "Association '#{key}' not defined in model '#{model_class.name}'" unless reflection
28+
def calculate_result_hash(model_class, key, value)
29+
reflection = model_class.reflect_on_association(key)
30+
unless reflection
31+
raise WrongAssociationName, "Association '#{key}' not defined in model '#{model_class.name}'"
32+
end
33+
34+
if reflection.options[:through].present?
35+
key = reflection.options[:through]
36+
value = { reflection.source_reflection_name => value }
37+
reflection = model_class.reflect_on_association(key)
38+
end
3039

31-
if reflection.options[:through].present?
32-
normalized_key = reflection.options[:through]
33-
normalized_value = { reflection.source_reflection_name => value }
34-
reflection_klass = model_class.reflect_on_association(normalized_key).klass.name.constantize
35-
{ normalized_key => normalize_conditions(reflection_klass, normalized_value) }
36-
else
3740
{ key => normalize_conditions(reflection.klass.name.constantize, value) }
3841
end
3942
end

spec/cancan/model_adapters/active_record_adapter_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,8 @@ class User < ActiveRecord::Base
306306
@ability.cannot :read, Article, published: false, secret: true
307307
expect(@ability.model_adapter(Article, :read).conditions)
308308
.to orderlessly_match(
309-
%["not (#{@article_table}"."published" = #{false_v} AND "#{@article_table}"."secret" = #{true_v})]
310-
)
309+
%["not (#{@article_table}"."published" = #{false_v} AND "#{@article_table}"."secret" = #{true_v})]
310+
)
311311
end
312312

313313
it 'returns appropriate sql conditions in complex case' do

0 commit comments

Comments
 (0)