Sha256: 2d799778b212a297452ed8f2b663416f938a75f7d39a3bbfb0af5952ec27389d

Contents?: true

Size: 1.16 KB

Versions: 7

Compression:

Stored size: 1.16 KB

Contents

class Datt < 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)

    # only allow object_type changes if this is NOT a new record
    # and it's text to string or vice versa
    interchangeable_types = %w(text string)
    #puts "It's currently a #{self.object_type}, and we wanna make it a #{object_type}"
    if !new_record? && interchangeable_types.include?(self.object_type) && interchangeable_types.include?(object_type) && self.object_type != object_type
      #puts "It's not new, and we're setting it to #{object_type}"
      self.object_type = object_type
    end
    send("#{self.class.attr_column(v)}=", v)
  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

7 entries across 7 versions & 1 rubygems

Version Path
datts_right-0.0.7 lib/datts_right/datt.rb
datts_right-0.0.6 lib/datts_right/datt.rb
datts_right-0.0.5 lib/datts_right/datt.rb
datts_right-0.0.4 lib/datts_right/datt.rb
datts_right-0.0.3 lib/datts_right/datt.rb
datts_right-0.0.2 lib/datts_right/datt.rb
datts_right-0.0.1 lib/datts_right/datt.rb