rack-backend-api 0.1.0 → 0.2.0
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 +7 -0
- data/lib/backend_api.rb +7 -7
- data/lib/sequel_rack_backend_api_adapter.rb +1 -0
- data/rack-backend-api.gemspec +1 -1
- data/test/db.rb +1 -0
- data/test/spec_backend_api.rb +20 -0
- data/test/spec_sequel_adapter.rb +4 -0
- metadata +4 -4
data/README.md
CHANGED
@@ -153,6 +153,11 @@ The option `:submit_text` is also available through the API as `_submit_text`.
|
|
153
153
|
It says "SAVE" by default but you might want it to say "CREATE" and "UPDATE" in appropriate cases,
|
154
154
|
like we did in the example.
|
155
155
|
|
156
|
+
Another thing to note is that you don't have to use a destination for when something is created or updated.
|
157
|
+
If you do not use destination, the API will call the instance method `Model#backend_show` on the entry.
|
158
|
+
By default it just says `'OK'` but you can override the method in order to send whatever you want.
|
159
|
+
This comes handy when you use ajax and want a representation of the entry once it's created.
|
160
|
+
|
156
161
|
SORTING
|
157
162
|
=======
|
158
163
|
|
@@ -259,6 +264,7 @@ Others are slightly more sophisticated:
|
|
259
264
|
- `Model#backend_form( action_url, columns=nil, options={} )` It is only the wrapping of the form without the actual fields. Try to implement it like the Sequel one.
|
260
265
|
- `Model#backend_fields( columns )` These are the actual fields. There is a default behaviour that basically puts a `textarea` for everything. That works in most cases but this is meant to be overridden for a better solution. We recommend [Crushyform](https://rubygems.org/gems/sequel-crushyform) for Sequel because we did it so we know it plays well with BackendAPI, and also because you don't have anything more to do. BackendAPI knows you have [Crushyform](https://rubygems.org/gems/sequel-crushyform) and use it to create the fields.
|
261
266
|
- `Model#backend_delete_form( action_url, options={})` Basically sugar for Model#backend_form but with an empty array for columns, and these options `{:submit_text=>'X', :method=>'DELETE'}` predefined which you can override. We've seen before that it is for creating DELETE forms.
|
267
|
+
- `Model#backend_show` What is sent when PUT or POST is successful and there is no `_destination`. Default is `'OK'`
|
262
268
|
- `Model::sort( array-of-ids )` It is used to do a bulk update of the position field, hence: re-order
|
263
269
|
|
264
270
|
CAN I HELP ?
|
@@ -285,6 +291,7 @@ CHANGE LOG
|
|
285
291
|
0.0.4 Partial available not only via XHR but also via `_no_wrap` param
|
286
292
|
0.0.5 Ordered list of fields to keep before validation
|
287
293
|
0.1.0 Introduce sorting functionality
|
294
|
+
0.2.0 Control what you send on 201 responses
|
288
295
|
|
289
296
|
COPYRIGHT
|
290
297
|
=========
|
data/lib/backend_api.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
class BackendAPI
|
2
|
-
VERSION = [0,
|
2
|
+
VERSION = [0,2,0]
|
3
3
|
WRAP = <<-EOT
|
4
4
|
<!doctype html>
|
5
5
|
<html>
|
@@ -52,7 +52,7 @@ class BackendAPI
|
|
52
52
|
@model_instance ||= @model_class.backend_post
|
53
53
|
@model_instance.backend_put @req['model']
|
54
54
|
form = @model_instance.backend_form(@req.path, @req['fields'], :destination => @req['_destination'], :submit_text => @req['_submit_text'], :no_wrap => @req['_no_wrap'])
|
55
|
-
@res.write(
|
55
|
+
@res.write(wrap_response(form))
|
56
56
|
end
|
57
57
|
|
58
58
|
# Update
|
@@ -100,23 +100,23 @@ class BackendAPI
|
|
100
100
|
def save_and_respond
|
101
101
|
if @model_instance.backend_save?
|
102
102
|
if @req['_destination'].nil?
|
103
|
-
@res.write
|
103
|
+
@res.write(wrap_response(@model_instance.backend_show))
|
104
104
|
@res.status=201 # Created
|
105
105
|
else
|
106
106
|
@res.redirect(::Rack::Utils::unescape(@req['_destination']))
|
107
107
|
end
|
108
108
|
else
|
109
109
|
form = @model_instance.backend_form(@req.path, @req['fields']||@req['model'].keys, :destination => @req['_destination'], :submit_text => @req['_submit_text'], :no_wrap => @req['_no_wrap'])
|
110
|
-
@res.write(
|
110
|
+
@res.write(wrap_response(form))
|
111
111
|
@res.status=400 # Bad Request
|
112
112
|
end
|
113
113
|
end
|
114
114
|
|
115
|
-
def
|
115
|
+
def wrap_response(content)
|
116
116
|
if @req['_no_wrap'] || @req.xhr?
|
117
|
-
|
117
|
+
content
|
118
118
|
else
|
119
|
-
WRAP % [@model_class_name,
|
119
|
+
WRAP % [@model_class_name, content]
|
120
120
|
end
|
121
121
|
end
|
122
122
|
|
data/rack-backend-api.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'rack-backend-api'
|
3
|
-
s.version = "0.
|
3
|
+
s.version = "0.2.0"
|
4
4
|
s.platform = Gem::Platform::RUBY
|
5
5
|
s.summary = "A Rack middleware that provides a simple API for your Admin section"
|
6
6
|
s.description = "The purpose of this Rack Middleware is to provide an API that interfaces with database actions in order to build a CMS."
|
data/test/db.rb
CHANGED
data/test/spec_backend_api.rb
CHANGED
@@ -95,6 +95,16 @@ describe 'API Post' do
|
|
95
95
|
res.status.should==201
|
96
96
|
end
|
97
97
|
|
98
|
+
should "Send the Model#backend_show on success" do
|
99
|
+
res = req_lint(BackendAPI.new).post('/haiku', :params => {'model' => {'body' => "..."}})
|
100
|
+
res.status.should==201
|
101
|
+
res.body.should.match(/Me, the Haiku/)
|
102
|
+
res.body.should.match(/doctype/)
|
103
|
+
res = req_lint(BackendAPI.new).post('/haiku', "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest", :params => {'model' => {'body' => "///"}})
|
104
|
+
res.status.should==201
|
105
|
+
res.body.should=='Me, the Haiku'
|
106
|
+
end
|
107
|
+
|
98
108
|
should "Send back the appropriate form when the creation is not valid" do
|
99
109
|
|
100
110
|
res = req_lint(BackendAPI.new).post('/haiku', :params => {'model' => {'title' => '13'}})
|
@@ -186,6 +196,16 @@ describe 'API Put' do
|
|
186
196
|
res.status.should==201
|
187
197
|
end
|
188
198
|
|
199
|
+
should "Send the Model#backend_show on success" do
|
200
|
+
res = req_lint(BackendAPI.new).put('/haiku/3', :params => {'model' => {'body' => "..."}})
|
201
|
+
res.status.should==201
|
202
|
+
res.body.should.match(/Me, the Haiku/)
|
203
|
+
res.body.should.match(/doctype/)
|
204
|
+
res = req_lint(BackendAPI.new).put('/haiku/3', "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest", :params => {'model' => {'body' => "///"}})
|
205
|
+
res.status.should==201
|
206
|
+
res.body.should=='Me, the Haiku'
|
207
|
+
end
|
208
|
+
|
189
209
|
should "Send back the appropriate form when the creation is not valid" do
|
190
210
|
|
191
211
|
res = req_lint(BackendAPI.new).put('/haiku/3', :params => {'model' => {'title' => '13'}})
|
data/test/spec_sequel_adapter.rb
CHANGED
@@ -87,4 +87,8 @@ describe 'Sequel Adapter' do
|
|
87
87
|
TopFive.order(:position).map(:flavour).should==['Vanilla','Chocolate','Strawberry','Apricot','Coconut']
|
88
88
|
end
|
89
89
|
|
90
|
+
should "Have an instance method called backend_show that says 'OK' by default" do
|
91
|
+
Haiku[1].backend_show.should=='OK'
|
92
|
+
end
|
93
|
+
|
90
94
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-backend-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Mickael Riga
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-07-
|
18
|
+
date: 2011-07-27 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|