sinatra_resource 0.2.0 → 0.2.1
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 +1 -1
- data/examples/datacatalog/models/category.rb +2 -1
- data/examples/datacatalog/resources/categories.rb +29 -0
- data/examples/datacatalog/test/resources/categories/categories_delete_test.rb +5 -0
- data/examples/datacatalog/test/resources/categories/categories_get_many_test.rb +1 -1
- data/examples/datacatalog/test/resources/categories/categories_get_one_test.rb +1 -1
- data/examples/datacatalog/test/resources/categories/categories_post_test.rb +6 -3
- data/examples/datacatalog/test/resources/categories/categories_put_test.rb +5 -1
- data/lib/builder/action_definitions.rb +10 -2
- data/lib/builder/helpers.rb +10 -0
- data/lib/resource.rb +28 -1
- data/sinatra_resource.gemspec +1 -1
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.1
|
@@ -14,6 +14,7 @@ module DataCatalog
|
|
14
14
|
# == Properties
|
15
15
|
|
16
16
|
property :name
|
17
|
+
property :log
|
17
18
|
|
18
19
|
property :sources do |category|
|
19
20
|
category.sources.map do |source|
|
@@ -25,6 +26,34 @@ module DataCatalog
|
|
25
26
|
}
|
26
27
|
end
|
27
28
|
end
|
29
|
+
|
30
|
+
# == Callbacks
|
31
|
+
|
32
|
+
callback :before_create do |action|
|
33
|
+
action.params["log"] = "before_create"
|
34
|
+
end
|
35
|
+
|
36
|
+
callback :after_create do |action, category|
|
37
|
+
category.log += " after_create"
|
38
|
+
end
|
39
|
+
|
40
|
+
callback :before_update do |action|
|
41
|
+
action.params["log"] = "before_update"
|
42
|
+
end
|
43
|
+
|
44
|
+
callback :after_update do |action, category|
|
45
|
+
category.log += " after_update"
|
46
|
+
end
|
47
|
+
|
48
|
+
callback :before_destroy do |action|
|
49
|
+
action.headers 'X-Test-Callbacks' => 'before_destroy'
|
50
|
+
end
|
51
|
+
|
52
|
+
callback :after_destroy do |action, category|
|
53
|
+
x = action.response['X-Test-Callbacks']
|
54
|
+
action.headers 'X-Test-Callbacks' => "#{x} after_destroy"
|
55
|
+
end
|
56
|
+
|
28
57
|
end
|
29
58
|
|
30
59
|
Categories.build
|
@@ -94,6 +94,11 @@ class CategoriesDeleteResourceTest < ResourceTestCase
|
|
94
94
|
|
95
95
|
use "return 204 No Content"
|
96
96
|
use "one less category"
|
97
|
+
|
98
|
+
test "callbacks should work" do
|
99
|
+
actual = last_response.headers['X-Test-Callbacks']
|
100
|
+
assert_equal "before_destroy after_destroy", actual
|
101
|
+
end
|
97
102
|
end
|
98
103
|
end
|
99
104
|
|
@@ -57,7 +57,7 @@ class CategoriesGetOneResourceTest < ResourceTestCase
|
|
57
57
|
end
|
58
58
|
|
59
59
|
use "return 200 Ok"
|
60
|
-
doc_properties %w(name id created_at updated_at sources)
|
60
|
+
doc_properties %w(name log id created_at updated_at sources)
|
61
61
|
|
62
62
|
test "body should have correct sources" do
|
63
63
|
expected = [
|
@@ -66,8 +66,7 @@ class CategoriesPostResourceTest < ResourceTestCase
|
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
69
|
-
%w(curator).each do |role|
|
70
|
-
# %w(curator admin).each do |role|
|
69
|
+
%w(curator admin).each do |role|
|
71
70
|
[:name].each do |missing|
|
72
71
|
context "#{role} : post / but missing #{missing}" do
|
73
72
|
before do
|
@@ -104,7 +103,7 @@ class CategoriesPostResourceTest < ResourceTestCase
|
|
104
103
|
use "return 201 Created"
|
105
104
|
location_header "categories"
|
106
105
|
use "one new category"
|
107
|
-
doc_properties %w(name id created_at updated_at sources)
|
106
|
+
doc_properties %w(name log id created_at updated_at sources)
|
108
107
|
|
109
108
|
test "should set all fields in database" do
|
110
109
|
category = Category.find_by_id(parsed_response_body["id"])
|
@@ -113,6 +112,10 @@ class CategoriesPostResourceTest < ResourceTestCase
|
|
113
112
|
assert_equal value, category[key]
|
114
113
|
end
|
115
114
|
end
|
115
|
+
|
116
|
+
test "callbacks should work" do
|
117
|
+
assert_equal "before_create after_create", parsed_response_body["log"]
|
118
|
+
end
|
116
119
|
end
|
117
120
|
end
|
118
121
|
|
@@ -120,7 +120,7 @@ class CategoriesPutResourceTest < ResourceTestCase
|
|
120
120
|
end
|
121
121
|
|
122
122
|
use "return 200 Ok"
|
123
|
-
doc_properties %w(name id created_at updated_at sources)
|
123
|
+
doc_properties %w(name log id created_at updated_at sources)
|
124
124
|
|
125
125
|
test "should change all fields in database" do
|
126
126
|
category = Category.find_by_id(@category.id)
|
@@ -128,6 +128,10 @@ class CategoriesPutResourceTest < ResourceTestCase
|
|
128
128
|
assert_equal value, category[key]
|
129
129
|
end
|
130
130
|
end
|
131
|
+
|
132
|
+
test "callbacks should work" do
|
133
|
+
assert_equal "before_update after_update", parsed_response_body["log"]
|
134
|
+
end
|
131
135
|
end
|
132
136
|
end
|
133
137
|
|
@@ -28,10 +28,12 @@ module SinatraResource
|
|
28
28
|
def document_for_post(role, model, resource_config, leaf, parent_document, association)
|
29
29
|
check_permission(:create, role, resource_config)
|
30
30
|
check_params(:create, role, resource_config, leaf)
|
31
|
+
do_callback(:before_create, resource_config, nil)
|
31
32
|
document = create_document!(model)
|
32
33
|
if resource_config[:parent]
|
33
34
|
make_related(parent_document, document, resource_config)
|
34
35
|
end
|
36
|
+
do_callback(:after_create, resource_config, document)
|
35
37
|
document
|
36
38
|
end
|
37
39
|
|
@@ -41,7 +43,10 @@ module SinatraResource
|
|
41
43
|
check_related?(parent_document, association, id)
|
42
44
|
end
|
43
45
|
check_params(:update, role, resource_config, leaf)
|
44
|
-
|
46
|
+
do_callback(:before_update, resource_config, nil)
|
47
|
+
document = update_document!(model, id)
|
48
|
+
do_callback(:after_update, resource_config, document)
|
49
|
+
document
|
45
50
|
end
|
46
51
|
|
47
52
|
def document_for_delete(role, model, resource_config, leaf, id, parent_document, association)
|
@@ -50,7 +55,10 @@ module SinatraResource
|
|
50
55
|
check_related?(parent_document, association, id)
|
51
56
|
end
|
52
57
|
check_params(:delete, role, resource_config, leaf)
|
53
|
-
|
58
|
+
do_callback(:before_destroy, resource_config, nil)
|
59
|
+
document = delete_document!(model, id)
|
60
|
+
do_callback(:after_destroy, resource_config, document)
|
61
|
+
document
|
54
62
|
end
|
55
63
|
|
56
64
|
end
|
data/lib/builder/helpers.rb
CHANGED
@@ -111,6 +111,16 @@ module SinatraResource
|
|
111
111
|
end
|
112
112
|
convert(object)
|
113
113
|
end
|
114
|
+
|
115
|
+
def do_callback(name, resource_config, document)
|
116
|
+
proc = resource_config[:callbacks][name]
|
117
|
+
return unless proc
|
118
|
+
if document
|
119
|
+
proc.call(self, document)
|
120
|
+
else
|
121
|
+
proc.call(self)
|
122
|
+
end
|
123
|
+
end
|
114
124
|
|
115
125
|
# Convert a path to a full URI.
|
116
126
|
#
|
data/lib/resource.rb
CHANGED
@@ -14,6 +14,22 @@ module SinatraResource
|
|
14
14
|
|
15
15
|
attr_reader :resource_config
|
16
16
|
|
17
|
+
# Specify a callback.
|
18
|
+
#
|
19
|
+
# @param [Symbol] method
|
20
|
+
# A symbol that refers to a method on the parent.
|
21
|
+
#
|
22
|
+
# @return [undefined]
|
23
|
+
def callback(name, &block)
|
24
|
+
unless @resource_config[:callbacks].include?(name)
|
25
|
+
raise DefinitionError, "callback #{name.inspect} is not supported #{self}"
|
26
|
+
end
|
27
|
+
if @resource_config[:callbacks][name]
|
28
|
+
raise DefinitionError, "callback #{name.inspect} already declared in #{self}"
|
29
|
+
end
|
30
|
+
@resource_config[:callbacks][name] = block
|
31
|
+
end
|
32
|
+
|
17
33
|
# Specify the association +method+ on +parent+ that points to the
|
18
34
|
# current (child) +model+.
|
19
35
|
#
|
@@ -188,13 +204,24 @@ module SinatraResource
|
|
188
204
|
# For internal use. Initializes internal data structure.
|
189
205
|
def setup
|
190
206
|
@resource_config = {
|
207
|
+
:callbacks => {
|
208
|
+
:before_create => nil,
|
209
|
+
:after_create => nil,
|
210
|
+
:before_update => nil,
|
211
|
+
:after_update => nil,
|
212
|
+
:before_destroy => nil,
|
213
|
+
:after_destroy => nil,
|
214
|
+
},
|
191
215
|
:child_association => nil,
|
192
216
|
:model => nil,
|
193
217
|
:parent => nil,
|
194
218
|
:path => nil, # default_path,
|
195
219
|
:permission => {},
|
196
220
|
:properties => {},
|
197
|
-
:relation => {
|
221
|
+
:relation => {
|
222
|
+
:create => nil,
|
223
|
+
:delete => nil,
|
224
|
+
},
|
198
225
|
:roles => nil,
|
199
226
|
}
|
200
227
|
end
|
data/sinatra_resource.gemspec
CHANGED