Sha256: c3f38ffb812a03bae599f39c196ca4bd64a793af3815cf4b4bb21edfa8240048

Contents?: true

Size: 840 Bytes

Versions: 1

Compression:

Stored size: 840 Bytes

Contents

class DynamicAttribute < ActiveRecord::Base
  # TODO
  # Set table name from configuration
  belongs_to :attributable, :polymorphic => true

  def value
    send("#{object_type}_value")
  end

  def value=(v)
    object_type = self.class.attr_type?(v)

    if self.object_type == object_type # has to be the same kind of object
      send "#{self.class.attr_column(v)}=", v
    elsif v.nil?
      send "#{self.object_type}_value=", v
    end
  end

  def self.attr_type?(v)
    object_type = if v.is_a?(Integer)
      "integer"
    elsif v.is_a?(Float)
      "float"
    elsif v.is_a?(TrueClass) || v.is_a?(FalseClass)
      "boolean"
    elsif v.is_a?(String)
      v.size < 255 ? "string" : "text"
    end
  end

  # Returns what column this kind of data should be in
  def self.attr_column(v)
    "#{self.attr_type?(v)}_value"
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
datts_right-0.0.11 lib/datts_right/dynamic_attribute.rb