Orbit/lib/orbit_category/categorizable.rb

50 lines
1.0 KiB
Ruby
Raw Permalink 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.write_attribute(:category_id, id)
end
def category_id=(id)
2013-07-19 09:09:17 +00:00
self.category = id
end
def disable?
category.disable?
end
end
end
end