Class: ActiveList::Definition::AssociationColumn

Inherits:
DataColumn show all
Defined in:
lib/active_list/definition/association_column.rb

Constant Summary

Constants inherited from DataColumn

DataColumn::LABELS_COLUMNS

Instance Attribute Summary collapse

Attributes inherited from AbstractColumn

#id, #name, #options, #table

Instance Method Summary collapse

Methods inherited from DataColumn

#datatype, #enumerize?, #exportable?, #exporting_datum_code, #header_code, #limit, #numeric?, #sortable?

Methods inherited from AbstractColumn

#check_options!, #exportable?, #header_code, #hidden?, #short_id, #sortable?, #unique_id

Constructor Details

#initialize(table, name, options = {}) ⇒ AssociationColumn

Returns a new instance of AssociationColumn.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/active_list/definition/association_column.rb', line 9

def initialize(table, name, options = {})
  super(table, name, options)
  unless @options[:through]
    raise ArgumentError, "Option :through must be given"
  end
  reflection_name = @options.delete(:through).to_sym
  if @reflection = @table.model.reflect_on_association(reflection_name)
    if @reflection.macro == :belongs_to
      # Do some stuff
    elsif @reflection.macro == :has_one
      # Do some stuff
    else
      raise ArgumentError, "Only belongs_to are usable. Can't handle: #{reflection.macro} :#{reflection.name}."
    end
  else
    raise UnknownReflection, "Reflection #{reflection_name} cannot be found for #{table.model.name}."
  end
  columns_def = @reflection.class_name.constantize.columns_hash.keys.map(&:to_sym)
  unless @label_method = @options.delete(:label_method)
    columns = columns_def + @reflection.class_name.constantize.instance_methods.map(&:to_sym)
    unless @label_method = LABELS_COLUMNS.detect{|m| columns.include?(m)}
      raise ArgumentError, ":label_method option must be given for association #{name}. (#{columns.inspect})"
    end
  end
  unless @sort_column = @options.delete(:sort)
    if columns_def.include?(@label_method)
      @sort_column = @label_method
    else
      unless @sort_column = LABELS_COLUMNS.detect{|m| columns_def.include?(m)}
        @sort_column = :id
      end
    end
  end
end

Instance Attribute Details

#label_methodObject (readonly)

Returns the value of attribute label_method.



7
8
9
# File 'lib/active_list/definition/association_column.rb', line 7

def label_method
  @label_method
end

#reflectionObject (readonly)

Returns the value of attribute reflection.



7
8
9
# File 'lib/active_list/definition/association_column.rb', line 7

def reflection
  @reflection
end

Instance Method Details

#class_nameObject



61
62
63
# File 'lib/active_list/definition/association_column.rb', line 61

def class_name
  return @reflection.class_name
end

#datum_code(record = 'record_of_the_death', child = false) ⇒ Object

Code for rows



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/active_list/definition/association_column.rb', line 46

def datum_code(record = 'record_of_the_death', child = false)
  code = ""
  if child
    code = "nil"
    # if @options[:children].is_a?(FalseClass)
    #   code = "nil"
    # else
    #   code = "#{record}.#{table.options[:children]}.#{@reflection.name}.#{@options[:children] || @label_method}"
    # end
  else
    code = "(#{record}.#{@reflection.name} ? #{record}.#{@reflection.name}.#{@label_method} : nil)"
  end
  return code.c
end

#record_expr(record = 'record_of_the_death') ⇒ Object



65
66
67
# File 'lib/active_list/definition/association_column.rb', line 65

def record_expr(record = 'record_of_the_death')
  return "#{record}.#{@reflection.name}"
end

#sort_expressionObject



69
70
71
72
73
74
75
# File 'lib/active_list/definition/association_column.rb', line 69

def sort_expression
  if table.reflections.select{|r| r.table_name == @reflection.table_name}.size > 1
    "#{@reflection.name.to_s.pluralize}_#{@reflection.class_name.constantize.table_name}.#{@sort_column}"
  else
    "#{@reflection.class_name.constantize.table_name}.#{@sort_column}"
  end
end