Sha256: 6472a62fca2a1cc8f63ca2f55a2b42033307a739dfaa390cbd11a43f3375f83a
Contents?: true
Size: 1.32 KB
Versions: 2
Compression:
Stored size: 1.32 KB
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) # 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 "#{attr_key} is 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 "#{attr_key} is not new, and we're setting object_type to #{object_type}" self.object_type = object_type send "#{self.class.attr_column(v)}=", v elsif self.object_type == object_type # has to be the same kind of object 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
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
datts_right-0.0.10 | lib/datts_right/dynamic_attribute.rb |
datts_right-0.0.9 | lib/datts_right/dynamic_attribute.rb |