Sha256: 54ce5ea16a3f52598be504a57de8118129ab4be15bc935f9b28ebd59ce5969c3
Contents?: true
Size: 1.08 KB
Versions: 4
Compression:
Stored size: 1.08 KB
Contents
class Dynomite::Item module Sti extend ActiveSupport::Concern included do class_attribute :inheritance_field_name end class_methods do def enable_sti(field_name='type') class_eval <<-RUBY, __FILE__, __LINE__ + 1 def self.inherited(subclass) field :#{field_name} # IE: field_name :type subclass.table_name(sti_base_table_name) # IE: subclass: Car base_table: vehicles subclass.inheritance_field_name = :#{field_name} before_save :set_type super end RUBY end alias inheritance_field enable_sti def sti_base_table_name klass = self table_name = nil until klass.abstract? # IE: ApplicationItem table_name = klass.name.pluralize.gsub('::','_').underscore # vehicles klass = klass.superclass end table_name end def sti_enabled? inheritance_field_name.present? end end def set_type self[self.class.inheritance_field_name] = self.class.name end end end
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
dynomite-2.0.3 | lib/dynomite/item/sti.rb |
dynomite-2.0.2 | lib/dynomite/item/sti.rb |
dynomite-2.0.1 | lib/dynomite/item/sti.rb |
dynomite-2.0.0 | lib/dynomite/item/sti.rb |