2013-07-02 08:46:44 +00:00
|
|
|
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)
|
2013-07-16 03:41:22 +00:00
|
|
|
self.write_attribute(:category_id, id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def category_id=(id)
|
2013-07-19 09:09:17 +00:00
|
|
|
self.category = id
|
2013-07-02 08:46:44 +00:00
|
|
|
end
|
|
|
|
|
2013-07-04 03:26:57 +00:00
|
|
|
def disable?
|
|
|
|
category.disable?
|
|
|
|
end
|
|
|
|
|
2013-07-02 08:46:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|