Class: CanCan::ModelAdapters::MongoidAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- CanCan::ModelAdapters::MongoidAdapter
- Defined in:
- lib/cancancan/model_adapters/mongoid_adapter.rb
Class Method Summary collapse
- .for_class?(model_class) ⇒ Boolean
- .matches_conditions_hash?(subject, conditions) ⇒ Boolean
- .override_conditions_hash_matching?(subject, conditions) ⇒ Boolean
Instance Method Summary collapse
Class Method Details
.for_class?(model_class) ⇒ Boolean
4 5 6 |
# File 'lib/cancancan/model_adapters/mongoid_adapter.rb', line 4 def self.for_class?(model_class) model_class <= Mongoid::Document end |
.matches_conditions_hash?(subject, conditions) ⇒ Boolean
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/cancancan/model_adapters/mongoid_adapter.rb', line 19 def self.matches_conditions_hash?(subject, conditions) # To avoid hitting the db, retrieve the raw Mongo selector from # the Mongoid Criteria and use Mongoid::Matchers#matches? q = subject.class.where(conditions).selector if subject.respond_to?(:_matches?) subject._matches?(q) else subject.matches?(q) end end |
.override_conditions_hash_matching?(subject, conditions) ⇒ Boolean
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/cancancan/model_adapters/mongoid_adapter.rb', line 8 def self.override_conditions_hash_matching?(subject, conditions) conditions.any? do |k, _v| key_is_not_symbol = -> { !k.is_a?(Symbol) } subject_value_is_array = lambda do subject.respond_to?(k) && subject.send(k).is_a?(Array) end key_is_not_symbol.call || subject_value_is_array.call end end |
Instance Method Details
#database_records ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/cancancan/model_adapters/mongoid_adapter.rb', line 30 def database_records if @rules.empty? @model_class.where(_id: { '$exists' => false, '$type' => 7 }) # return no records in Mongoid elsif @rules.size == 1 && @rules[0].conditions.is_a?(Mongoid::Criteria) @rules[0].conditions else # we only need to process can rules if # there are no rules with empty conditions database_records_from_multiple_rules end end |
#database_records_from_multiple_rules ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/cancancan/model_adapters/mongoid_adapter.rb', line 42 def database_records_from_multiple_rules rules = @rules.reject { |rule| rule.conditions.empty? && rule.base_behavior } process_can_rules = @rules.count == rules.count rules.inject(@model_class.all) do |records, rule| if process_can_rules && rule.base_behavior records.or simplify_relations(@model_class, rule.conditions) elsif !rule.base_behavior records.excludes simplify_relations(@model_class, rule.conditions) else records end end end |