Class: ActiveList::Definition::AssociationColumn
- Inherits:
-
DataColumn
- Object
- AbstractColumn
- DataColumn
- ActiveList::Definition::AssociationColumn
- Defined in:
- lib/active_list/definition/association_column.rb
Constant Summary
Constants inherited from DataColumn
Instance Attribute Summary collapse
-
#label_method ⇒ Object
readonly
Returns the value of attribute label_method.
-
#reflection ⇒ Object
readonly
Returns the value of attribute reflection.
Attributes inherited from AbstractColumn
Instance Method Summary collapse
- #class_name ⇒ Object
-
#datum_code(record = 'record_of_the_death', child = false) ⇒ Object
Code for rows.
-
#initialize(table, name, options = {}) ⇒ AssociationColumn
constructor
A new instance of AssociationColumn.
- #record_expr(record = 'record_of_the_death') ⇒ Object
- #sort_expression ⇒ Object
Methods inherited from DataColumn
#currency_for, #datatype, #enumerize?, #exportable?, #exporting_datum_code, #header_code, #limit, #numeric?, #sortable?, #state_machine?
Methods inherited from AbstractColumn
#check_options!, #computable?, #exportable?, #header_code, #hidden?, #short_id, #sortable?, #unique_id
Constructor Details
#initialize(table, name, options = {}) ⇒ AssociationColumn
Returns a new instance of AssociationColumn.
6 7 8 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 43 44 45 46 |
# File 'lib/active_list/definition/association_column.rb', line 6 def initialize(table, name, = {}) super(table, name, ) 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 unless klass = begin @reflection.class_name.constantize rescue nil end raise StandardError, "Given reflection #{reflection_name} seems to be invalid" end columns_def = klass.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_method ⇒ Object (readonly)
Returns the value of attribute label_method.
4 5 6 |
# File 'lib/active_list/definition/association_column.rb', line 4 def label_method @label_method end |
#reflection ⇒ Object (readonly)
Returns the value of attribute reflection.
4 5 6 |
# File 'lib/active_list/definition/association_column.rb', line 4 def reflection @reflection end |
Instance Method Details
#class_name ⇒ Object
64 65 66 |
# File 'lib/active_list/definition/association_column.rb', line 64 def class_name @reflection.class_name end |
#datum_code(record = 'record_of_the_death', child = false) ⇒ Object
Code for rows
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/active_list/definition/association_column.rb', line 49 def datum_code(record = 'record_of_the_death', child = false) code = '' code = if child 'nil' # if @options[:children].is_a?(FalseClass) # code = "nil" # else # code = "#{record}.#{table.options[:children]}.#{@reflection.name}.#{@options[:children] || @label_method}" # end else "(#{record}.#{@reflection.name} ? #{record}.#{@reflection.name}.#{@label_method} : nil)" end code.c end |
#record_expr(record = 'record_of_the_death') ⇒ Object
68 69 70 |
# File 'lib/active_list/definition/association_column.rb', line 68 def record_expr(record = 'record_of_the_death') "#{record}.#{@reflection.name}" end |
#sort_expression ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/active_list/definition/association_column.rb', line 72 def sort_expression same_table_reflections = table.reflections.select { |r| r.table_name == @reflection.table_name } if same_table_reflections.size > 1 && same_table_reflections.index { |r| r.name == @reflection.name } > 0 # "#{@reflection.name.to_s.pluralize}_#{@reflection.class_name.constantize.table_name}.#{@sort_column}" "#{@reflection.name.to_s.pluralize}_#{table.model.table_name}.#{@sort_column}" else "#{@reflection.class_name.constantize.table_name}.#{@sort_column}" end end |