Orbit/lib/orbit_category/categorizable.rb

42 lines
914 B
Ruby
Raw Normal View History

module OrbitCategory
module Categorizable
def self.included(base)
base.extend ClassMethods
base.class_eval 'categorizable'
end
module ClassMethods
def categorizable
class_eval do
field :category_id, type: BSON::ObjectId
has_one :buffer_category, as: :categorizable, autosave: true, dependent: :destroy
accepts_nested_attributes_for :buffer_category, allow_destroy: true
end
send :include, InstanceMethods
end
end
module InstanceMethods
def remove_id(id)
self.update_attribute(:category_id, id)
end
def category
self.buffer_category.category if self.buffer_category
end
def category=(id)
self.buffer_category.destroy if self.buffer_category
self.build_buffer_category(category_id: id)
self.category_id = id
end
end
end
end