orbit-basic/lib/orbit_category/categorizable.rb

50 lines
1.0 KiB
Ruby

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.write_attribute(:category_id, id)
end
def category_id=(id)
self.category = id
end
def disable?
category.disable?
end
end
end
end