class ModelBase::ColumnAttribute

Constants

LOCALIZED_TYPES

Attributes

column[R]
linkable[RW]
linkable?[RW]
model[R]
reference[R]
title[W]

Public Class Methods

new(model, name, type, column: nil, reference: nil, index_type: false, title: false, attr_options: {}) click to toggle source
Calls superclass method
# File lib/model_base/column_attribute.rb, line 13
def initialize(model, name, type, column: nil, reference: nil, index_type: false, title: false, attr_options: {})
  super(name, type, index_type, attr_options)
  @model = model
  @reference = reference
  @column = column
  @title = !!title
end

Public Instance Methods

assert_select_exp() click to toggle source
# File lib/model_base/column_attribute.rb, line 121
def assert_select_exp
  model_name = model.full_resource_name
  case type
  when :datetime, :timestamp, :time
    "assert_select_datetime_field :#{model_name}, :#{name}"
  else
    "assert_select '#{ input_type }##{ model_name }_#{ name }[name=?]', '#{ model_name }[#{ name }]'"
  end
end
base_sample_value() click to toggle source
# File lib/model_base/column_attribute.rb, line 102
def base_sample_value
  name.split('').map(&:ord).sum
end
enumerized?() click to toggle source
# File lib/model_base/column_attribute.rb, line 37
def enumerized?
  model.model_class.respond_to?(name) &&
    defined?(Enumerize) && model.model_class.send(name).is_a?(Enumerize::Attribute)
end
input_type() click to toggle source
Calls superclass method
# File lib/model_base/column_attribute.rb, line 47
def input_type
  select_renderer ? :select : super
end
new_attribute_exp() click to toggle source
# File lib/model_base/column_attribute.rb, line 144
def new_attribute_exp
  if enumerized?
    '%s.%s.values[%d]' % [model.name, name, 1] # 2nd value
  elsif type == :boolean
    "valid_parameters[:#{name}].!"
  else
    "valid_parameters[:#{name}].succ"
  end
end
ref_model() click to toggle source
# File lib/model_base/column_attribute.rb, line 26
def ref_model
  unless defined?(@ref_model)
    @ref_model = reference.nil? ? nil : ModelBase::MetaModel.new(reference.class_name)
  end
  @ref_model
end
required?() click to toggle source
# File lib/model_base/column_attribute.rb, line 33
def required?
  column ? !column.null : false
end
sample_string_exp(idx = 1) click to toggle source
# File lib/model_base/column_attribute.rb, line 131
def sample_string_exp(idx = 1)
  case type
  when :datetime, :timestamp, :time
    'localize(Time.zone.parse(\'%s\'))' % sample_value(idx)
  else
    if !title? && ref_model && !ref_model.title_column
      "#{ref_model.full_resource_name}.title"
    else
      "'%s'" % sample_value(idx)
    end
  end
end
sample_time(idx = 1) click to toggle source
# File lib/model_base/column_attribute.rb, line 106
def sample_time(idx = 1)
  ModelBase.base_time +
    model.sample_value.hours +
    (base_sample_value.minutes * 10 * idx)
end
sample_value(idx = 1, context: nil) { |: 1| ... } click to toggle source
# File lib/model_base/column_attribute.rb, line 64
def sample_value(idx = 1, context: nil)
  if name == 'id'
    idx
  elsif name == 'email' && type == :string
    'user1@example.com'
  elsif title?
    model.full_resource_name + idx.to_s
  elsif ref_model
    if tc = ref_model.title_column
      tc.sample_value(idx)
    else
      block_given? ? yield : 1
    end
  elsif enumerized?
    enum = model.model_class.send(name)
    r = enum.values.first
    context == :factory ? r.to_sym : r.text
  else
    case type
      when :integer                     then idx
      when :float                       then idx + 0.5
      when :decimal                     then "#{idx}.99"
      when :datetime, :timestamp, :time then sample_time(idx).strftime('%F %T %:z')
      when :date                        then sample_time(idx).to_date.to_s(:db)
      when :string                      then
        case name
        when 'type' then ""
        else "#{model.full_resource_name}_#{name}_#{idx}"
        end
      when :text                        then "#{model.full_resource_name}_#{name}_#{idx}"
      when :boolean                     then false
      when :references, :belongs_to     then nil
      else
        ""
    end
  end
end
sample_value_regexp_exp(idx = 1) click to toggle source
# File lib/model_base/column_attribute.rb, line 112
def sample_value_regexp_exp(idx = 1)
  case type
  when :datetime, :timestamp, :time
    "localized_time_re('%s')" % sample_value(idx)
  else
    '/%s/' % sample_value(idx)
  end
end
select_renderer() click to toggle source
# File lib/model_base/column_attribute.rb, line 42
def select_renderer
  ref_model ? ReferenceSelectRenderer.new(self) :
    enumerized? ? EnumerizedSelectRenderer.new(self) : nil
end
single_sample_only?() click to toggle source
# File lib/model_base/column_attribute.rb, line 56
def single_sample_only?
  ref_model || enumerized? ||
    case type
    when :boolean, :datetime, :timestamp, :time, :date then true
    else false
    end
end
title?() click to toggle source
# File lib/model_base/column_attribute.rb, line 21
def title?
  @title
end
to_be_localized?() click to toggle source
# File lib/model_base/column_attribute.rb, line 52
def to_be_localized?
  LOCALIZED_TYPES.include?(type)
end