extended_her 0.5
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/.gitignore +8 -0
- data/.rspec +2 -0
- data/.travis.yml +8 -0
- data/CONTRIBUTING.md +26 -0
- data/Gemfile +2 -0
- data/LICENSE +7 -0
- data/README.md +723 -0
- data/Rakefile +11 -0
- data/UPGRADE.md +32 -0
- data/examples/twitter-oauth/Gemfile +13 -0
- data/examples/twitter-oauth/app.rb +50 -0
- data/examples/twitter-oauth/config.ru +5 -0
- data/examples/twitter-oauth/views/index.haml +9 -0
- data/examples/twitter-search/Gemfile +12 -0
- data/examples/twitter-search/app.rb +55 -0
- data/examples/twitter-search/config.ru +5 -0
- data/examples/twitter-search/views/index.haml +9 -0
- data/extended_her.gemspec +27 -0
- data/lib/her.rb +23 -0
- data/lib/her/api.rb +108 -0
- data/lib/her/base.rb +17 -0
- data/lib/her/collection.rb +12 -0
- data/lib/her/errors.rb +5 -0
- data/lib/her/exceptions/exception.rb +4 -0
- data/lib/her/exceptions/record_invalid.rb +8 -0
- data/lib/her/exceptions/record_not_found.rb +13 -0
- data/lib/her/middleware.rb +9 -0
- data/lib/her/middleware/accept_json.rb +15 -0
- data/lib/her/middleware/first_level_parse_json.rb +34 -0
- data/lib/her/middleware/second_level_parse_json.rb +28 -0
- data/lib/her/model.rb +69 -0
- data/lib/her/model/base.rb +7 -0
- data/lib/her/model/hooks.rb +114 -0
- data/lib/her/model/http.rb +284 -0
- data/lib/her/model/introspection.rb +57 -0
- data/lib/her/model/orm.rb +191 -0
- data/lib/her/model/orm/comparison_methods.rb +20 -0
- data/lib/her/model/orm/create_methods.rb +29 -0
- data/lib/her/model/orm/destroy_methods.rb +53 -0
- data/lib/her/model/orm/error_methods.rb +19 -0
- data/lib/her/model/orm/fields_definition.rb +15 -0
- data/lib/her/model/orm/find_methods.rb +46 -0
- data/lib/her/model/orm/persistance_methods.rb +22 -0
- data/lib/her/model/orm/relation_mapper.rb +21 -0
- data/lib/her/model/orm/save_methods.rb +58 -0
- data/lib/her/model/orm/serialization_methods.rb +28 -0
- data/lib/her/model/orm/update_methods.rb +31 -0
- data/lib/her/model/paths.rb +82 -0
- data/lib/her/model/relationships.rb +191 -0
- data/lib/her/paginated_collection.rb +20 -0
- data/lib/her/relation.rb +94 -0
- data/lib/her/version.rb +3 -0
- data/spec/api_spec.rb +131 -0
- data/spec/collection_spec.rb +26 -0
- data/spec/middleware/accept_json_spec.rb +10 -0
- data/spec/middleware/first_level_parse_json_spec.rb +42 -0
- data/spec/middleware/second_level_parse_json_spec.rb +25 -0
- data/spec/model/hooks_spec.rb +406 -0
- data/spec/model/http_spec.rb +184 -0
- data/spec/model/introspection_spec.rb +59 -0
- data/spec/model/orm_spec.rb +552 -0
- data/spec/model/paths_spec.rb +286 -0
- data/spec/model/relationships_spec.rb +222 -0
- data/spec/model_spec.rb +31 -0
- data/spec/spec_helper.rb +46 -0
- metadata +222 -0
data/lib/her/version.rb
ADDED
data/spec/api_spec.rb
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.join(File.dirname(__FILE__), "spec_helper.rb")
|
3
|
+
|
4
|
+
describe Her::API do
|
5
|
+
subject { Her::API.new }
|
6
|
+
|
7
|
+
context "initialization" do
|
8
|
+
describe ".setup" do
|
9
|
+
it "creates a default connection" do
|
10
|
+
Her::API.setup :url => "https://api.example.com"
|
11
|
+
Her::API.default_api.base_uri.should == "https://api.example.com"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#setup" do
|
16
|
+
context "when using :url option" do
|
17
|
+
before { subject.setup :url => "https://api.example.com" }
|
18
|
+
its(:base_uri) { should == "https://api.example.com" }
|
19
|
+
end
|
20
|
+
|
21
|
+
context "when using the legacy :base_uri option" do
|
22
|
+
before { subject.setup :base_uri => "https://api.example.com" }
|
23
|
+
its(:base_uri) { should == "https://api.example.com" }
|
24
|
+
end
|
25
|
+
|
26
|
+
context "when setting custom middleware" do
|
27
|
+
before do
|
28
|
+
class Foo; end;
|
29
|
+
class Bar; end;
|
30
|
+
|
31
|
+
subject.setup :url => "https://api.example.com" do |connection|
|
32
|
+
connection.use Foo
|
33
|
+
connection.use Bar
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
specify { subject.connection.builder.handlers.should == [Foo, Bar] }
|
38
|
+
end
|
39
|
+
|
40
|
+
context "when setting custom options" do
|
41
|
+
before { subject.setup :foo => { :bar => "baz" }, :url => "https://api.example.com" }
|
42
|
+
its(:options) { should == { :foo => { :bar => "baz" }, :url => "https://api.example.com" } }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "#request" do
|
47
|
+
before do
|
48
|
+
class SimpleParser < Faraday::Response::Middleware
|
49
|
+
def on_complete(env)
|
50
|
+
env[:body] = { :data => env[:body] }
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context "making HTTP requests" do
|
56
|
+
let(:parsed_data) { subject.request(:_method => :get, :_path => "/foo") }
|
57
|
+
before do
|
58
|
+
subject.setup :url => "https://api.example.com" do |builder|
|
59
|
+
builder.use SimpleParser
|
60
|
+
builder.adapter(:test) { |stub| stub.get("/foo") { |env| [200, {}, "Foo, it is."] } }
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
specify { parsed_data[:data].should == "Foo, it is." }
|
65
|
+
end
|
66
|
+
|
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 }) }
|
69
|
+
|
70
|
+
before do
|
71
|
+
subject.setup :url => "https://api.example.com" do |builder|
|
72
|
+
builder.use SimpleParser
|
73
|
+
builder.adapter(:test) { |stub| stub.get("/foo") { |env| [200, {}, "Foo, it is page #{env[:request_headers]["X-Page"]}."] } }
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
specify { parsed_data[:data].should == "Foo, it is page 2." }
|
78
|
+
end
|
79
|
+
|
80
|
+
context "parsing a request with the default parser" do
|
81
|
+
let(:parsed_data) { subject.request(:_method => :get, :_path => "users/1") }
|
82
|
+
before do
|
83
|
+
subject.setup :url => "https://api.example.com" do |builder|
|
84
|
+
builder.use Her::Middleware::FirstLevelParseJSON
|
85
|
+
builder.adapter :test do |stub|
|
86
|
+
stub.get("/users/1") { |env| [200, {}, MultiJson.dump({ :id => 1, :name => "George Michael Bluth", :errors => ["This is a single error"], :metadata => { :page => 1, :per_page => 10 } })] }
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
specify do
|
92
|
+
parsed_data[:data].should == { :id => 1, :name => "George Michael Bluth" }
|
93
|
+
parsed_data[:errors].should == ["This is a single error"]
|
94
|
+
parsed_data[:metadata].should == { :page => 1, :per_page => 10 }
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context "parsing a request with a custom parser" do
|
99
|
+
let(:parsed_data) { subject.request(:_method => :get, :_path => "users/1") }
|
100
|
+
before do
|
101
|
+
class CustomParser < Faraday::Response::Middleware
|
102
|
+
def on_complete(env)
|
103
|
+
json = MultiJson.load(env[:body], :symbolize_keys => true)
|
104
|
+
errors = json.delete(:errors) || []
|
105
|
+
metadata = json.delete(:metadata) || {}
|
106
|
+
env[:body] = {
|
107
|
+
:data => json,
|
108
|
+
:errors => errors,
|
109
|
+
:metadata => metadata,
|
110
|
+
}
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
subject.setup :url => "https://api.example.com" do |builder|
|
115
|
+
builder.use CustomParser
|
116
|
+
builder.use Faraday::Request::UrlEncoded
|
117
|
+
builder.adapter :test do |stub|
|
118
|
+
stub.get("/users/1") { |env| [200, {}, MultiJson.dump(:id => 1, :name => "George Michael Bluth")] }
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
specify do
|
124
|
+
parsed_data[:data].should == { :id => 1, :name => "George Michael Bluth" }
|
125
|
+
parsed_data[:errors].should == []
|
126
|
+
parsed_data[:metadata].should == {}
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Her::Collection do
|
4
|
+
|
5
|
+
let(:items) { [1, 2, 3, 4] }
|
6
|
+
let(:metadata) { { :name => 'Testname' } }
|
7
|
+
let(:errors) { { :name => ['not_present'] } }
|
8
|
+
|
9
|
+
describe "#new" do
|
10
|
+
context "without parameters" do
|
11
|
+
subject { Her::Collection.new }
|
12
|
+
|
13
|
+
it { should eq([]) }
|
14
|
+
its(:metadata) { should eq({}) }
|
15
|
+
its(:errors) { should eq({}) }
|
16
|
+
end
|
17
|
+
|
18
|
+
context "with parameters" do
|
19
|
+
subject { Her::Collection.new(items, metadata, errors) }
|
20
|
+
|
21
|
+
it { should eq([1,2,3,4]) }
|
22
|
+
its(:metadata) { should eq({ :name => 'Testname' }) }
|
23
|
+
its(:errors) { should eq({ :name => ['not_present'] }) }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "spec_helper"
|
3
|
+
|
4
|
+
describe Her::Middleware::FirstLevelParseJSON do
|
5
|
+
subject { described_class.new }
|
6
|
+
let(:body_without_errors) { "{\"id\": 1, \"name\": \"Tobias Fünke\", \"metadata\": 3}" }
|
7
|
+
let(:body_with_errors) { "{\"id\": 1, \"name\": \"Tobias Fünke\", \"errors\": { \"name\": [ \"not_valid\", \"should_be_present\" ] }, \"metadata\": 3}" }
|
8
|
+
|
9
|
+
it "parses body as json" do
|
10
|
+
subject.parse(body_without_errors).tap do |json|
|
11
|
+
json[:data].should == { :id => 1, :name => "Tobias Fünke" }
|
12
|
+
json[:metadata].should == 3
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it "parses :body key as json in the env hash" do
|
17
|
+
env = { :body => body_without_errors }
|
18
|
+
subject.on_complete(env)
|
19
|
+
env[:body].tap do |json|
|
20
|
+
json[:data].should == { :id => 1, :name => "Tobias Fünke" }
|
21
|
+
json[:metadata].should == 3
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'ensures the errors are a hash if there are no errors' do
|
26
|
+
subject.parse(body_without_errors)[:errors].should eq({})
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'ensures the errors are a hash if there are no errors' do
|
30
|
+
subject.parse(body_with_errors)[:errors].should eq({:name => [ 'not_valid', 'should_be_present']})
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'with status code 204' do
|
34
|
+
it 'returns an empty body' do
|
35
|
+
env = { :status => 204 }
|
36
|
+
subject.on_complete(env)
|
37
|
+
env[:body].tap do |json|
|
38
|
+
json[:data].should == { }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "spec_helper"
|
3
|
+
|
4
|
+
describe Her::Middleware::SecondLevelParseJSON do
|
5
|
+
subject { described_class.new }
|
6
|
+
let(:body) { "{\"data\": 1, \"errors\": 2, \"metadata\": 3}" }
|
7
|
+
|
8
|
+
it "parses body as json" do
|
9
|
+
subject.parse(body).tap do |json|
|
10
|
+
json[:data].should == 1
|
11
|
+
json[:errors].should == 2
|
12
|
+
json[:metadata].should == 3
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it "parses :body key as json in the env hash" do
|
17
|
+
env = { :body => body }
|
18
|
+
subject.on_complete(env)
|
19
|
+
env[:body].tap do |json|
|
20
|
+
json[:data].should == 1
|
21
|
+
json[:errors].should == 2
|
22
|
+
json[:metadata].should == 3
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,406 @@
|
|
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
|
+
}.to 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
|
+
}.to 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
|