customize 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -5,6 +5,8 @@ source "http://ruby.taobao.org"
5
5
 
6
6
  gem "rails", ">= 3.0.12"
7
7
  gem "capybara", ">= 0.4.0"
8
+ gem "inherited_resources"
9
+ gem "has_scope"
8
10
 
9
11
  group :development,:test do
10
12
  gem "sqlite3"
@@ -38,7 +38,7 @@ GEM
38
38
  rack-test (>= 0.5.4)
39
39
  selenium-webdriver (~> 2.0)
40
40
  xpath (~> 0.1.4)
41
- childprocess (0.3.1)
41
+ childprocess (0.3.2)
42
42
  ffi (~> 1.0.6)
43
43
  cucumber (1.1.9)
44
44
  builder (>= 2.1.2)
@@ -52,8 +52,12 @@ GEM
52
52
  gherkin (2.9.3)
53
53
  json (>= 1.4.6)
54
54
  git (1.2.5)
55
+ has_scope (0.5.1)
55
56
  hike (1.2.1)
56
57
  i18n (0.6.0)
58
+ inherited_resources (1.3.1)
59
+ has_scope (~> 0.5.0)
60
+ responders (~> 0.6)
57
61
  jeweler (1.8.3)
58
62
  bundler (~> 1.0)
59
63
  git (>= 1.2.5)
@@ -96,6 +100,8 @@ GEM
96
100
  rake (0.9.2.2)
97
101
  rdoc (3.12)
98
102
  json (~> 1.4)
103
+ responders (0.9.1)
104
+ railties (~> 3.1)
99
105
  rspec (2.9.0)
100
106
  rspec-core (~> 2.9.0)
101
107
  rspec-expectations (~> 2.9.0)
@@ -116,8 +122,8 @@ GEM
116
122
  libwebsocket (~> 0.1.3)
117
123
  multi_json (~> 1.0)
118
124
  rubyzip
119
- simplecov (0.6.1)
120
- multi_json (~> 1.0)
125
+ simplecov (0.6.2)
126
+ multi_json (~> 1.3)
121
127
  simplecov-html (~> 0.5.3)
122
128
  simplecov-html (0.5.3)
123
129
  sprockets (2.1.2)
@@ -147,6 +153,8 @@ DEPENDENCIES
147
153
  bundler (>= 1.0.0)
148
154
  capybara (>= 0.4.0)
149
155
  cucumber
156
+ has_scope
157
+ inherited_resources
150
158
  jeweler (>= 1.8.3)
151
159
  rails (>= 3.0.12)
152
160
  rdoc (~> 3.12)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
