categoryz3 0.3 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,6 +8,9 @@ module Categoryz3
8
8
 
9
9
  scope :parent_categories, -> { where(parent_id: nil) }
10
10
  attr_accessible :name, :parent, :parent_id
11
+
12
+ before_update :mark_as_dirty_if_parent_changed
13
+ after_update :update_children_if_parent_changed
11
14
 
12
15
  # Public: Returns the full categories path from the root category until this category
13
16
  #
@@ -17,13 +20,29 @@ module Categoryz3
17
20
  # #=> [category, subcategory1, subcategory2, subcategory3]
18
21
  #
19
22
  def path
20
- return @path_array if @path_array
21
- parent_category = self
22
- @path_array = [parent_category]
23
- while (parent_category = parent_category.parent) != nil do
24
- @path_array.insert(0, parent_category)
23
+ @path_array ||= parent ? [parent.path, self].flatten : [self]
24
+ end
25
+
26
+ # Public: Reprocess all items from this category
27
+ #
28
+ def reprocess_items!
29
+ @path_array = nil
30
+ self.direct_items.each { |item| item.reprocess_child_items! }
31
+ self.children.each { |category| category.reprocess_items! }
32
+ end
33
+
34
+ private
35
+
36
+ def mark_as_dirty_if_parent_changed
37
+ @dirty_parent = parent_id_changed?
38
+ end
39
+
40
+ # Private: Reprocess all items in case the parent category
41
+ # of this category has changed.
42
+ def update_children_if_parent_changed
43
+ if @dirty_parent
44
+ reprocess_items!
25
45
  end
26
- @path_array
27
46
  end
28
47
 
29
48
  end
@@ -10,20 +10,27 @@ module Categoryz3
10
10
  after_create :create_child_items
11
11
  attr_accessible :categorizable
12
12
 
13
+ # Public: Destroy the child items and recreate them
14
+ #
15
+ def reprocess_child_items!
16
+ self.child_items.destroy_all
17
+ create_child_items
18
+ end
19
+
13
20
  private
14
21
 
15
22
  # Private: Creates a child item for the category
16
23
  #
17
24
  def create_child_item_for_category(category)
18
- category.child_items.create(categorizable: self.categorizable, master_item: self)
25
+ category.child_items.find_or_create_by_categorizable_type_and_categorizable_id_and_master_item_id( self.categorizable.class.name, self.categorizable.id, self.id)
19
26
  end
20
27
 
21
28
  # Private: Create a child_item for each subcategory the category from this item have
22
29
  # Also creates a child_item for this category
23
30
  #
24
31
  def create_child_items
25
- category.path.each do |category|
26
- create_child_item_for_category category
32
+ self.category.path.each do |parent_category|
33
+ create_child_item_for_category parent_category
27
34
  end
28
35
  end
29
36
 
@@ -1,3 +1,3 @@
1
1
  module Categoryz3
2
- VERSION = "0.3"
2
+ VERSION = "0.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: categoryz3
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.3'
4
+ version: '0.4'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -131,7 +131,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
131
131
  version: '0'
132
132
  segments:
133
133
  - 0
134
- hash: 214583827626684549
134
+ hash: 2265189143857855117
135
135
  required_rubygems_version: !ruby/object:Gem::Requirement
136
136
  none: false
137
137
  requirements:
@@ -140,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
140
140
  version: '0'
141
141
  segments:
142
142
  - 0
143
- hash: 214583827626684549
143
+ hash: 2265189143857855117
144
144
  requirements: []
145
145
  rubyforge_project:
146
146
  rubygems_version: 1.8.24