Class: ActiveRecordHas::Scope
- Inherits:
-
Object
- Object
- ActiveRecordHas::Scope
- Defined in:
- lib/active_record_has/scope.rb
Instance Attribute Summary collapse
-
#association ⇒ Object
readonly
Returns the value of attribute association.
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#reflection ⇒ Object
readonly
Returns the value of attribute reflection.
-
#relation ⇒ Object
readonly
Returns the value of attribute relation.
Instance Method Summary collapse
- #exists ⇒ Object
-
#initialize(model, association, relation, **options, &block) ⇒ Scope
constructor
A new instance of Scope.
- #scope ⇒ Object
Constructor Details
#initialize(model, association, relation, **options, &block) ⇒ Scope
Returns a new instance of Scope.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/active_record_has/scope.rb', line 7 def initialize(model, association, relation, **, &block) @model = model @association = association @block = block = @reflection ||= if association.is_a? Symbol @relation = relation if relation.is_a? ActiveRecord::Relation @reflection = reflections[association.to_s] elsif association.is_a? ActiveRecord::Relation @relation = association model_name = association.model_name @reflection = reflections[model_name.plural] || reflections[model_name.singular] else nil end end |
Instance Attribute Details
#association ⇒ Object (readonly)
Returns the value of attribute association.
5 6 7 |
# File 'lib/active_record_has/scope.rb', line 5 def association @association end |
#block ⇒ Object (readonly)
Returns the value of attribute block.
5 6 7 |
# File 'lib/active_record_has/scope.rb', line 5 def block @block end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
5 6 7 |
# File 'lib/active_record_has/scope.rb', line 5 def model @model end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/active_record_has/scope.rb', line 5 def end |
#reflection ⇒ Object (readonly)
Returns the value of attribute reflection.
5 6 7 |
# File 'lib/active_record_has/scope.rb', line 5 def reflection @reflection end |
#relation ⇒ Object (readonly)
Returns the value of attribute relation.
5 6 7 |
# File 'lib/active_record_has/scope.rb', line 5 def relation @relation end |
Instance Method Details
#exists ⇒ Object
52 53 54 |
# File 'lib/active_record_has/scope.rb', line 52 def exists scope.arel.exists end |
#scope ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/active_record_has/scope.rb', line 39 def scope raise Error unless reflection @scope ||= begin result = reflection.foreign_scope result = result.instance_eval(&block) if block result = result.merge(relation) if relation result = result.where(**) if .present? result end end |