@@ -0,0 +1,27 @@
1
+ class Customize::InheritNodesController < InheritedResources::Base
2
+ include InheritedResources::DSL
3
+ respond_to :json, :xml
4
+ has_scope :by_type
5
+
6
+ custom_actions :resource=>:select
7
+
8
+ index! do |format|
9
+ format.json do
10
+ render :json=>collection.collect{|item|{:label=>item.node.label,:value=>item.node.id}}
11
+ end
12
+ end
13
+
14
+ def select
15
+ select! do |format|
16
+ node = Customize::InheritNode.find params[:target]
17
+ begin
18
+ resource.node.inherit node.node
19
+ rescue
20
+ end
21
+ format.json do
22
+ render :json=>resource.save
23
+ end
24
+ end
25
+ end
26
+
27
+ end
@@ -0,0 +1,31 @@
1
+ class Customize::Character < ActiveRecord::Base
2
+ attr_accessor :content
3
+ self.table_name = "customize_characters"
4
+ serialize :value
5
+ belongs_to :related, :polymorphic => true
6
+
7
+ before_validation do |object|
8
+ unless object.content.nil?
9
+ case
10
+ when object.content =~ /\d+/
11
+ object.value = content.to_i
12
+ object.value_type = :integer
13
+ when content =~ /(true)|(false)/
14
+ object.value = Boolean.new content
15
+ object.value_type = :boolean
16
+ when content =~ /[0-9\.]+/
17
+ object.value = Decimal.new content
18
+ object.value_type = :decimal
19
+ when content =~ /^\[/
20
+ object.value = ""
21
+ object.value_type = :list
22
+ when content =~ /^\{/
23
+ object.value = ""
24
+ object.value_type = :hash
25
+ else
26
+ object.value = content
27
+ object.value_type = :string
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,11 +1,19 @@
1
1
  class Customize::InheritNode < ActiveRecord::Base
2
2
  self.table_name = "customize_inherit_nodes"
3
3
  belongs_to :parent_node, :class_name=>self.name, :foreign_key=> 'parent_id'
4
+ belongs_to :node, :polymorphic=>true
4
5
  has_many :children, :class_name=>self.name, :foreign_key=>'parent_id'
6
+
7
+ scope :by_type, lambda { |type| where(:node_type=>type) }
5
8
 
6
9
  after_destroy { |object|
7
10
  object.class.where("right > :right and node_type = :type",
8
11
  :right=>object.right, :type=>object.node_type).update_all("right = right -2")
9
12
  }
13
+
14
+ def leaf?
15
+ children.empty?
16
+ end
17
+
10
18
  end
11
19
 
@@ -0,0 +1,8 @@
1
+ Customize::Engine.routes.draw do
2
+ resources :characters
3
+ resources :inherit_nodes do
4
+ member do
5
+ get :select
6
+ end
7
+ end
8
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "customize"
8
- s.version = "0.0.3"
8
+ s.version = "0.0.4"
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-04-20"
12
+ s.date = "2012-05-02"
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 = [
@@ -24,12 +24,14 @@ Gem::Specification.new do |s|
24
24
  "README.rdoc",
25
25
  "Rakefile",
26
26
  "VERSION",
27
+ "app/controllers/customize/inherit_nodes_controller.rb",
28
+ "app/models/customize/character.rb",
29
+ "app/models/customize/inherit_node.rb",
30
+ "config/routes.rb",
27
31
  "customize.gemspec",
28
32
  "lib/customize.rb",
29
- "lib/customize/character.rb",
30
33
  "lib/customize/characterize.rb",
31
34
  "lib/customize/engine.rb",
32
- "lib/customize/inherit_node.rb",
33
35
  "lib/customize/inherited.rb",
34
36
  "lib/generators/customize/USAGE",
35
37
  "lib/generators/customize/customize_generator.rb",
@@ -85,6 +87,8 @@ Gem::Specification.new do |s|
85
87
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
86
88
  s.add_runtime_dependency(%q<rails>, [">= 3.0.12"])
87
89
  s.add_runtime_dependency(%q<capybara>, [">= 0.4.0"])
90
+ s.add_runtime_dependency(%q<inherited_resources>, [">= 0"])
91
+ s.add_runtime_dependency(%q<has_scope>, [">= 0"])
88
92
  s.add_development_dependency(%q<sqlite3>, [">= 0"])
89
93
  s.add_development_dependency(%q<rspec-rails>, [">= 2.8.0"])
90
94
  s.add_development_dependency(%q<sqlite3-ruby>, [">= 0"])
@@ -97,6 +101,8 @@ Gem::Specification.new do |s|
97
101
  else
98
102
  s.add_dependency(%q<rails>, [">= 3.0.12"])
99
103
  s.add_dependency(%q<capybara>, [">= 0.4.0"])
104
+ s.add_dependency(%q<inherited_resources>, [">= 0"])
105
+ s.add_dependency(%q<has_scope>, [">= 0"])
100
106
  s.add_dependency(%q<sqlite3>, [">= 0"])
101
107
  s.add_dependency(%q<rspec-rails>, [">= 2.8.0"])
102
108
  s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
@@ -110,6 +116,8 @@ Gem::Specification.new do |s|
110
116
  else
111
117
  s.add_dependency(%q<rails>, [">= 3.0.12"])
112
118
  s.add_dependency(%q<capybara>, [">= 0.4.0"])
119
+ s.add_dependency(%q<inherited_resources>, [">= 0"])
120
+ s.add_dependency(%q<has_scope>, [">= 0"])
113
121
  s.add_dependency(%q<sqlite3>, [">= 0"])
114
122
  s.add_dependency(%q<rspec-rails>, [">= 2.8.0"])
115
123
  s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
@@ -1,9 +1,8 @@
1
1
  require 'active_record'
2
+ require 'has_scope'
2
3
  require 'customize/engine'
3
4
 
4
5
  module Customize
5
6
  autoload :Characterize, 'customize/characterize'
6
7
  autoload :Inherited, 'customize/inherited'
7
- autoload :InheritNode, 'customize/inherit_node'
8
- autoload :Character, 'customize/character'
9
8
  end
@@ -4,6 +4,7 @@ module Customize
4
4
  def self.included base
5
5
  base.extend ClassMethods
6
6
  base.has_many :characters, :class_name=>Customize::Character.name, :as=>:related
7
+ base.accepts_nested_attributes_for :characters
7
8
  end
8
9
 
9
10
  module ClassMethods
@@ -20,7 +21,12 @@ module Customize
20
21
  }.call
21
22
 
22
23
  end
23
-
24
+
25
+ def inherited_characters
26
+ if self.class.include?(Customize::Inherited)
27
+ Character.where(:related_id=>ascent_ids, :related_type=>self.class.name)
28
+ end
29
+ end
24
30
  end
25
31
 
26
32
  end
@@ -1,5 +1,6 @@
1
1
  module Customize
2
2
  class Engine < ::Rails::Engine
3
+ isolate_namespace Customize
3
4
  config.generators do |g|
4
5
  # g.test_framework :rspec, :view_specs => false
5
6
  end
@@ -2,12 +2,15 @@ module Customize
2
2
  module Inherited
3
3
  def self.included base
4
4
  base.extend ClassMethods
5
+
5
6
  base.has_one :inherit_node, :class_name=>Customize::InheritNode.name, :as=>:node, :dependent=>:destroy
6
7
 
7
8
  base.after_create { |object|
8
9
  object.create_inherit_node :left=>0, :right=>1
9
10
  }
10
11
 
12
+ base.delegate :leaf?, :to=>:inherit_node
13
+
11
14
  base.before_destroy { |object|
12
15
  raise 'object should not have children when destroy' if object.inherit_node.children.size > 0
13
16
  }
@@ -18,11 +21,35 @@ module Customize
18
21
  def root
19
22
  joins(:inherit_node).where("parent_id is null")
20
23
  end
24
+
25
+ def type_tree
26
+ roots = root
27
+ converter = proc {|items|
28
+ out = items.collect { |item|
29
+ {:id=>item.id,:label=>item.label,:inherit_node_id=>item.inherit_node.id, :children=>converter.call(item.children)}
30
+ }
31
+ }
32
+
33
+ converter.call(roots)
34
+ end
35
+ end
36
+
37
+ def parent
38
+ inherit_node.parent_node.try(:node)
39
+ end
40
+
41
+ def children
42
+ inherit_node.children.collect(&:node)
43
+ end
44
+
45
+ def label
46
+ self.to_s
21
47
  end
22
48
 
23
49
  def inherit parent
24
50
  raise 'should be save first' if self.new_record?
25
51
  raise 'should be same class' if self.class != parent.class
52
+ raise 'should not be self' if self.id == parent.id
26
53
  inherit_node.parent_id = parent.inherit_node.id
27
54
  right = parent.inherit_node.right
28
55
  InheritNode.where("right >= ?", inherit_node.left).update_all("right = right+2")
@@ -3,7 +3,7 @@ class CreateCustomize < ActiveRecord::Migration
3
3
  def change
4
4
  create_table :customize_characters do |t|
5
5
  t.string :key
6
- t.string :type
6
+ t.string :value_type
7
7
  t.text :value
8
8
  t.string :related_type
9
9
  t.integer :related_id
@@ -9,8 +9,8 @@ describe Customize::Characterize do
9
9
  end
10
10
 
11
11
  model do
12
- include Customize::Characterize
13
12
  include Customize::Inherited
13
+ include Customize::Characterize
14
14
  end
15
15
  end
16
16
 
@@ -31,10 +31,20 @@ describe Customize::Characterize do
31
31
  subject.character[:foo].should be_true
32
32
  end
33
33
 
34
+ it :should_not_return_inherited_characters do
35
+ subject.inherited_characters.should be_nil
36
+ end
34
37
  end
35
38
 
36
39
  context :model_with_inherit do
37
- subject { Product.create! }
40
+ subject {
41
+ Customize::Character.delete_all
42
+ parent = Product.create!
43
+ parent.characters.create! :key=>:p, :value=>1
44
+ p = Product.create!
45
+ p.inherit parent
46
+ p
47
+ }
38
48
 
39
49
  it "has characters" do
40
50
  subject.characters.should_not be_nil
@@ -70,11 +80,11 @@ describe Customize::Characterize do
70
80
  end
71
81
 
72
82
  it "has inherited character" do
73
- parent = Product.create!
74
- parent.characters.create! :key=>:p, :value=>1
75
- subject.inherit parent
76
83
  subject.character[:p].should == 1
77
84
  end
85
+ it :has_def_inherited_characters do
86
+ subject.inherited_characters.should_not be_empty
87
+ end
78
88
  end
79
89
 
80
90
  end
@@ -51,6 +51,27 @@ describe Customize::Inherited do
51
51
  @child.ascents.should have(1).items
52
52
  end
53
53
 
54
+ it "child is leaf" do
55
+ @child.leaf?.should be_true
56
+ end
57
+
58
+ it "parent is not leaf" do
59
+ @parent.leaf?.should_not be_true
60
+ end
61
+
62
+ it "convert type_tree" do
63
+ tree = Product.type_tree
64
+ tree.first[:id].should == @parent.id
65
+ tree.first[:children].should have(1).items
66
+ end
67
+
68
+ it "should not inherit self" do
69
+ expect {
70
+ @parent.inherit @parent
71
+ }.to raise_error
72
+ end
73
+
74
+
54
75
  context "new node" do
55
76
  subject { Product.new }
56
77
 
@@ -1,4 +1,5 @@
1
1
  Dummy::Application.routes.draw do
2
+ mount Customize::Engine, :at => "customize"
2
3
  # The priority is based upon order of creation:
3
4
  # first created -> highest priority.
4
5
 
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.3
4
+ version: 0.0.4
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-04-20 00:00:00.000000000 Z
12
+ date: 2012-05-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -43,6 +43,38 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: 0.4.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: inherited_resources
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: has_scope
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
46
78
  - !ruby/object:Gem::Dependency
47
79
  name: sqlite3
48
80
  requirement: !ruby/object:Gem::Requirement
@@ -202,12 +234,14 @@ files:
202
234
  - README.rdoc
203
235
  - Rakefile
204
236
  - VERSION
237
+ - app/controllers/customize/inherit_nodes_controller.rb
238
+ - app/models/customize/character.rb
239
+ - app/models/customize/inherit_node.rb
240
+ - config/routes.rb
205
241
  - customize.gemspec
206
242
  - lib/customize.rb
207
- - lib/customize/character.rb
208
243
  - lib/customize/characterize.rb
209
244
  - lib/customize/engine.rb
210
- - lib/customize/inherit_node.rb
211
245
  - lib/customize/inherited.rb
212
246
  - lib/generators/customize/USAGE
213
247
  - lib/generators/customize/customize_generator.rb
@@ -265,7 +299,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
265
299
  version: '0'
266
300
  segments:
267
301
  - 0
268
- hash: 348052147
302
+ hash: -24310993
269
303
  required_rubygems_version: !ruby/object:Gem::Requirement
270
304
  none: false
271
305
  requirements:
@@ -1,5 +0,0 @@
1
- class Customize::Character < ActiveRecord::Base
2
- self.table_name = "customize_characters"
3
- serialize :value
4
- belongs_to :related, :polymorphic => true
5
- end