customize 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.7
1
+ 0.0.8
@@ -11,13 +11,33 @@ class Customize::InheritNode < ActiveRecord::Base
11
11
  }
12
12
  }
13
13
 
14
+ validates_presence_of :node_type, :node_id
15
+
14
16
  scope :by_type, lambda { |type| where(:node_type=>type) }
15
17
 
16
18
  after_destroy { |object|
17
19
  object.class.where("right > :right and node_type = :type",
18
20
  :right=>object.right, :type=>object.node_type).update_all("right = right -2")
21
+ object.class.where("left > :right and node_type = :type",
22
+ :right=>object.right, :type=>object.node_type).update_all("left = left -2")
23
+ }
24
+ before_create { |object|
25
+ if object.parent_id.nil?
26
+ left = self.class.where(:node_type=>object.node_type).maximum(:right) || -1
27
+ left = left + 1
28
+ object.left = left
29
+ object.right = left + 1
30
+ else
31
+ object.left = object.parent_node.right
32
+ object.right = object.parent_node.right + 1
33
+ object.class.where("right >= :right and node_type = :type",
34
+ :right=>object.left, :type=>object.node_type).update_all("right = right + 2")
35
+ object.class.where("left >= :right and node_type = :type",
36
+ :right=>object.right, :type=>object.node_type).update_all("left = left + 2")
37
+ end
19
38
  }
20
39
 
40
+
21
41
  def leaf?
22
42
  children.empty?
23
43
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "customize"
8
- s.version = "0.0.7"
8
+ s.version = "0.0.8"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ryan Wong"]
12
- s.date = "2012-05-07"
12
+ s.date = "2012-05-08"
13
13
  s.description = "\n\t\teasy customize your domain model, including:\n\t\tcharacterize;\n\t\tinherit;\n\t\tformula\n\t"
14
14
  s.email = "ryan@idolgo.com"
15
15
  s.extra_rdoc_files = [
@@ -42,6 +42,7 @@ Gem::Specification.new do |s|
42
42
  "lib/generators/customize/templates/migration.rb",
43
43
  "spec/customize/characterize_spec.rb",
44
44
  "spec/customize/formulaic_spec.rb",
45
+ "spec/customize/inherit_node_spec.rb",
45
46
  "spec/customize/inherited_spec.rb",
46
47
  "spec/customize_spec.rb",
47
48
  "spec/dummy/Rakefile",
@@ -28,7 +28,7 @@ module Customize
28
28
  }
29
29
 
30
30
  base.after_create { |object|
31
- object.create_inherit_node :left=>0, :right=>1
31
+ object.create_inherit_node
32
32
  }
33
33
 
34
34
  base.delegate :leaf?, :to=>:inherit_node
@@ -81,12 +81,8 @@ module Customize
81
81
  raise 'should not inherit descents' if self.descent_ids.include? parent.id
82
82
  raise 'should not inherit parent' if self.parent.try(:id) == parent.id
83
83
  self.class.transaction do
84
- inherit_node.parent_id = parent.inherit_node.id
85
- right = parent.inherit_node.right
86
- InheritNode.where("right >= ?", inherit_node.left).update_all("right = right+2")
87
- inherit_node.left = right
88
- inherit_node.right = right + 1
89
- inherit_node.save
84
+ inherit_node.destroy
85
+ create_inherit_node(:parent_id=>parent.inherit_node.id)
90
86
  end
91
87
  end
92
88
 
@@ -39,6 +39,8 @@ describe Customize::Characterize do
39
39
  context :model_with_inherit do
40
40
  subject {
41
41
  Customize::Character.delete_all
42
+ Product.delete_all
43
+ Customize::InheritNode.delete_all
42
44
  parent = Product.create!
43
45
  parent.characters.create! :key=>:p, :value=>1
44
46
  p = Product.create!
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+
3
+ describe Customize::InheritNode do
4
+
5
+ before :each do
6
+ described_class.delete_all
7
+ end
8
+
9
+ context :single_tree do
10
+ context :one do
11
+ let(:root) { described_class.create! :node_type=>"Node", :node_id=>1 }
12
+
13
+ it { root.left.should == (root.right-1) }
14
+ end
15
+
16
+ context :two do
17
+ before :each do
18
+ @root = described_class.create!(:node_type=>"Node", :node_id=>2)
19
+ @node = described_class.create!(:node_type=>"Node", :node_id=>3, :parent_id=>@root.id)
20
+ @root.reload
21
+ end
22
+
23
+ it :node_right do
24
+ @root.right.should == (@node.right+1)
25
+ end
26
+ it :node_left do
27
+ @root.left.should == (@node.left-1)
28
+ end
29
+
30
+ it :destroy do
31
+ @last = described_class.create!(:node_type=>"Node", :node_id=>4)
32
+ @node.destroy
33
+ @last.reload
34
+ @root.reload
35
+ @root.left.should == (@root.right - 1)
36
+ @last.left.should == (@root.right + 1)
37
+ end
38
+
39
+ end
40
+
41
+ end
42
+ context :forest do
43
+ context :lvl_one do
44
+ before :each do
45
+ @root = (1..3).collect do |i|
46
+ described_class.create :node_type=>"Node", :node_id=>i
47
+ end
48
+ @node = described_class.create :node_type=>"Node", :node_id=>4, :parent_id=>@root.first.id
49
+ end
50
+
51
+ let(:root){described_class.where(:node_type=>"Node", :parent_id=>nil)}
52
+ let(:node){described_class.find_by_node_id 4}
53
+
54
+ it :first_left do (root.first.left + 1).should == node.left end
55
+ it :first_right do
56
+ root.reload
57
+ (root.first.right - 1).should == node.right
58
+ end
59
+ it :second do (root[1].left - 1).should == root.first.right end
60
+ end
61
+ end
62
+
63
+ end
@@ -22,6 +22,8 @@ describe Customize::Inherited do
22
22
  end
23
23
 
24
24
  before :each do
25
+ Customize::InheritNode.delete_all
26
+ Product.delete_all
25
27
  @parent = Product.create!
26
28
  @child = Product.create!
27
29
  @child.inherit @parent
@@ -29,6 +31,9 @@ describe Customize::Inherited do
29
31
  end
30
32
 
31
33
  context "inherit" do
34
+ after :each do
35
+ #Customize::InheritNode.all.each do |n| pp [n.left, n.right] end
36
+ end
32
37
  it "cannot inherit different class" do
33
38
  expect {
34
39
  @child.inherit Object.new
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: customize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-07 00:00:00.000000000 Z
12
+ date: 2012-05-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -252,6 +252,7 @@ files:
252
252
  - lib/generators/customize/templates/migration.rb
253
253
  - spec/customize/characterize_spec.rb
254
254
  - spec/customize/formulaic_spec.rb
255
+ - spec/customize/inherit_node_spec.rb
255
256
  - spec/customize/inherited_spec.rb
256
257
  - spec/customize_spec.rb
257
258
  - spec/dummy/Rakefile
@@ -304,7 +305,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
304
305
  version: '0'
305
306
  segments:
306
307
  - 0
307
- hash: 562141071
308
+ hash: -683783373
308
309
  required_rubygems_version: !ruby/object:Gem::Requirement
309
310
  none: false
310
311
  requirements: