Sha256: b4df2efe7ffa5974be85f0c829de8bcb6cfbae8fe2bd794f5a7d23574f00966e

Contents?: true

Size: 860 Bytes

Versions: 4

Compression:

Stored size: 860 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 v.nil?
      send "#{self.object_type}_value=", v
    elsif object_type == "string" && self.object_type == "text"
      self.text_value = v
    else
      send "#{self.class.attr_column(v)}=", 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

4 entries across 4 versions & 1 rubygems

Version Path
datts_right-0.0.15 lib/datts_right/dynamic_attribute.rb
datts_right-0.0.14 lib/datts_right/dynamic_attribute.rb
datts_right-0.0.13 lib/datts_right/dynamic_attribute.rb
datts_right-0.0.12 lib/datts_right/dynamic_attribute.rb