customize 0.0.5 → 0.0.6

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.5
1
+ 0.0.6
@@ -4,12 +4,14 @@ module Customize
4
4
  serialize :calculator_attributes
5
5
 
6
6
  def calculator
7
+ return unless calculator_name
7
8
  cal = calculator_name.constantize
8
9
  cal_ins = cal.new
9
- #calculator_attributes.try :symbolize_keys!
10
- (calculator_attributes.keys & cal.inputs).each do |name|
10
+ calculator_attributes.try :symbolize_keys! if calculator_attributes.respond_to? :symbolize_keys!
11
+ names = (calculator_attributes.keys.map!(&:to_sym) & cal.inputs) if calculator_attributes
12
+ names.each do |name|
11
13
  cal_ins.instance_variable_set "@#{name}", calculator_attributes[name]
12
- end if calculator_attributes
14
+ end if names
13
15
  cal_ins
14
16
  end
15
17
  end
@@ -3,6 +3,13 @@ class Customize::InheritNode < ActiveRecord::Base
3
3
  belongs_to :parent_node, :class_name=>self.name, :foreign_key=> 'parent_id'
4
4
  belongs_to :node, :polymorphic=>true
5
5
  has_many :children, :class_name=>self.name, :foreign_key=>'parent_id'
6
+ has_many :ancestors, :class_name=>self.name, :finder_sql=> proc {
7
+ %Q{
8
+ select distinct nodes.*
9
+ from customize_inherit_nodes nodes
10
+ where nodes.right > #{right} and nodes.left < #{left} and nodes.node_type = "#{node_type}"
11
+ }
12
+ }
6
13
 
7
14
  scope :by_type, lambda { |type| where(:node_type=>type) }
