Class: Releaf::Builders::AssociationReflector
- Inherits:
-
Object
- Object
- Releaf::Builders::AssociationReflector
- Defined in:
- app/builders/releaf/builders/association_reflector.rb
Instance Attribute Summary collapse
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#reflection ⇒ Object
Returns the value of attribute reflection.
-
#sortable_cache ⇒ Object
Returns the value of attribute sortable_cache.
-
#sortable_column_name ⇒ Object
Returns the value of attribute sortable_column_name.
Instance Method Summary collapse
- #actual_order_clause ⇒ Object
- #destroyable? ⇒ Boolean
- #expected_order_clause ⇒ Object
- #extract_order_clause(relation) ⇒ Object
-
#initialize(reflection, fields, sortable_column_name) ⇒ AssociationReflector
constructor
A new instance of AssociationReflector.
- #sortable? ⇒ Boolean
- #value_as_sql(value) ⇒ Object
Constructor Details
#initialize(reflection, fields, sortable_column_name) ⇒ AssociationReflector
Returns a new instance of AssociationReflector.
6 7 8 9 10 |
# File 'app/builders/releaf/builders/association_reflector.rb', line 6 def initialize(reflection, fields, sortable_column_name) self.reflection = reflection self.fields = fields self.sortable_column_name = sortable_column_name.to_sym end |
Instance Attribute Details
#fields ⇒ Object
Returns the value of attribute fields.
4 5 6 |
# File 'app/builders/releaf/builders/association_reflector.rb', line 4 def fields @fields end |
#reflection ⇒ Object
Returns the value of attribute reflection.
4 5 6 |
# File 'app/builders/releaf/builders/association_reflector.rb', line 4 def reflection @reflection end |
#sortable_cache ⇒ Object
Returns the value of attribute sortable_cache.
4 5 6 |
# File 'app/builders/releaf/builders/association_reflector.rb', line 4 def sortable_cache @sortable_cache end |
#sortable_column_name ⇒ Object
Returns the value of attribute sortable_column_name.
4 5 6 |
# File 'app/builders/releaf/builders/association_reflector.rb', line 4 def sortable_column_name @sortable_column_name end |
Instance Method Details
#actual_order_clause ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'app/builders/releaf/builders/association_reflector.rb', line 32 def actual_order_clause relation = reflection.klass.all if reflection.scope relation = relation.instance_exec(reflection.active_record, &reflection.scope) end extract_order_clause(relation) end |
#destroyable? ⇒ Boolean
20 21 22 23 24 25 26 27 28 29 30 |
# File 'app/builders/releaf/builders/association_reflector.rb', line 20 def destroyable? if @destroyable.nil? @destroyable = reflection .active_record . .fetch(reflection.name, {}) .fetch(:allow_destroy, false) end @destroyable end |
#expected_order_clause ⇒ Object
42 43 44 45 |
# File 'app/builders/releaf/builders/association_reflector.rb', line 42 def expected_order_clause relation = reflection.klass.all.order(sortable_column_name) extract_order_clause(relation) end |
#extract_order_clause(relation) ⇒ Object
47 48 49 |
# File 'app/builders/releaf/builders/association_reflector.rb', line 47 def extract_order_clause(relation) relation.order_values.map{|value| value_as_sql(value) }.join(", ") end |
#sortable? ⇒ Boolean
12 13 14 15 16 17 18 |
# File 'app/builders/releaf/builders/association_reflector.rb', line 12 def sortable? if @sortable.nil? @sortable = (expected_order_clause == actual_order_clause) end @sortable end |
#value_as_sql(value) ⇒ Object
51 52 53 54 55 56 57 |
# File 'app/builders/releaf/builders/association_reflector.rb', line 51 def value_as_sql(value) if value.respond_to?(:to_sql) value.to_sql else value end end |