Sha256: 26315120f26640db1443e6b92ae4c2db03c270c97979aeffd8f2b49a5e738500

Contents?: true

Size: 965 Bytes

Versions: 2

Compression:

Stored size: 965 Bytes

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?
  before_destroy :destroy_attribute_types_without_values

  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 destroy_attribute_types_without_values
    self.attribute_type.destroy unless self.attribute_type.attribute_values.count > 1
  end
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
erp_tech_svcs-3.0.2 app/models/attribute_value.rb
erp_tech_svcs-3.0.1 app/models/attribute_value.rb