her 0.4.1 → 0.5

Sign up to get free protection for your applications and to get access to all the features.
data/lib/her/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Her
2
- VERSION = "0.4.1"
2
+ VERSION = "0.5"
3
3
  end
data/spec/api_spec.rb CHANGED
@@ -53,7 +53,7 @@ describe Her::API do
53
53
  end
54
54
 
55
55
  context "making HTTP requests" do
56
- let(:parsed_data) { subject.request(:_method => :get, :_path => "/foo") }
56
+ let(:parsed_data) { subject.request(:_method => :get, :_path => "/foo")[:parsed_data] }
57
57
  before do
58
58
  subject.setup :url => "https://api.example.com" do |builder|
59
59
  builder.use SimpleParser
@@ -65,7 +65,7 @@ describe Her::API do
65
65
  end
66
66
 
67
67
  context "making HTTP requests while specifying custom HTTP headers" do
68
- let(:parsed_data) { subject.request(:_method => :get, :_path => "/foo", :_headers => { "X-Page" => 2 }) }
68
+ let(:parsed_data) { subject.request(:_method => :get, :_path => "/foo", :_headers => { "X-Page" => 2 })[:parsed_data] }
69
69
 
70
70
  before do
71
71
  subject.setup :url => "https://api.example.com" do |builder|
@@ -78,7 +78,7 @@ describe Her::API do
78
78
  end
79
79
 
80
80
  context "parsing a request with the default parser" do
81
- let(:parsed_data) { subject.request(:_method => :get, :_path => "users/1") }
81
+ let(:parsed_data) { subject.request(:_method => :get, :_path => "users/1")[:parsed_data] }
82
82
  before do
83
83
  subject.setup :url => "https://api.example.com" do |builder|
84
84
  builder.use Her::Middleware::FirstLevelParseJSON
@@ -96,7 +96,7 @@ describe Her::API do
96
96
  end
97
97
 
98
98
  context "parsing a request with a custom parser" do
99
- let(:parsed_data) { subject.request(:_method => :get, :_path => "users/1") }
99
+ let(:parsed_data) { subject.request(:_method => :get, :_path => "users/1")[:parsed_data] }
100
100
  before do
101
101
  class CustomParser < Faraday::Response::Middleware
102
102
  def on_complete(env)
@@ -0,0 +1,102 @@
1
+ # encoding: utf-8
2
+ require File.join(File.dirname(__FILE__), "../spec_helper.rb")
3
+
4
+ describe "Her::Model and ActiveModel::Callbacks" do
5
+ before do
6
+ Her::API.setup :url => "https://api.example.com" do |builder|
7
+ builder.use Her::Middleware::FirstLevelParseJSON
8
+ end
9
+
10
+ spawn_model "Foo::User"
11
+ end
12
+
13
+ context :before_save do
14
+ subject { Foo::User.create(:name => "Tobias Funke") }
15
+ before do
16
+ Her::API.default_api.connection.adapter :test do |stub|
17
+ stub.post("/users") { |env| [200, {}, { :id => 1, :name => env[:body][:name] }.to_json] }
18
+ end
19
+ end
20
+
21
+ context "when using a symbol callback" do
22
+ before do
23
+ class Foo::User
24
+ before_save :alter_name
25
+ def alter_name; self.name.upcase!; end
26
+ end
27
+ end
28
+
29
+ its(:name) { should == "TOBIAS FUNKE" }
30
+ end
31
+
32
+ context "when using a block callback" do
33
+ before do
34
+ class Foo::User
35
+ before_save lambda { self.name.upcase! }
36
+ end
37
+ end
38
+
39
+ its(:name) { should == "TOBIAS FUNKE" }
40
+ end
41
+ end
42
+
43
+ context :before_create do
44
+ subject { Foo::User.create(:name => "Tobias Funke") }
45
+ before do
46
+ Her::API.default_api.connection.adapter :test do |stub|
47
+ stub.post("/users") { |env| [200, {}, { :id => 1, :name => env[:body][:name] }.to_json] }
48
+ end
49
+ end
50
+
51
+ context "when using a symbol callback" do
52
+ before do
53
+ class Foo::User
54
+ before_create :alter_name
55
+ def alter_name; self.name.upcase!; end
56
+ end
57
+ end
58
+
59
+ its(:name) { should == "TOBIAS FUNKE" }
60
+ end
61
+
62
+ context "when using a block callback" do
63
+ before do
64
+ class Foo::User
65
+ before_create lambda { self.name.upcase! }
66
+ end
67
+ end
68
+
69
+ its(:name) { should == "TOBIAS FUNKE" }
70
+ end
71
+ end
72
+
73
+ context :after_find do
74
+ subject { Foo::User.find(1) }
75
+ before do
76
+ Her::API.default_api.connection.adapter :test do |stub|
77
+ stub.get("/users/1") { |env| [200, {}, { :id => 1, :name => "Tobias Funke" }.to_json] }
78
+ end
79
+ end
80
+
81
+ context "when using a symbol callback" do
82
+ before do
83
+ class Foo::User
84
+ after_find :alter_name
85
+ def alter_name; self.name.upcase!; end
86
+ end
87
+ end
88
+
89
+ its(:name) { should == "TOBIAS FUNKE" }
90
+ end
91
+
92
+ context "when using a block callback" do
93
+ before do
94
+ class Foo::User
95
+ after_find lambda { self.name.upcase! }
96
+ end
97
+ end
98
+
99
+ its(:name) { should == "TOBIAS FUNKE" }
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+ require File.join(File.dirname(__FILE__), "../spec_helper.rb")
3
+
4
+ describe "Her::Model and ActiveModel::Dirty" do
5
+ end
@@ -92,7 +92,7 @@ describe Her::Model::HTTP do
92
92
  describe :get_raw do
93
93
  context "with a block" do
94
94
  specify do
95
- Foo::User.get_raw("/users") do |parsed_data|
95
+ Foo::User.get_raw("/users") do |parsed_data, response|
96
96
  parsed_data[:data].should == [{ :id => 1 }]
97
97
  end
98
98
  end
@@ -100,7 +100,7 @@ describe Her::Model::HTTP do
100
100
 
101
101
  context "with a return value" do
102
102
  subject { Foo::User.get_raw("/users") }
103
- specify { subject[:data].should == [{ :id => 1 }] }
103
+ specify { subject[:parsed_data][:data].should == [{ :id => 1 }] }
104
104
  end
105
105
  end
106
106
 
@@ -138,7 +138,7 @@ describe Her::Model::HTTP do
138
138
 
139
139
  describe :get_raw do
140
140
  specify do
141
- Foo::User.get_raw(:popular) do |parsed_data|
141
+ Foo::User.get_raw(:popular) do |parsed_data, response|
142
142
  parsed_data[:data].should == [{ :id => 1 }, { :id => 2 }]
143
143
  end
144
144
  end
@@ -133,8 +133,7 @@ describe Her::Model::ORM do
133
133
 
134
134
  it "handles error data on a resource" do
135
135
  @user = User.create(:name => "George Michael Bluth")
136
- @user.errors.should == ["Yes", "Sir"]
137
- @user.should be_invalid
136
+ @user.response_errors.should == ["Yes", "Sir"]
138
137
  end
139
138
  end
140
139
 
@@ -356,12 +355,14 @@ describe Her::Model::ORM do
356
355
  it "handle resource deletion through the .destroy class method" do
357
356
  @user = Foo::User.destroy_existing(1)
358
357
  @user.active.should be_false
358
+ @user.should be_destroyed
359
359
  end
360
360
 
361
361
  it "handle resource deletion through #destroy on an existing resource" do
362
362
  @user = Foo::User.find(1)
363
363
  @user.destroy
364
364
  @user.active.should be_false
365
+ @user.should be_destroyed
365
366
  end
366
367
  end
367
368
 
@@ -86,10 +86,17 @@ describe Her::Model::Relationships do
86
86
  stub.get("/users/1/role") { |env| [200, {}, { :id => 3, :body => "User" }.to_json] }
87
87
  stub.get("/users/1/posts") { |env| [200, {}, {:id => 1, :body => 'blogging stuff', :admin_id => 1 }.to_json] }
88
88
  stub.get("/organizations/1") { |env| [200, {}, { :id => 1, :name => "Bluth Company Foo" }.to_json] }
89
- stub.get("/organizations/2") { |env| [200, {}, { :id => 2, :name => "Bluth Company" }.to_json] }
90
89
  stub.post("/users") { |env| [200, {}, { :id => 5, :name => "Mr. Krabs", :comments => [{ :id => 99, :body => "Rodríguez, nasibisibusi?", :user_id => 5 }], :role => { :id => 1, :body => "Admin" }, :organization => { :id => 3, :name => "Krusty Krab" }, :organization_id => 3 }.to_json] }
