Sha256: 8ffa51b5e72c91f3adcc10816cf2aeca438135f7f47c9c4155b50b4d4eef8b63

Contents?: true

Size: 1.04 KB

Versions: 10

Compression:

Stored size: 1.04 KB

Contents

class AttributeValue < ActiveRecord::Base
  belongs_to :attributed_record, :polymorphic => true
  belongs_to :attribute_type

  validates_format_of :value, :with => /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/, :if => :is_date?
  after_destroy :remove_unused_types

  def is_date?
    self.attribute_type.data_type == 'Date' ? true : false
  end

  def value_as_data_type
    data_type = self.attribute_type.data_type

    case data_type
    when "Date"
      self.value.to_date
    when "Boolean"
      self.value == "true" ? true : false
    when "Int"
      self.value.to_i
    when "Float"
      self.value.to_f
    else
      self.value
    end
  end

  def value_as_date
    if self.is_date?
      self.value.to_date
    else
      raise "value is not a Date or is not properly formated"
    end
  end

  def remove_unused_types
    AttributeType.includes(:attribute_values).joins('LEFT OUTER JOIN attribute_values on attribute_values.attribute_type_id = attribute_types.id').where('attribute_values.id is null').each do |type|
      type.destroy
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
erp_tech_svcs-3.0.12 app/models/attribute_value.rb
erp_tech_svcs-3.0.11 app/models/attribute_value.rb
erp_tech_svcs-3.0.10 app/models/attribute_value.rb
erp_tech_svcs-3.0.9 app/models/attribute_value.rb
erp_tech_svcs-3.0.8 app/models/attribute_value.rb
erp_tech_svcs-3.0.7 app/models/attribute_value.rb
erp_tech_svcs-3.0.6 app/models/attribute_value.rb
erp_tech_svcs-3.0.5 app/models/attribute_value.rb
erp_tech_svcs-3.0.4 app/models/attribute_value.rb
erp_tech_svcs-3.0.3 app/models/attribute_value.rb