rack-autocrud 0.1.6 → 0.1.7
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/README.md +23 -11
- data/lib/rack/autocrud.rb +14 -10
- metadata +1 -1
data/README.md
CHANGED
@@ -63,7 +63,7 @@ _config.ru_. This middleware will dynamically create a _Sinatra::Base_ subclass
|
|
63
63
|
| Route | Action | HTTP Response Code |
|
64
64
|
| ----------- | -------------------------------| ------------------ |
|
65
65
|
| get / | List all _Person_ entries | 403 |
|
66
|
-
| post / | Create a new _Person_ | 201 /
|
66
|
+
| post / | Create a new _Person_ | 201 / 403 |
|
67
67
|
| get /:id | Retrieve a _Person_ | 200 |
|
68
68
|
| put /:id | Update a _Person_ | 201 / 403 |
|
69
69
|
| delete /:id | Destroy a _Person_ | 204 |
|
@@ -98,16 +98,16 @@ CRUD Processing Hooks
|
|
98
98
|
|
99
99
|
There are some basic processing hooks you can define in your endpoint:
|
100
100
|
|
101
|
-
| Hook
|
102
|
-
|
|
103
|
-
| pre_create(env,request)
|
104
|
-
| post_create(env,request,obj)
|
105
|
-
| pre_retrieve(env,request)
|
106
|
-
| post_retrieve(env,request,obj)
|
107
|
-
| pre_update(env,request)
|
108
|
-
| post_update(env,request)
|
109
|
-
| pre_destroy(env,request)
|
110
|
-
| post_destroy(env,request,obj)
|
101
|
+
| Hook | Description |
|
102
|
+
| -------------------------------- | ---------------------------------------------------------------- |
|
103
|
+
| pre_create(env,request,params) | Called before the record is created |
|
104
|
+
| post_create(env,request,obj) | Called after the record is saved, if it was saved successfully |
|
105
|
+
| pre_retrieve(env,request,params) | Called before the record is fetched |
|
106
|
+
| post_retrieve(env,request,obj) | Called after the record is fetched |
|
107
|
+
| pre_update(env,request,params) | Called before the record is updated |
|
108
|
+
| post_update(env,request,params) | Called after the record is updated, if it was saved successfully |
|
109
|
+
| pre_destroy(env,request,params) | Called before the record is destroyed |
|
110
|
+
| post_destroy(env,request,obj) | Called after the record is destroyed |
|
111
111
|
|
112
112
|
Parameters:
|
113
113
|
|
@@ -132,3 +132,15 @@ def set_request_body(new_body,content_type='text/json')
|
|
132
132
|
|
133
133
|
where *new_body* is expected to be a string.
|
134
134
|
|
135
|
+
Auto-Inclusion of Other Modules
|
136
|
+
===============================
|
137
|
+
|
138
|
+
This middleware also takes an option _:includes_ which can be used to automatically
|
139
|
+
include other modules (e.g. helpers) when creating and/or patching endpoints.
|
140
|
+
|
141
|
+
For example:
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
use Rack::AutoCRUD, :model_namespace => 'Models', :endpoint_namespace => 'Endpoints', :includes => [ Your::HelperModule ]
|
145
|
+
```
|
146
|
+
|
data/lib/rack/autocrud.rb
CHANGED
@@ -20,6 +20,7 @@ module Rack
|
|
20
20
|
@app = app
|
21
21
|
@model_namespace = options[:model_namespace]
|
22
22
|
@endpoint_namespace = options[:endpoint_namespace]
|
23
|
+
@includes = options[:includes]
|
23
24
|
@endpoint_mod = nil
|
24
25
|
end
|
25
26
|
|
@@ -57,6 +58,9 @@ module Rack
|
|
57
58
|
@endpoint_mod.const_set(endpoint.capitalize,endpoint_klass)
|
58
59
|
end
|
59
60
|
|
61
|
+
# Add in any specified helpers
|
62
|
+
@includes.each { |inc| endpoint_klass.send(:include,inc) } unless @includes.nil?
|
63
|
+
|
60
64
|
# Patch in the routes
|
61
65
|
endpoint_klass.class_exec(model_klass,endpoint,env) { |model,endpoint,env|
|
62
66
|
def set_request_body(new_body,content_type='text/json')
|
@@ -67,13 +71,13 @@ module Rack
|
|
67
71
|
end
|
68
72
|
|
69
73
|
get '/' do
|
70
|
-
halt [403, '{ "error": "Access Denied" }']
|
74
|
+
halt [ 403, '{ "error": "Access Denied" }' ]
|
71
75
|
end
|
72
76
|
|
73
77
|
post '/' do
|
74
78
|
# Call the pre-create hook
|
75
79
|
if self.respond_to?(:pre_create)
|
76
|
-
ret = pre_create(env,request)
|
80
|
+
ret = pre_create(env,request,params)
|
77
81
|
return ret unless ret.nil?
|
78
82
|
end
|
79
83
|
|
@@ -84,7 +88,7 @@ module Rack
|
|
84
88
|
obj = nil
|
85
89
|
begin
|
86
90
|
obj = model.create(JSON.parse(request.body.read))
|
87
|
-
halt [
|
91
|
+
halt [ 403, '{ "error": "Failed to save ' + endpoint + '" }' ] unless obj && obj.saved?
|
88
92
|
rescue JSON::ParserError
|
89
93
|
halt [ 400, { 'error' => 'Invalid JSON in request body.', 'details' => $! }.to_json ]
|
90
94
|
end
|
@@ -101,7 +105,7 @@ module Rack
|
|
101
105
|
get '/:id' do
|
102
106
|
# Call the pre-retrieve hook
|
103
107
|
if self.respond_to?(:pre_retrieve)
|
104
|
-
ret = pre_retrieve(env,request)
|
108
|
+
ret = pre_retrieve(env,request,params)
|
105
109
|
return ret unless ret.nil?
|
106
110
|
end
|
107
111
|
|
@@ -119,7 +123,7 @@ module Rack
|
|
119
123
|
put '/:id' do
|
120
124
|
# Call the pre-update hook
|
121
125
|
if self.respond_to?(:pre_update)
|
122
|
-
ret = pre_update(env,request)
|
126
|
+
ret = pre_update(env,request,params)
|
123
127
|
return ret unless ret.nil?
|
124
128
|
end
|
125
129
|
|
@@ -128,15 +132,15 @@ module Rack
|
|
128
132
|
|
129
133
|
# Attempt to update the model
|
130
134
|
begin
|
131
|
-
saved = model.update(JSON.parse(request.body.read))
|
132
|
-
halt [403, 'Access Denied'] unless saved
|
135
|
+
saved = model.update(JSON.parse(request.body.read).merge(:id => params[:id]))
|
136
|
+
halt [ 403, '{ "error": "Access Denied" }' ] unless saved
|
133
137
|
rescue JSON::ParserError
|
134
138
|
halt [ 400, { 'error' => 'Invalid JSON in request body.', 'details' => $! }.to_json ]
|
135
139
|
end
|
136
140
|
|
137
141
|
# Call the post-update hook
|
138
142
|
if self.respond_to?(:post_update)
|
139
|
-
ret = post_update(env,request)
|
143
|
+
ret = post_update(env,request,params)
|
140
144
|
return ret unless ret.nil?
|
141
145
|
end
|
142
146
|
|
@@ -146,12 +150,12 @@ module Rack
|
|
146
150
|
delete '/:id' do
|
147
151
|
# Call the pre-destroy hook
|
148
152
|
if self.respond_to?(:pre_destroy)
|
149
|
-
ret = pre_destroy(env,request)
|
153
|
+
ret = pre_destroy(env,request,params)
|
150
154
|
return ret unless ret.nil?
|
151
155
|
end
|
152
156
|
|
153
157
|
obj = model.get(params[:id])
|
154
|
-
obj.destroy if obj
|
158
|
+
obj.destroy! if obj
|
155
159
|
|
156
160
|
# Call the post-destroy hook
|
157
161
|
if self.respond_to?(:post_destroy)
|