Class: ComboBox::Generator::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/combo_box/generator.rb

Overview

Column = Struct.new('Column', :name, :filter, :interpolation_key, :full_name)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, name, options = {}) ⇒ Column

@@count = 0


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/combo_box/generator.rb', line 11

def initialize(model, name, options={})
  @model = model
  @name = name.to_s
  @filter = options.delete(:filter) || "%X%"
  @code = options.delete(:code)
  @interpolation_key = options.delete(:interpolation_key) || @name.gsub(/\W/, '_')
  klass = @model
  @through = (options.delete(:through) || []).collect do |reflection|
    unless klass.reflections[reflection.to_sym]
      raise Exception.new("Model #{klass.name} has no reflections #{reflection.inspect}")
    end
    klass = klass.reflections[reflection.to_sym].class_name.constantize
    reflection.to_sym
  end || []
  @column = foreign_model.columns_hash[@name]
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column


7
8
9
# File 'lib/combo_box/generator.rb', line 7

def column
  @column
end

#filterObject (readonly)

Returns the value of attribute filter


7
8
9
# File 'lib/combo_box/generator.rb', line 7

def filter
  @filter
end

#interpolation_keyObject (readonly)

Returns the value of attribute interpolation_key


7
8
9
# File 'lib/combo_box/generator.rb', line 7

def interpolation_key
  @interpolation_key
end

#nameObject (readonly)

Returns the value of attribute name


7
8
9
# File 'lib/combo_box/generator.rb', line 7

def name
  @name
end

Instance Method Details

#foreign_modelObject


54
55
56
57
58
59
60
# File 'lib/combo_box/generator.rb', line 54

def foreign_model
  model = @model
  for reflection in @through
    model = model.reflections[reflection].class_name.constantize
  end
  return model
end

#sql_nameObject


28
29
30
# File 'lib/combo_box/generator.rb', line 28

def sql_name
  return "#{foreign_model.table_name}.#{@name}"
end

#typeObject


50
51
52
# File 'lib/combo_box/generator.rb', line 50

def type
  @column.type
end

#value_code(record = 'record') ⇒ Object


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/combo_box/generator.rb', line 32

def value_code(record='record')
  code  = "("
  value = "(#{record}#{'.'+@through.join('.') unless @through.empty?}.#{name})"
  @through.each_index do |i|
    code << "#{record}.#{@through[0..i].join('.')}.nil? ? '' : "
  end
  code << if @code
            @code.gsub(/RECORD/, record).gsub(/DATUM/, value)
          elsif[:date, :datetime, :timestamp].include? self.type
            "(#{value}.nil? ? '' : ::I18n.localize(#{value}))"
          else
            value
          end
  code << ")"
  code << ".to_s" unless name.to_s == "count"
  return code
end