Module: Inkwell::ActsAsInkwellCategory::InstanceMethods

Includes:
Common, Constants
Defined in:
lib/acts_as_inkwell_category/base.rb

Instance Method Summary collapse

Methods included from Common

#category_class, #check_post, #check_user, #community_class, #community_id_attr, #get_class_for_item_type, #get_item_type, #get_owner_type, #post_class, #user_class, #user_id_attr

Instance Method Details

#add_item(options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/acts_as_inkwell_category/base.rb', line 51

def add_item(options = {})
  options.symbolize_keys!
  item = options[:item]
  item_type = get_item_type item
  owner = options[:owner]
  owner_type = get_owner_type owner
  blog_item = Inkwell::BlogItem.where(:owner_id => owner.id, :owner_type => owner_type, :item_id => item.id, :item_type => item_type).first
  ::Inkwell::BlogItemCategory.create :blog_item_id => blog_item.id, :category_id => self.id,
                                     :blog_item_created_at => blog_item.created_at, :item_id => blog_item.item_id, :item_type => blog_item.item_type
end

#after_create_processingObject



81
82
83
84
85
86
87
88
89
90
# File 'lib/acts_as_inkwell_category/base.rb', line 81

def after_create_processing
  parent_ids = ActiveSupport::JSON.decode self.parent_ids
  parent_ids.each do |parent_id|
    parent = category_class.find parent_id
    his_child_ids = ActiveSupport::JSON.decode parent.child_ids
    his_child_ids << self.id
    parent.child_ids = ActiveSupport::JSON.encode his_child_ids
    parent.save
  end
end

#before_create_processingObject



72
73
74
75
76
77
78
79
# File 'lib/acts_as_inkwell_category/base.rb', line 72

def before_create_processing
  if self.parent_category_id
    parent = category_class.find self.parent_category_id
    his_parent_ids = ActiveSupport::JSON.decode parent.parent_ids
    parent_ids = his_parent_ids + [parent.id]
    self.parent_ids = ActiveSupport::JSON.encode parent_ids
  end
end

#before_destroy_processingObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/acts_as_inkwell_category/base.rb', line 92

def before_destroy_processing
  child_ids = ActiveSupport::JSON.decode self.child_ids
  ids_to_delete_for_parent = child_ids + [self.id]
  category_class.delete_all :id => child_ids unless child_ids.empty?

  parent_ids = ActiveSupport::JSON.decode self.parent_ids
  parent_ids.each do |parent_id|
    parent = category_class.find parent_id
    his_child_ids = ActiveSupport::JSON.decode parent.child_ids
    his_child_ids -= ids_to_delete_for_parent
    parent.child_ids = ActiveSupport::JSON.encode his_child_ids
    parent.save
  end

  ::Inkwell::BlogItemCategory.delete_all :category_id => ids_to_delete_for_parent

end

#remove_item(options = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/acts_as_inkwell_category/base.rb', line 62

def remove_item(options = {})
  options.symbolize_keys!
  item = options[:item]
  item_type = get_item_type item
  owner = options[:owner]
  owner_type = get_owner_type owner
  blog_item = Inkwell::BlogItem.where(:owner_id => owner.id, :owner_type => owner_type, :item_id => item.id, :item_type => item_type).first
  ::Inkwell::BlogItemCategory.delete_all :blog_item_id => blog_item.id, :category_id => self.id
end