customize 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.6
1
+ 0.0.7
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "customize"
8
- s.version = "0.0.6"
8
+ s.version = "0.0.7"
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-04"
12
+ s.date = "2012-05-07"
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 = [
@@ -46,16 +46,20 @@ module Customize
46
46
  joins(:inherit_node).where("parent_id is null")
47
47
  end
48
48
 
49
- def type_tree
49
+ def type_tree node = nil
50
50
  roots = root
51
- converter = proc {|items|
52
- out = items.collect { |item|
53
- {:id=>item.id,:label=>item.label,:inherit_node_id=>item.inherit_node.id, :children=>converter.call(item.children)}
51
+ converter = proc {|items, node|
52
+ out = items.select { |item|
53
+ node.nil? || (item.inherit_node.id != node.id &&
54
+ (node.parent_node.nil? || item.inherit_node.id != node.parent_node.id))
55
+ }.collect { |item|
56
+ {:id=>item.id,:label=>item.label,:inherit_node_id=>item.inherit_node.id, :children=>converter.call(item.children, node)}
54
57
  }
55
58
  }
56
59
 
57
- converter.call(roots)
60
+ converter.call(roots, node)
58
61
  end
62
+
59
63
  end
60
64
 
61
65
  def parent
@@ -74,6 +78,8 @@ module Customize
74
78
  raise 'should be save first' if self.new_record?
75
79
  raise 'should be same class' if self.class != parent.class
76
80
  raise 'should not be self' if self.id == parent.id
81
+ raise 'should not inherit descents' if self.descent_ids.include? parent.id
82
+ raise 'should not inherit parent' if self.parent.try(:id) == parent.id
77
83
  self.class.transaction do
78
84
  inherit_node.parent_id = parent.inherit_node.id
79
85
  right = parent.inherit_node.right
@@ -90,10 +96,10 @@ module Customize
90
96
  end
91
97
 
92
98
  def inherit_association name, options={:include=>false}
93
- association_table_name = association(name).aliased_table_name
94
-
95
- r = ascents(options).joins(name).select("#{association_table_name}.*").uniq
96
- r.map(&name).flatten
99
+ a = association(name)
100
+ sql = ascents(options).joins(name).select("#{a.aliased_table_name}.#{a.klass.primary_key}").to_sql
101
+ a.klass.from("(#{sql}) as subquery, #{a.klass.table_name}").
102
+ where("#{a.klass.table_name}.#{a.klass.primary_key} = subquery.#{a.klass.primary_key}")
97
103
  end
98
104
 
99
105
  def descents
@@ -28,60 +28,93 @@ describe Customize::Inherited do
28
28
  @parent.reload
29
29
  end
30
30
 
31
- it "cannot inherit different class" do
32
- expect {
33
- @child.inherit Object.new
34
- }.to raise_error
35
- end
36
-
37
- it "have method ascent_ids" do
38
- @child.ascent_ids.should_not be_nil
39
- end
40
-
41
- it "child should have 1 ascent" do
42
- @child.ascent_ids.should have(1).items
43
- end
44
-
45
- it "parent should have 1 descent" do
46
- @parent.descent_ids.should have(1).items
47
- end
31
+ context "inherit" do
32
+ it "cannot inherit different class" do
33
+ expect {
34
+ @child.inherit Object.new
35
+ }.to raise_error
36
+ end
37
+
38
+ it "have method ascent_ids" do
39
+ @child.ascent_ids.should_not be_nil
40
+ end
41
+
42
+ it "child should have 1 ascent" do
43
+ @child.ascent_ids.should have(1).items
44
+ end
45
+
46
+ it "parent should have 1 descent" do
47
+ @parent.descent_ids.should have(1).items
48
+ end
49
+
50
+ it "should support 2 levels inherit" do
51
+ c2 = Product.create!
52
+ c2.inherit @child
53
+ c2.ascent_ids.should have(2).items
54
+ @child.ascent_ids.should have(1).items
55
+ end
56
+
57
+ it "root has 1 item" do
58
+ Product.root.should have(1).items
59
+ end
60
+
61
+ it "child should have ascents" do
62
+ @child.ascents.should have(1).items
63
+ end
64
+
65
+ it "child is leaf" do
66
+ @child.leaf?.should be_true
67
+ end
68
+
69
+ it "parent is not leaf" do
70
+ @parent.leaf?.should_not be_true
71
+ end
72
+
73
+ it "should not inherit self" do
74
+ expect {
75
+ @parent.inherit @parent
76
+ }.to raise_error
77
+ end
48
78
 
49
- it "should support 2 levels inherit" do
50
- c2 = Product.create!
51
- c2.inherit @child
52
- c2.ascent_ids.should have(2).items
53
- @child.ascent_ids.should have(1).items
54
- end
79
+ it "should not inherit parent" do
80
+ expect {
81
+ @child.inherit @parent
82
+ }.to raise_error
83
+ end
55
84
 
56
- it "root has 1 item" do
57
- Product.root.should have(1).items
58
- end
85
+ it "should not inherit child" do
86
+ expect {
87
+ @parent.inherit @child
88
+ }.to raise_error
89
+ end
59
90
 
60
- it "child should have ascents" do
61
- @child.ascents.should have(1).items
62
91
  end
63
92
 
64
- it "child is leaf" do
65
- @child.leaf?.should be_true
66
- end
93
+ context :type_tree do
94
+ it "convert type_tree" do
95
+ tree = Product.type_tree
96
+ tree.first[:id].should == @parent.id
97
+ tree.first[:children].should have(1).items
98
+ end
67
99
 
68
- it "parent is not leaf" do
69
- @parent.leaf?.should_not be_true
70
- end
100
+ it "convert type_tree for parent" do
101
+ tree = Product.type_tree @parent.inherit_node
102
+ tree.should be_empty
103
+ end
71
104
 
72
- it "convert type_tree" do
73
- tree = Product.type_tree
74
- tree.first[:id].should == @parent.id
75
- tree.first[:children].should have(1).items
76
- end
105
+ it "convert type_tree for child" do
106
+ tree = Product.type_tree @child.inherit_node
107
+ tree.should be_empty
108
+ end
77
109
 
78
- it "should not inherit self" do
79
- expect {
80
- @parent.inherit @parent
81
- }.to raise_error
110
+ it "convert type_tree for 2lvl-child" do
111
+ node = Product.create
112
+ node.inherit @child
113
+ tree = Product.type_tree node.inherit_node
114
+ tree.should have(1).items
115
+ end
82
116
  end
83
117
 
84
-
85
118
  context "new node" do
86
119
  subject { Product.new }
87
120
 
@@ -124,7 +157,6 @@ describe Customize::Inherited do
124
157
  }
125
158
 
126
159
  it "should have 2 inherit_association :product_objects" do
127
- pending "cannot pass with with_model"
128
160
  subject.inherit_association(:product_objects,:include=>true).should have(2).times
129
161
  end
130
162
  end
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.6
4
+ version: 0.0.7
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-04 00:00:00.000000000 Z
12
+ date: 2012-05-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -304,7 +304,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
304
304
  version: '0'
305
305
  segments:
306
306
  - 0
307
- hash: -197001151
307
+ hash: 562141071
308
308
  required_rubygems_version: !ruby/object:Gem::Requirement
309
309
  none: false
310
310
  requirements: