Class: Prato::Types::AssociationColumn

Inherits:
Object
  • Object
show all
Defined in:
lib/prato/types/association_column.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(accessor, format: nil, filter: nil) ⇒ AssociationColumn

Returns a new instance of AssociationColumn.



8
9
10
11
12
13
# File 'lib/prato/types/association_column.rb', line 8

def initialize(accessor, format: nil, filter: nil)
  @association_path = accessor[0..-2].map(&:to_sym).freeze
  @attribute_name = accessor[-1].to_sym
  @format = format
  @filter = filter
end

Instance Attribute Details

#association_pathObject (readonly)

Returns the value of attribute association_path.



6
7
8
# File 'lib/prato/types/association_column.rb', line 6

def association_path
  @association_path
end

#filterObject (readonly)

Returns the value of attribute filter.



6
7
8
# File 'lib/prato/types/association_column.rb', line 6

def filter
  @filter
end

#formatObject (readonly)

Returns the value of attribute format.



6
7
8
# File 'lib/prato/types/association_column.rb', line 6

def format
  @format
end

Instance Method Details

#extract_value(record, _ruby_data) ⇒ Object



31
32
33
34
# File 'lib/prato/types/association_column.rb', line 31

def extract_value(record, _ruby_data)
  target = @association_path.reduce(record) { |obj, assoc| obj&.public_send(assoc) }
  target&.[](@attribute_name)
end

#resolve_arel!(base_model, _display_id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/prato/types/association_column.rb', line 15

def resolve_arel!(base_model, _display_id)
  current_model = base_model

  @association_path.each do |assoc_name|
    reflection = current_model.reflect_on_association(assoc_name)
    raise ArgumentError, "Unknown association '#{assoc_name}' on #{current_model}" unless reflection

    current_model = reflection.klass
  end
end

#sql_node_for(scope) ⇒ Object



26
27
28
29
# File 'lib/prato/types/association_column.rb', line 26

def sql_node_for(scope)
  table = Internal::SqlSupport.table_for(scope, @association_path)
  table[@attribute_name]
end