Sha256: b45957eb50599f13e30316130e424e10de6aae80c33ecae58fdb8f33333f0f4e

Contents?: true

Size: 1.08 KB

Versions: 10

Compression:

Stored size: 1.08 KB

Contents

class AttributeValue < ActiveRecord::Base
  attr_protected :created_at, :updated_at

  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-4.2.0 app/models/attribute_value.rb
erp_tech_svcs-4.0.0 app/models/attribute_value.rb
erp_tech_svcs-3.1.8 app/models/attribute_value.rb
erp_tech_svcs-3.1.7 app/models/attribute_value.rb
erp_tech_svcs-3.1.5 app/models/attribute_value.rb
erp_tech_svcs-3.1.4 app/models/attribute_value.rb
erp_tech_svcs-3.1.3 app/models/attribute_value.rb
erp_tech_svcs-3.1.2 app/models/attribute_value.rb
erp_tech_svcs-3.1.1 app/models/attribute_value.rb
erp_tech_svcs-3.1.0 app/models/attribute_value.rb