8
15
 
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "customize"
8
- s.version = "0.0.5"
8
+ s.version = "0.0.6"
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-02"
12
+ s.date = "2012-05-04"
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 = [
@@ -1,15 +1,19 @@
1
1
  module Customize
2
2
  module Calculator
3
- class Base
3
+ class Base
4
4
  cattr_accessor :inputs
5
- @@inputs = []
5
+ self.inputs = []
6
6
  def self.input name
7
7
  sym = name.to_sym
8
- @@inputs << sym
8
+ inputs << sym
9
9
  attr_accessor sym
10
10
  end
11
11
 
12
- def calculate instance
12
+ def calculate instance, options={}
13
+ end
14
+
15
+ def persisted?
16
+ false
13
17
  end
14
18
  end
15
19
  end
@@ -3,7 +3,7 @@ module Customize
3
3
  class ExpressionCalculator < Base
4
4
  input :expression
5
5
 
6
- def calculate instance
6
+ def calculate instance, options={}
7
7
  instance.instance_eval expression
8
8
  end
9
9
  end
@@ -5,6 +5,28 @@ module Customize
5
5
 
6
6
  base.has_one :inherit_node, :class_name=>Customize::InheritNode.name, :as=>:node, :dependent=>:destroy
7
7
 
8
+ base.scope :ascents_for, lambda { |object, options={:include=>false}|
9
+ ntn = Customize::InheritNode.table_name
10
+ node = object.inherit_node
11
+ condition_string = if options[:include]
12
+ "#{ntn}.left <= ? and #{ntn}.right >= ?"
13
+ else
14
+ "#{ntn}.left < ? and #{ntn}.right > ?"
15
+ end
16
+ base.joins(:inherit_node).where(condition_string, node.left, node.right)
17
+ }
18
+
19
+ base.scope :descents_for, lambda { |object, options={:include=>false}|
20
+ ntn = Customize::InheritNode.table_name
21
+ node = object.inherit_node
22
+ condition_string = if options[:include]
23
+ "#{ntn}.left >= ? and #{ntn}.right <= ?"
24
+ else
25
+ "#{ntn}.left > ? and #{ntn}.right < ?"
26
+ end
27
+ base.joins(:inherit_node).where(condition_string, node.left, node.right)
28
+ }
29
+
8
30
  base.after_create { |object|
9
31
  object.create_inherit_node :left=>0, :right=>1
10
32
  }
@@ -14,6 +36,8 @@ module Customize
14
36
  base.before_destroy { |object|
15
37
  raise 'object should not have children when destroy' if object.inherit_node.children.size > 0
16
38
  }
39
+
40
+
17
41
 
18
42
  end
19
43
 
@@ -50,30 +74,39 @@ module Customize
50
74
  raise 'should be save first' if self.new_record?
51
75
  raise 'should be same class' if self.class != parent.class
52
76
  raise 'should not be self' if self.id == parent.id
53
- inherit_node.parent_id = parent.inherit_node.id
54
- right = parent.inherit_node.right
55
- InheritNode.where("right >= ?", inherit_node.left).update_all("right = right+2")
56
- inherit_node.left = right
57
- inherit_node.right = right + 1
58
- inherit_node.save
77
+ self.class.transaction do
78
+ inherit_node.parent_id = parent.inherit_node.id
79
+ right = parent.inherit_node.right
80
+ InheritNode.where("right >= ?", inherit_node.left).update_all("right = right+2")
81
+ inherit_node.left = right
82
+ inherit_node.right = right + 1
83
+ inherit_node.save
84
+ end
59
85
  end
60
86
 
61
- def ascent_ids
62
- return [] if self.new_record?
63
- r = InheritNode.where("left < :left and right > :right and node_type = :type",:left=>inherit_node.left, :right=>inherit_node.right, :type=>self.class.name)
64
- r.pluck(:node_id)
87
+ def ascents options={:include=>false}
88
+ return [] if new_record?
89
+ self.class.ascents_for self, options
65
90
  end
66
91
 
67
- def descent_ids
68
- return [] if self.new_record?
69
- r = InheritNode.where("left > :left and right < :right and node_type = :type",
70
- :left=>inherit_node.left, :right=>inherit_node.right, :type=>self.class.name)
71
- r.pluck(:node_id)
92
+ 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
72
97
  end
73
98
 
74
- def ascents
75
- self.class.find ascent_ids
99
+ def descents
100
+ return [] if new_record?
101
+ self.class.descents_for self
76
102
  end
77
103
 
104
+ def ascent_ids
105
+ ascents.map(&:id)
106
+ end
107
+
108
+ def descent_ids
109
+ descents.map(&:id)
110
+ end
78
111
  end
79
112
  end
@@ -29,7 +29,7 @@ describe Customize::Formulaic do
29
29
  end
30
30
 
31
31
  it "store input value" do
32
- subject.calculator_attributes = {:expression=>1}
32
+ subject.calculator_attributes = {"expression"=>1}
33
33
  subject.save
34
34
  subject.reload
35
35
  subject.calculator.expression.should == 1
@@ -8,6 +8,16 @@ describe Customize::Inherited do
8
8
  end
9
9
  model do
10
10
  include Customize::Inherited
11
+ has_many :product_objects
12
+ end
13
+ end
14
+
15
+ with_model :product_object do
16
+ table do |t|
17
+ t.references :product
18
+ end
19
+ model do
20
+ belongs_to :product
11
21
  end
12
22
  end
13
23
 
@@ -103,5 +113,21 @@ describe Customize::Inherited do
103
113
  end
104
114
  end
105
115
 
116
+ context "associations" do
117
+ subject {
118
+ p1 = Product.create
119
+ p2 = Product.create
120
+ p2.inherit p1
121
+ p1.product_objects.create
122
+ p2.product_objects.create
123
+ p2
124
+ }
125
+
126
+ it "should have 2 inherit_association :product_objects" do
127
+ pending "cannot pass with with_model"
128
+ subject.inherit_association(:product_objects,:include=>true).should have(2).times
129
+ end
130
+ end
131
+
106
132
  end
107
133
 
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.5
4
+ version: 0.0.6
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-02 00:00:00.000000000 Z
12
+ date: 2012-05-04 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: 1041955549
307
+ hash: -197001151
308
308
  required_rubygems_version: !ruby/object:Gem::Requirement
309
309
  none: false
310
310
  requirements: