Module: CanCan::ModelAdapters::ActiveRecordAdapter
- Includes:
- Joins
- Included in:
- ActiveRecord4Adapter
- Defined in:
- lib/cancan/model_adapters/active_record_adapter.rb,
lib/cancan/model_adapters/can_can/model_adapters/active_record_adapter/joins.rb
Defined Under Namespace
Modules: Joins
Instance Method Summary collapse
- #calculate_nested(model_class, name, result_hash, value) ⇒ Object
- #calculate_result_hash(model_class, name, result_hash, value) ⇒ Object
-
#conditions ⇒ Object
Returns conditions intended to be used inside a database query.
- #database_records ⇒ Object
- #extract_multiple_conditions ⇒ Object
- #tableized_conditions(conditions, model_class = @model_class) ⇒ Object
Methods included from Joins
Instance Method Details
#calculate_nested(model_class, name, result_hash, value) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/cancan/model_adapters/active_record_adapter.rb', line 55 def calculate_nested(model_class, name, result_hash, value) value.each_with_object({}) do |(k, v), nested| if v.is_a? Hash value.delete(k) nested[k] = v else result_hash[model_class.reflect_on_association(name).table_name.to_sym] = value end nested end end |
#calculate_result_hash(model_class, name, result_hash, value) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/cancan/model_adapters/active_record_adapter.rb', line 44 def calculate_result_hash(model_class, name, result_hash, value) if value.is_a? Hash association_class = model_class.reflect_on_association(name).klass.name.constantize nested_resulted = calculate_nested(model_class, name, result_hash, value.dup) result_hash.merge!(tableized_conditions(nested_resulted, association_class)) else result_hash[name] = value end result_hash end |
#conditions ⇒ Object
Returns conditions intended to be used inside a database query. Normally you will not call this method directly, but instead go through ModelAdditions#accessible_by.
If there is only one “can” definition, a hash of conditions will be returned matching the one defined.
can :manage, User, :id => 1
query(:manage, User).conditions # => { :id => 1 }
If there are multiple “can” definitions, a SQL string will be returned to handle complex cases.
can :manage, User, :id => 1
can :manage, User, :manager_id => 1
cannot :manage, User, :self_managed => true
query(:manage, User).conditions # => "not (self_managed = 't') AND ((manager_id = 1) OR (id = 1))"
22 23 24 25 26 27 28 29 |
# File 'lib/cancan/model_adapters/active_record_adapter.rb', line 22 def conditions if @rules.size == 1 && @rules.first.base_behavior # Return the conditions directly if there's just one definition tableized_conditions(@rules.first.conditions).dup else extract_multiple_conditions end end |
#database_records ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/cancan/model_adapters/active_record_adapter.rb', line 67 def database_records if override_scope @model_class.where(nil).merge(override_scope) elsif @model_class.respond_to?(:where) && @model_class.respond_to?(:joins) mergeable_conditions? ? build_relation(conditions) : build_relation(*@rules.map(&:conditions)) else @model_class.all(conditions: conditions, joins: joins) end end |
#extract_multiple_conditions ⇒ Object
31 32 33 34 35 |
# File 'lib/cancan/model_adapters/active_record_adapter.rb', line 31 def extract_multiple_conditions @rules.reverse.inject(false_sql) do |sql, rule| merge_conditions(sql, tableized_conditions(rule.conditions).dup, rule.base_behavior) end end |
#tableized_conditions(conditions, model_class = @model_class) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/cancan/model_adapters/active_record_adapter.rb', line 37 def tableized_conditions(conditions, model_class = @model_class) return conditions unless conditions.is_a? Hash conditions.each_with_object({}) do |(name, value), result_hash| calculate_result_hash(model_class, name, result_hash, value) end end |