Class: ActiveList::DataColumn

Inherits:
Column
  • Object
show all
Defined in:
lib/active-list/columns/data_column.rb

Instance Attribute Summary

Attributes inherited from Column

#id, #name, #options, #table

Instance Method Summary collapse

Methods inherited from Column

#initialize, #simple_id, #unique_id

Constructor Details

This class inherits a constructor from ActiveList::Column

Instance Method Details

#class_nameObject

Returns the class name of the used model



118
119
120
121
122
123
124
125
126
127
# File 'lib/active-list/columns/data_column.rb', line 118

def class_name
  klass = self.table.model
  if through = @options[:through]
    through = [through] unless through.is_a? Array
    for ref in through
      klass = klass.reflections[ref].class_name.constantize
    end
  end
  return klass.name
end

#datatypeObject

Returns the data type of the column if the column is in the database



91
92
93
# File 'lib/active-list/columns/data_column.rb', line 91

def datatype
  @options[:datatype] || (@column ? @column.type : nil)
end

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

Code for rows



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/active-list/columns/data_column.rb', line 46

def datum_code(record='rekord', child = false)
  code = if child and @options[:children].is_a? Symbol
           "#{record}.#{@options[:children]}"
         elsif child and @options[:children].is_a? FalseClass
           "nil"
         elsif through = @options[:through] and !child
           through = [through] unless through.is_a?(Array)
           foreign_record = record
           through.each { |x| foreign_record += '.'+x.to_s }
           "(#{foreign_record}.#{@name} rescue nil)"
         else
           "#{record}.#{@name}"
         end
  return code
end

#enumerize?Boolean

Returns:

  • (Boolean)


96
97
98
99
100
101
102
103
104
105
# File 'lib/active-list/columns/data_column.rb', line 96

def enumerize?
  if self.table.model.respond_to?(@name) and !@options[:through]
    if self.table.model.method(@name).arity.zero?
      if self.table.model.send(@name).respond_to?(:values)
        return true
      end
    end
  end
  return false
end

#exportable?Boolean

Defines if column is exportable

Returns:

  • (Boolean)


130
131
132
# File 'lib/active-list/columns/data_column.rb', line 130

def exportable?
  true
end

#exporting_datum_code(record = 'rekord', noview = false) ⇒ Object

Code for exportation



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/active-list/columns/data_column.rb', line 63

def exporting_datum_code(record='rekord', noview=false)
  datum = self.datum_code(record)
  if self.datatype == :boolean
    datum = "(#{datum} ? ::I18n.translate('list.export.true_value') : ::I18n.translate('list.export.false_value'))"
  elsif self.datatype == :date
    datum = "(#{datum}.nil? ? '' : ::I18n.localize(#{datum}))"
  elsif self.datatype == :decimal and not noview
    currency = nil
    if currency = self.options[:currency]
      currency = currency[:body] if currency.is_a?(Hash)
      currency = :currency if currency.is_a?(TrueClass)
      currency = "RECORD.#{currency}" if currency.is_a?(Symbol)
      raise Exception.new("Option :currency is not valid. Hash, Symbol or true/false") unless currency.is_a?(String)
    end
    datum = "(#{datum}.nil? ? '' : ::I18n.localize(#{datum}#{', :currency=>'+currency.gsub(/RECORD/, record) if currency}))"
  elsif @name.to_s.match(/(^|\_)currency$/) and self.datatype == :string and self.limit == 3
    datum = "(#{datum}.nil? ? '' : ::I18n.currency_label(#{datum}))"
  elsif @name==:country and  self.datatype == :string and self.limit == 2
    datum = "(#{datum}.nil? ? '' : ::I18n.translate('countries.'+#{datum}))"
  elsif @name==:language and self.datatype == :string and self.limit <= 8
    datum = "(#{datum}.nil? ? '' : ::I18n.translate('languages.'+#{datum}))"
  elsif self.enumerize?
    datum = "(#{datum}.nil? ? '' : #{datum}.text)"
  end
  return datum
end

#header_codeObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/active-list/columns/data_column.rb', line 24

def header_code
  if @options[:label].is_a? String
    "::I18n.translate('labels.#{@options[:label].strip}')"
  elsif through = @options[:through]
    through = [through] unless through.is_a? Array
    model, reflection = @table.model, nil
    for ref in through
      unless reflection.nil?
        model = reflection.class_name.constantize rescue nil
      end
      raise Exception.new("Unknown model #{reflection.class_name}") if model.nil?
      reflection = model.reflections[ref]
      raise Exception.new("Unknown reflection :#{ref} (#{through.inspect}) for the ActiveRecord: "+model.name) if reflection.nil?
    end
    "#{model.name}.human_attribute_name('#{reflection.name}')"
  else
    "#{@table.model.name}.human_attribute_name('#{@name}')"
  end
end

#limitObject

Returns the size/length of the column if the column is in the database



112
113
114
# File 'lib/active-list/columns/data_column.rb', line 112

def limit
  @column.limit if @column
end

#numeric?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/active-list/columns/data_column.rb', line 107

def numeric?
  [:decimal, :integer, :float, :numeric].include? self.datatype
end

#record_expr(record = 'record') ⇒ Object

Generate code in order to get the (foreign) record of the column



141
142
143
144
145
146
147
# File 'lib/active-list/columns/data_column.rb', line 141

def record_expr(record='record')
  if @options[:through]
    return ([record]+[@options[:through]]).flatten.join(".")
  else
    return record
  end
end

#sortable?Boolean

Check if a column is sortable

Returns:

  • (Boolean)


135
136
137
138
# File 'lib/active-list/columns/data_column.rb', line 135

def sortable?
  #not self.action? and 
  not self.options[:through] and not @column.nil?
end