Class: Releaf::Builders::AssociationReflector

Inherits:
Object
  • Object
show all
Defined in:
app/builders/releaf/builders/association_reflector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#fieldsObject

Returns the value of attribute fields


4
5
6
# File 'app/builders/releaf/builders/association_reflector.rb', line 4

def fields
  @fields
end

#reflectionObject

Returns the value of attribute reflection


4
5
6
# File 'app/builders/releaf/builders/association_reflector.rb', line 4

def reflection
  @reflection
end

#sortable_cacheObject

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_nameObject

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_clauseObject


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

Returns:

  • (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
      .nested_attributes_options
      .fetch(reflection.name, {})
      .fetch(:allow_destroy, false)
  end

  @destroyable
end

#expected_order_clauseObject


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

Returns:

  • (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