91
90
  stub.put("/users/5") { |env| [200, {}, { :id => 5, :name => "Clancy Brown", :comments => [{ :id => 99, :body => "Rodríguez, nasibisibusi?", :user_id => 5 }], :role => { :id => 1, :body => "Admin" }, :organization => { :id => 3, :name => "Krusty Krab" }, :organization_id => 3 }.to_json] }
92
91
  stub.delete("/users/5") { |env| [200, {}, { :id => 5, :name => "Clancy Brown", :comments => [{ :id => 99, :body => "Rodríguez, nasibisibusi?", :user_id => 5 }], :role => { :id => 1, :body => "Admin" }, :organization => { :id => 3, :name => "Krusty Krab" }, :organization_id => 3 }.to_json] }
92
+
93
+ stub.get("/organizations/2") do |env|
94
+ if env[:params]["admin"] == "true"
95
+ [200, {}, { :id => 2, :name => "Bluth Company (admin)" }.to_json]
96
+ else
97
+ [200, {}, { :id => 2, :name => "Bluth Company" }.to_json]
98
+ end
99
+ end
93
100
  end
94
101
  end
95
102
 
@@ -185,6 +192,10 @@ describe Her::Model::Relationships do
185
192
  @user_without_included_data.get_relationship(:organization).name.should == "Bluth Company"
186
193
  end
187
194
 
195
+ it "pass query string parameters when additional arguments are passed" do
196
+ @user_without_included_data.organization(:admin => true).name.should == "Bluth Company (admin)"
197
+ end
198
+
188
199
  [:create, :save_existing, :destroy].each do |type|
189
200
  context "after #{type}" do
190
201
  let(:subject) { self.send("user_with_included_data_after_#{type}")}
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+ require File.join(File.dirname(__FILE__), "../spec_helper.rb")
3
+
4
+ describe "Her::Model and ActiveModel::Validations" do
5
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: her
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: '0.5'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rémi Prévost
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-02-27 00:00:00.000000000 Z
11
+ date: 2013-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ~>
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0.13'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activemodel
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 3.0.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: 3.0.0
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: activesupport
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -129,7 +143,6 @@ files:
129
143
  - lib/her/middleware/second_level_parse_json.rb
130
144
  - lib/her/model.rb
131
145
  - lib/her/model/base.rb
132
- - lib/her/model/hooks.rb
133
146
  - lib/her/model/http.rb
134
147
  - lib/her/model/introspection.rb
135
148
  - lib/her/model/nested_attributes.rb
@@ -142,13 +155,15 @@ files:
142
155
  - spec/middleware/accept_json_spec.rb
143
156
  - spec/middleware/first_level_parse_json_spec.rb
144
157
  - spec/middleware/second_level_parse_json_spec.rb
145
- - spec/model/hooks_spec.rb
158
+ - spec/model/callbacks_spec.rb
159
+ - spec/model/dirty_spec.rb
146
160
  - spec/model/http_spec.rb
147
161
  - spec/model/introspection_spec.rb
148
162
  - spec/model/nested_attributes_spec.rb
149
163
  - spec/model/orm_spec.rb
150
164
  - spec/model/paths_spec.rb
151
165
  - spec/model/relationships_spec.rb
166
+ - spec/model/validations_spec.rb
152
167
  - spec/model_spec.rb
153
168
  - spec/spec_helper.rb
154
169
  homepage: http://remiprev.github.com/her
@@ -171,7 +186,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
171
186
  version: '0'
172
187
  requirements: []
173
188
  rubyforge_project:
174
- rubygems_version: 2.0.0
189
+ rubygems_version: 2.0.2
175
190
  signing_key:
176
191
  specification_version: 4
177
192
  summary: A simple Representational State Transfer-based Hypertext Transfer Protocol-powered
@@ -182,12 +197,14 @@ test_files:
182
197
  - spec/middleware/accept_json_spec.rb
183
198
  - spec/middleware/first_level_parse_json_spec.rb
184
199
  - spec/middleware/second_level_parse_json_spec.rb
185
- - spec/model/hooks_spec.rb
200
+ - spec/model/callbacks_spec.rb
201
+ - spec/model/dirty_spec.rb
186
202
  - spec/model/http_spec.rb
187
203
  - spec/model/introspection_spec.rb
188
204
  - spec/model/nested_attributes_spec.rb
189
205
  - spec/model/orm_spec.rb
