Class: Scopiform::ScopeContext
- Inherits:
-
Object
- Object
- Scopiform::ScopeContext
- Defined in:
- lib/scopiform/scope_context.rb
Instance Attribute Summary collapse
-
#ancestors ⇒ Object
Returns the value of attribute ancestors.
-
#arel_table ⇒ Object
Returns the value of attribute arel_table.
-
#association ⇒ Object
Returns the value of attribute association.
-
#association_arel_table ⇒ Object
Returns the value of attribute association_arel_table.
-
#has_manys ⇒ Object
Returns the value of attribute has_manys.
-
#joins ⇒ Object
Returns the value of attribute joins.
-
#scopes ⇒ Object
Returns the value of attribute scopes.
Class Method Summary collapse
Instance Method Summary collapse
- #alias_name ⇒ Object
- #build_joins ⇒ Object
-
#initialize ⇒ ScopeContext
constructor
A new instance of ScopeContext.
- #set(arel_table) ⇒ Object
Constructor Details
#initialize ⇒ ScopeContext
Returns a new instance of ScopeContext.
19 20 21 22 23 24 |
# File 'lib/scopiform/scope_context.rb', line 19 def initialize @joins = [] @ancestors = [] @scopes = [] @has_manys = [] end |
Instance Attribute Details
#ancestors ⇒ Object
Returns the value of attribute ancestors.
3 4 5 |
# File 'lib/scopiform/scope_context.rb', line 3 def ancestors @ancestors end |
#arel_table ⇒ Object
Returns the value of attribute arel_table.
3 4 5 |
# File 'lib/scopiform/scope_context.rb', line 3 def arel_table @arel_table end |
#association ⇒ Object
Returns the value of attribute association.
3 4 5 |
# File 'lib/scopiform/scope_context.rb', line 3 def association @association end |
#association_arel_table ⇒ Object
Returns the value of attribute association_arel_table.
3 4 5 |
# File 'lib/scopiform/scope_context.rb', line 3 def association_arel_table @association_arel_table end |
#has_manys ⇒ Object
Returns the value of attribute has_manys.
3 4 5 |
# File 'lib/scopiform/scope_context.rb', line 3 def has_manys @has_manys end |
#joins ⇒ Object
Returns the value of attribute joins.
3 4 5 |
# File 'lib/scopiform/scope_context.rb', line 3 def joins @joins end |
#scopes ⇒ Object
Returns the value of attribute scopes.
3 4 5 |
# File 'lib/scopiform/scope_context.rb', line 3 def scopes @scopes end |
Class Method Details
.from(ctx) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/scopiform/scope_context.rb', line 5 def self.from(ctx) created = new if ctx created.set(ctx.arel_table) created.association = ctx.association created.association_arel_table = ctx.association_arel_table created.joins = ctx.joins created.ancestors = [*ctx.ancestors] end created end |
Instance Method Details
#alias_name ⇒ Object
63 64 65 |
# File 'lib/scopiform/scope_context.rb', line 63 def alias_name ancestors.join('_').downcase end |
#build_joins ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/scopiform/scope_context.rb', line 32 def build_joins loop do if association.through_reflection source_reflection_name = association.source_reflection_name self.association = association.through_reflection end unless association.has_one? || association.belongs_to? has_manys << association.name end ancestors << association.name.to_s.pluralize self.association_arel_table = association.klass.arel_table.alias(alias_name) joins << create_join if association.scope.present? && association.scope.arity.zero? association.klass.scopiform_ctx = ScopeContext.from(self).set(association_arel_table) scopes << association.klass.instance_exec(&association.scope) association.klass.scopiform_ctx = nil end break if source_reflection_name.blank? self.association = association.klass.reflect_on_association(source_reflection_name) self.arel_table = association_arel_table end joins end |
#set(arel_table) ⇒ Object
26 27 28 29 30 |
# File 'lib/scopiform/scope_context.rb', line 26 def set(arel_table) @arel_table = arel_table self end |