190
206
  - spec/model/paths_spec.rb
191
207
  - spec/model/relationships_spec.rb
208
+ - spec/model/validations_spec.rb
192
209
  - spec/model_spec.rb
193
210
  - spec/spec_helper.rb
@@ -1,114 +0,0 @@
1
- module Her
2
- module Model
3
- # Her supports hooks/callbacks that are triggered whenever resources are created, updated or destroyed.
4
- #
5
- # @example Defining a hook with a block
6
- # class User
7
- # include Her::Model
8
- # before_save { |resource| resource.internal_id = 42 }
9
- # end
10
- #
11
- # @example Defining a hook with a method name
12
- # class User
13
- # include Her::Model
14
- # before_save :set_internal_id
15
- #
16
- # private
17
- # def set_internal_id
18
- # self.internal_id = 42
19
- # end
20
- # end
21
- module Hooks
22
- # Add a *before save* callback. Triggered before a resource is created or updated.
23
- # @param [Symbol, &block] method A method or a block to be called
24
- def before_save(method=nil, &block); set_hook(:before, :save, method || block); end
25
-
26
- # Add a *before create* callback. Triggered before a resource is created.
27
- # @param [Symbol, &block] method A method or a block to be called
28
- def before_create(method=nil, &block); set_hook(:before, :create, method || block); end
29
-
30
- # Add a *before update* callback. Triggered before a resource is updated.
31
- # @param [Symbol, &block] method A method or a block to be called
32
- def before_update(method=nil, &block); set_hook(:before, :update, method || block); end
33
-
34
- # Add a *before destroy* callback. Triggered before a resource is destroyed.
35
- # @param [Symbol, &block] method A method or a block to be called
36
- def before_destroy(method=nil, &block); set_hook(:before, :destroy, method || block); end
37
-
38
- # Do not add a *before find* callback. Only *after find* is supported.
39
- def before_find(method=nil, &block); raise NoMethodError, "undefined method `before_find' for #{self}"; end
40
-
41
- # Add a *after save* callback. Triggered after a resource is created or updated.
42
- # @param [Symbol, &block] method A method or a block to be called
43
- def after_save(method=nil, &block); set_hook(:after, :save, method || block); end
44
-
45
- # Add a *after create* callback. Triggered after a resource is created.
46
- # @param [Symbol, &block] method A method or a block to be called
47
- def after_create(method=nil, &block); set_hook(:after, :create, method || block); end
48
-
49
- # Add a *after update* callback. Triggered after a resource is updated.
50
- # @param [Symbol, &block] method A method or a block to be called
51
- def after_update(method=nil, &block); set_hook(:after, :update, method || block); end
52
-
53
- # Add a *after destroy* callback. Triggered after a resource is destroyed.
54
- # @param [Symbol, &block] method A method or a block to be called
55
- def after_destroy(method=nil, &block); set_hook(:after, :destroy, method || block); end
56
-
57
- # Add a *after find* callback. Triggered after a resource is found.
58
- # @param [Symbol, &block] method A method or a block to be called
59
- def after_find(method=nil, &block); set_hook(:after, :find, method || block); end
60
-
61
- # Wrap a block between “before” and “after” hooks
62
- # @private
63
- def wrap_in_hooks(resource, *hooks)
64
- perform_before_hooks(resource, *hooks)
65
- yield(resource, resource.class) if block_given?
66
- perform_after_hooks(resource, *hooks.reverse)
67
- end
68
-
69
- # @private
70
- def hooks
71
- @her_hooks ||= begin
72
- if superclass.respond_to?(:hooks)
73
- superclass.hooks.dup
74
- else
75
- {}
76
- end
77
- end
78
- end
79
-
80
- private
81
- # @private
82
- def set_hook(time, name, action)
83
- (self.hooks["#{time}_#{name}".to_sym] ||= []) << action
84
- end
85
-
86
- # @private
87
- def perform_hook(record, time, name)
88
- Array(self.hooks["#{time}_#{name}".to_sym]).each do |hook|
89
- if hook.is_a? Symbol
90
- record.send(hook)
91
- else
92
- hook.call(record)
93
- end
94
- end
95
- end
96
-
97
- # Perform “after” hooks on a resource
98
- # @private
99
- def perform_after_hooks(resource, *hooks)
100
- hooks.each do |hook|
101
- perform_hook(resource, :after, hook)
102
- end
103
- end
104
-
105
- # Perform “before” hooks on a resource
106
- # @private
107
- def perform_before_hooks(resource, *hooks)
108
- hooks.each do |hook|
109
- perform_hook(resource, :before, hook)
110
- end
111
- end
112
- end
113
- end
114
- end
@@ -1,406 +0,0 @@
1
- # encoding: utf-8
2
- require File.join(File.dirname(__FILE__), "../spec_helper.rb")
3
-
4
- describe Her::Model::Hooks do
5
- context "adding hooks to a model" do
6
- before do
7
- spawn_model "Foo::User"
8
- end
9
-
10
- describe "method hooks" do
11
- it "handles “before save” method hooks" do
12
- Foo::User.before_save :set_internal_id
13
- Foo::User.hooks[:before_save].length.should == 1
14
- Foo::User.hooks[:before_save].first.class.should == Symbol
15
- end
16
-
17
- it "handles “before create” method hooks" do
18
- Foo::User.before_create :set_internal_id
19
- Foo::User.hooks[:before_create].length.should == 1
20
- Foo::User.hooks[:before_create].first.class.should == Symbol
21
- end
22
-
23
- it "handles “before update” method hooks" do
24
- Foo::User.before_update :set_internal_id
25
- Foo::User.hooks[:before_update].length.should == 1
26
- Foo::User.hooks[:before_update].first.class.should == Symbol
27
- end
28
-
29
- it "handles “before destroy” method hooks" do
30
- Foo::User.before_destroy :set_internal_id
31
- Foo::User.hooks[:before_destroy].length.should == 1
32
- Foo::User.hooks[:before_destroy].first.class.should == Symbol
33
- end
34
-
35
- it "does not handle “before find” method hooks" do
36
- expect {
37
- Foo::User.before_find :set_internal_id
38
- }.should raise_error(NoMethodError)
39
- end
40
-
41
- it "handles “after save” method hooks" do
42
- Foo::User.after_save :set_internal_id
43
- Foo::User.hooks[:after_save].length.should == 1
44
- Foo::User.hooks[:after_save].first.class.should == Symbol
45
- end
46
-
47
- it "handles “after create” method hooks" do
48
- Foo::User.after_create :set_internal_id
49
- Foo::User.hooks[:after_create].length.should == 1
50
- Foo::User.hooks[:after_create].first.class.should == Symbol
51
- end
52
-
53
- it "handles “after update” method hooks" do
54
- Foo::User.after_update :set_internal_id
55
- Foo::User.hooks[:after_update].length.should == 1
56
- Foo::User.hooks[:after_update].first.class.should == Symbol
57
- end
58
-
59
- it "handles “after destroy” method hooks" do
60
- Foo::User.after_destroy :set_internal_id
61
- Foo::User.hooks[:after_destroy].length.should == 1
62
- Foo::User.hooks[:after_destroy].first.class.should == Symbol
63
- end
64
-
65
- it "handles “after find” method hooks" do
66
- Foo::User.after_find :set_internal_id
67
- Foo::User.hooks[:after_find].length.should == 1
68
- Foo::User.hooks[:after_find].first.class.should == Symbol
69
- end
70
- end
71
-
72
- describe "block hooks" do
73
- it "handles “before save” block hooks" do
74
- Foo::User.before_save { |record| record.internal_id = 42 }
75
- Foo::User.hooks[:before_save].length.should == 1
76
- Foo::User.hooks[:before_save].first.class.should == Proc
77
- end
78
-
79
- it "handles “before create” block hooks" do
80
- Foo::User.before_create { |record| record.internal_id = 42 }
81
- Foo::User.hooks[:before_create].length.should == 1
82
- Foo::User.hooks[:before_create].first.class.should == Proc
83
- end
84
-
85
- it "handles “before update” block hooks" do
86
- Foo::User.before_update { |record| record.internal_id = 42 }
87
- Foo::User.hooks[:before_update].length.should == 1
88
- Foo::User.hooks[:before_update].first.class.should == Proc
89
- end
90
-
91
- it "handles “before destroy” block hooks" do
92
- Foo::User.before_destroy { |record| record.internal_id = 42 }
93
- Foo::User.hooks[:before_destroy].length.should == 1
94
- Foo::User.hooks[:before_destroy].first.class.should == Proc
95
- end
96
-
97
- it "does not handle “before find” block hooks" do
98
- expect {
99
- Foo::User.before_find :set_internal_id
100
- }.should raise_error(NoMethodError)
101
- end
102
-
103
- it "handles “after save” block hooks" do
104
- Foo::User.after_save { |record| record.internal_id = 42 }
105
- Foo::User.hooks[:after_save].length.should == 1
106
- Foo::User.hooks[:after_save].first.class.should == Proc
107
- end
108
-
109
- it "handles “after create” block hooks" do
110
- Foo::User.after_create { |record| record.internal_id = 42 }
111
- Foo::User.hooks[:after_create].length.should == 1
112
- Foo::User.hooks[:after_create].first.class.should == Proc
113
- end
114
-
115
- it "handles “after update” block hooks" do
116
- Foo::User.after_update { |record| record.internal_id = 42 }
117
- Foo::User.hooks[:after_update].length.should == 1
118
- Foo::User.hooks[:after_update].first.class.should == Proc
119
- end
120
-
121
- it "handles “after destroy” block hooks" do
122
- Foo::User.after_destroy { |record| record.internal_id = 42 }
123
- Foo::User.hooks[:after_destroy].length.should == 1
124
- Foo::User.hooks[:after_destroy].first.class.should == Proc
125
- end
126
-
127
- it "handles “after find” block hooks" do
128
- Foo::User.after_find { |record| record.internal_id = 42 }
129
- Foo::User.hooks[:after_find].length.should == 1
130
- Foo::User.hooks[:after_find].first.class.should == Proc
131
- end
132
- end
133
-
134
- context "inheriting hooks from a superclass" do
135
- it "copies hooks to the subclass" do
136
- Foo::User.before_save :set_internal_id
137
- Foo::User.after_create { |record| record.internal_id = 42 }
138
- subclass = Class.new(Foo::User)
139
- subclass.hooks.object_id.should_not == Foo::User.hooks.object_id
140
- subclass.hooks[:before_save].should == [:set_internal_id]
141
- subclass.hooks[:after_create].length.should == 1
142
- end
143
- end
144
- end
145
-
146
- context "perform hooks on a model" do
147
- before do
148
- Her::API.setup :url => "https://api.example.com" do |builder|
149
- builder.use Her::Middleware::FirstLevelParseJSON
150
- builder.use Faraday::Request::UrlEncoded
151
- builder.adapter :test do |stub|
152
- stub.get("/users") { |env| [200, {}, [{ :id => 1, :name => "Tobias Fünke" }, { :id => 2, :name => "Lindsay Fünke" }].to_json] }
153
- stub.post("/users") { |env| [200, {}, { :id => 1, :name => "Tobias Fünke" }.to_json] }
154
- stub.get("/users/1") { |env| [200, {}, { :id => 1, :name => "Tobias Fünke" }.to_json] }
155
- stub.get("/users/2") { |env| [200, {}, { :id => 2, :name => "Lindsay Fünke" }.to_json] }
156
- stub.put("/users/1") { |env| [200, {}, { :id => 1, :name => "Tobias Fünke" }.to_json] }
157
- stub.delete("/users/1") { |env| [200, {}, { :id => 1, :name => "Tobias Fünke" }.to_json] }
158
- end
159
- end
160
-
161
- spawn_model "Foo::User" do
162
- attr_accessor :internal_save_id, :internal_create_id, :internal_update_id, :internal_destroy_id
163
- attr_accessor :internal_after_save_id, :internal_after_create_id, :internal_after_update_id, :internal_after_destroy_id
164
- attr_accessor :internal_after_find_id
165
-
166
- def change_internal_save_id; @internal_save_id = 100; end
167
- def change_internal_create_id; @internal_create_id = 101; end
168
- def change_internal_update_id; @internal_update_id = 102; end
169
- def change_internal_destroy_id; @internal_destroy_id = 103; end
170
-
171
- def change_internal_after_save_id; @internal_after_save_id = 100; end
172
- def change_internal_after_create_id; @internal_after_create_id = 101; end
173
- def change_internal_after_update_id; @internal_after_update_id = 102; end
174
- def change_internal_after_destroy_id; @internal_after_destroy_id = 103; end
175
-
176
- def change_internal_after_find_id; @internal_after_find_id = 104; end
177
- end
178
- end
179
-
180
- describe "method hooks" do
181
- before do
182
- Foo::User.before_save :change_internal_save_id
183
- Foo::User.before_update :change_internal_update_id
184
- Foo::User.before_create :change_internal_create_id
185
- Foo::User.before_destroy :change_internal_destroy_id
186
-
187
- Foo::User.after_save :change_internal_after_save_id
188
- Foo::User.after_update :change_internal_after_update_id
189
- Foo::User.after_create :change_internal_after_create_id
190
- Foo::User.after_destroy :change_internal_after_destroy_id
191
-
192
- Foo::User.after_find :change_internal_after_find_id
193
- end
194
-
195
- it "perform “before save” “before create” method hook on Model#save without an ID" do
196
- @user = Foo::User.new(:fullname => "Tobias Fünke")
197
- @user.save
198
- @user.internal_save_id.should == 100
199
- @user.internal_create_id.should == 101
200
- @user.internal_update_id.should == nil
201
- @user.internal_after_find_id.should == nil
202
- end
203
-
204
- it "perform “before save” and “before update” method hook on Model#save with an ID" do
205
- @user = Foo::User.find(1)
206
- @user.save
207
- @user.internal_save_id.should == 100
208
- @user.internal_create_id.should == nil
209
- @user.internal_update_id.should == 102
210
- @user.internal_after_find_id.should == 104
211
- end
212
-
213
- it "perform “before destroy” method hook on Model#destroy" do
214
- @user = Foo::User.find(1)
215
- @user.destroy
216
- @user.internal_save_id.should == nil
217
- @user.internal_create_id.should == nil
218
- @user.internal_update_id.should == nil
219
- @user.internal_destroy_id.should == 103
220
- @user.internal_after_find_id.should == 104
221
- end
222
-
223
- it "perform “after save” “after create” method hook on Model#save without an ID" do
224
- @user = Foo::User.new(:fullname => "Tobias Fünke")
225
- @user.save
226
- @user.internal_after_save_id.should == 100
227
- @user.internal_after_create_id.should == 101
228
- @user.internal_after_update_id.should == nil
229
- @user.internal_after_find_id.should == nil
230
- end
231
-
232
- it "perform “after save” “after update” method hook on Model#save with an ID" do
233
- @user = Foo::User.find(1)
234
- @user.save
235
- @user.internal_after_save_id.should == 100
236
- @user.internal_after_create_id.should == nil
237
- @user.internal_after_update_id.should == 102
238
- @user.internal_after_find_id.should == 104
239
- end
240
-
241
- it "perform “after save” “after update” method hook on Model.save_existing" do
242
- @user = Foo::User.save_existing(1, { :fullname => "Tobias Fünke" })
243
- @user.internal_after_save_id.should == 100
244
- @user.internal_after_create_id.should == nil
245
- @user.internal_after_update_id.should == 102
246
- @user.internal_after_find_id.should == nil
247
- end
248
-
249
- it "perform “after save” “after create” method hook on Model.create" do
250
- @user = Foo::User.create({ :fullname => "Tobias Fünke" })
251
- @user.internal_after_save_id.should == 100
252
- @user.internal_after_create_id.should == 101
253
- @user.internal_after_update_id.should == nil
254
- @user.internal_after_find_id.should == nil
255
- end
256
-
257
- it "perform “after find” method hook on Model.find" do
258
- @user = Foo::User.find(1)
259
- @user.internal_after_find_id.should == 104
260
- @user.internal_save_id.should == nil
261
- @user.internal_create_id.should == nil
262
- @user.internal_update_id.should == nil
263
- @user.internal_destroy_id.should == nil
264
- end
265
-
266
- it "perform “after find” method hook on Model#find with IDs" do
267
- @users = Foo::User.find([1,2])
268
- @users.each do |user|
269
- user.internal_after_find_id.should == 104
270
- user.internal_save_id.should == nil
271
- user.internal_create_id.should == nil
272
- user.internal_update_id.should == nil
273
- user.internal_destroy_id.should == nil
274
- end
275
- end
276
-
277
- it "perform “after find” method hook on Model.all" do
278
- @users = Foo::User.all
279
- @users.each do |user|
280
- user.internal_after_find_id.should == 104
281
- user.internal_save_id.should == nil
282
- user.internal_create_id.should == nil
283
- user.internal_update_id.should == nil
284
- user.internal_destroy_id.should == nil
285
- end
286
- end
287
- end
288
-
289
- describe "block hooks" do
290
- before do
291
- Foo::User.before_save { |record| record.internal_save_id = 200 }
292
- Foo::User.before_create { |record| record.internal_create_id = 201 }
293
- Foo::User.before_update { |record| record.internal_update_id = 202 }
294
- Foo::User.before_destroy { |record| record.internal_destroy_id = 203 }
295
-
296
- Foo::User.after_save { |record| record.internal_after_save_id = 200 }
297
- Foo::User.after_create { |record| record.internal_after_create_id = 201 }
298
- Foo::User.after_update { |record| record.internal_after_update_id = 202 }
299
- Foo::User.after_destroy { |record| record.internal_after_destroy_id = 203 }
300
- Foo::User.after_find { |record| record.internal_after_find_id = 204 }
301
- end
302
-
303
- it "perform “before save” and “before create” block hook on Model#save without an ID" do
304
- @user = Foo::User.new(:fullname => "Tobias Fünke")
305
- @user.save
306
- @user.internal_save_id.should == 200
307
- @user.internal_create_id.should == 201
308
- @user.internal_update_id.should == nil
309
- @user.internal_after_find_id.should == nil
310
- end
311
-
312
- it "perform “before save” and “before update” block hook on Model#save with an ID" do
313
- @user = Foo::User.find(1)
314
- @user.save
315
- @user.internal_save_id.should == 200
316
- @user.internal_create_id.should == nil
317
- @user.internal_update_id.should == 202
318
- @user.internal_after_find_id.should == 204
319
- end
320
-
321
- it "perform “before destroy” block hook on Model#destroy" do
322
- @user = Foo::User.find(1)
323
- @user.destroy
324
- @user.internal_save_id.should == nil
325
- @user.internal_create_id.should == nil
326
- @user.internal_update_id.should == nil
327
- @user.internal_destroy_id.should == 203
328
- @user.internal_after_find_id.should == 204
329
- end
330
-
331
- it "perform “after save” “after create” block hook on Model#save without an ID" do
332
- @user = Foo::User.new(:fullname => "Tobias Fünke")
333
- @user.save
334
- @user.internal_after_save_id.should == 200
335
- @user.internal_after_create_id.should == 201
336
- @user.internal_after_update_id.should == nil
337
- @user.internal_after_find_id.should == nil
338
- end
339
-
340
- it "perform “after save” “after update” block hook on Model#save with an ID" do
341
- @user = Foo::User.find(1)
342
- @user.save
343
- @user.internal_after_find_id.should == 204
344
- @user.internal_after_save_id.should == 200
345
- @user.internal_after_create_id.should == nil
346
- @user.internal_after_update_id.should == 202
347
- end
348
-
349
- it "perform “after find” block hook on Model#find" do
350
- @user = Foo::User.find(1)
351
- @user.internal_after_find_id.should == 204
352
- @user.internal_save_id.should == nil
353
- @user.internal_create_id.should == nil
354
- @user.internal_update_id.should == nil
355
- end
356
-
357
- it "perform “after find” method hook on Model#find with IDs" do
358
- @users = Foo::User.find([1,2])
359
- @users.each do |user|
360
- user.internal_after_find_id.should == 204
361
- user.internal_save_id.should == nil
362
- user.internal_create_id.should == nil
363
- user.internal_update_id.should == nil
364
- end
365
- end
366
-
367
- it "perform “after find” method hook on Model.all" do
368
- @users = Foo::User.all
369
- @users.each do |user|
370
- user.internal_after_find_id.should == 204
371
- user.internal_save_id.should == nil
372
- user.internal_create_id.should == nil
373
- user.internal_update_id.should == nil
374
- end
375
- end
376
-
377
- it "perform “after find” block hook on Model#find" do
378
- @user = Foo::User.find(1)
379
- @user.internal_after_find_id.should == 204
380
- @user.internal_save_id.should == nil
381
- @user.internal_create_id.should == nil
382
- @user.internal_update_id.should == nil
383
- end
384
-
385
- it "perform “after find” method hook on Model#find with IDs" do
386
- @users = Foo::User.find([1,2])
387
- @users.each do |user|
388
- user.internal_after_find_id.should == 204
389
- user.internal_save_id.should == nil
390
- user.internal_create_id.should == nil
391
- user.internal_update_id.should == nil
392
- end
393
- end
394
-
395
- it "perform “after find” method hook on Model.all" do
396
- @users = Foo::User.all
397
- @users.each do |user|
398
- user.internal_after_find_id.should == 204
399
- user.internal_save_id.should == nil
400
- user.internal_create_id.should == nil
401
- user.internal_update_id.should == nil
402
- end
403
- end
404
- end
405
- end
406
- end