rack-backend-api 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -153,6 +153,30 @@ 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
+ SORTING
157
+ =======
158
+
159
+ The way I implemented it for the moment might be a bit awkward, but I needed that option.
160
+ Basically when you use a PUT request without an ID, the API assumes that you want to sort the entries of the class.
161
+ For instance:
162
+
163
+ PUT /admin/TopFive
164
+
165
+ Then it will look for the parameter that has the name of the class which should be a list of ids.
166
+ And it will update each entry with its position field set to the position in the array.
167
+ That is more or less what people are used to do when sorting via javascript.
168
+
169
+ TopFive[]=4&TopFive[]=2&TopFive[]=1&TopFive[]=3
170
+
171
+ This is an example of a query string for the order: 4,2,1,3. But this is just an example, it has to be a PUT request
172
+ or a POST request with method override.
173
+
174
+ The position field is obviously known by the ORM.
175
+ For Sequel (which adapter is included) it assumes that you use the `:list` plugin.
176
+ It is shipped with Sequel.
177
+
178
+ Still a work in progress but it satisfies the tests so far.
179
+
156
180
  A LITTLE BIT OF JAVASCRIPT
157
181
  ==========================
158
182
 
@@ -235,6 +259,14 @@ Others are slightly more sophisticated:
235
259
  - `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.
236
260
  - `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.
237
261
  - `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.
262
+ - `Model::sort( array-of-ids )` It is used to do a bulk update of the position field, hence: re-order
263
+
264
+ CAN I HELP ?
265
+ ============
266
+
267
+ Of course you can. This project is still in heavy development and it definitely lacks some error checking and stuff.
268
+ So do not hesitate to fork the project on Github and request a `pull`. If you don't use Github, you still can send a patch.
269
+ But that's less fun isn't it?
238
270
 
239
271
  THANX
240
272
  =====
@@ -251,6 +283,8 @@ CHANGE LOG
251
283
  0.0.2 Accept CamelCased class names
252
284
  0.0.3 Fix Form mockup
253
285
  0.0.4 Partial available not only via XHR but also via `_no_wrap` param
286
+ 0.0.5 Ordered list of fields to keep before validation
287
+ 0.1.0 Introduce sorting functionality
254
288
 
255
289
  COPYRIGHT
256
290
  =========
data/lib/backend_api.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  class BackendAPI
2
- VERSION = [0,0,5]
2
+ VERSION = [0,1,0]
3
3
  WRAP = <<-EOT
4
4
  <!doctype html>
5
5
  <html>
@@ -57,8 +57,12 @@ class BackendAPI
57
57
 
58
58
  # Update
59
59
  def put
60
- @model_instance.backend_put @req['model']
61
- save_and_respond
60
+ if @id.nil? && @req[@model_class_name]
61
+ @model_class.sort(@req[@model_class_name])
62
+ else
63
+ @model_instance.backend_put @req['model']
64
+ save_and_respond
65
+ end
62
66
  end
63
67
 
64
68
  # Delete
@@ -1,6 +1,11 @@
1
1
  module ::Sequel::Plugins::RackBackendApiAdapter
2
2
 
3
3
  module ClassMethods
4
+ def sort(list)
5
+ list.each_with_index do |id, position|
6
+ self[id].update(position_field=>position)
7
+ end
8
+ end
4
9
  end
5
10
 
6
11
  module InstanceMethods
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'rack-backend-api'
3
- s.version = "0.0.5"
3
+ s.version = "0.1.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
@@ -49,6 +49,16 @@ class CamelCasedClass < ::Sequel::Model
49
49
  create_table unless table_exists?
50
50
  end
51
51
 
52
+ class TopFive < ::Sequel::Model
53
+ set_schema do
54
+ primary_key :id
55
+ Fixnum :position
56
+ String :flavour
57
+ end
58
+ create_table unless table_exists?
59
+ plugin :list
60
+ end
61
+
52
62
  Haiku.create( :title=>'Autumn', :body=>"Rust the ground\nFlush the branches\nReveal the trees" )
53
63
  Haiku.create( :title=>'Winter', :body=>"There is snow\nIt covers you\nBut you are still the most beautiful" )
54
64
  Haiku.create( :title=>'Spring', :body=>"No inspiration" )
@@ -56,3 +66,9 @@ Haiku.create( :title=>'Spring', :body=>"No inspiration" )
56
66
  Author.create(:name=>'Ray',:surname=>'Bradbury')
57
67
  Author.create(:name=>'Jorge Luis',:surname=>'Borges')
58
68
  Author.create(:name=>'Yasunari', :surname=>'Kawabata')
69
+
70
+ TopFive.create(:flavour=>'Strawberry')
71
+ TopFive.create(:flavour=>'Vanilla')
72
+ TopFive.create(:flavour=>'Chocolate')
73
+ TopFive.create(:flavour=>'Coconut')
74
+ TopFive.create(:flavour=>'Apricot')
@@ -227,6 +227,11 @@ describe 'API Put' do
227
227
  res.body.should==('<!-- '+compared.backend_form('/haiku/3',['title'], {:no_wrap=>'true'})+' -->')
228
228
  res.body.should.match(/name='_no_wrap'.*value='true'/)
229
229
  end
230
+
231
+ should "Consider that a PUT request without an ID is a bulk update of position field (re-order)" do
232
+ req_lint(BackendAPI.new).put('/TopFive', :params => {'TopFive'=>['2','3','1','5','4']})
233
+ TopFive.order(:position).map(:flavour).should==['Vanilla','Chocolate','Strawberry','Apricot','Coconut']
234
+ end
230
235
  end
231
236
 
232
237
  describe 'API Delete' do
@@ -82,4 +82,9 @@ describe 'Sequel Adapter' do
82
82
  form.scan(/input/).size.should==4
83
83
  end
84
84
 
85
+ should "Be able to sort entries with a list of IDs" do
86
+ TopFive.sort([2,3,1,5,4])
87
+ TopFive.order(:position).map(:flavour).should==['Vanilla','Chocolate','Strawberry','Apricot','Coconut']
88
+ end
89
+
85
90
  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: 21
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 5
10
- version: 0.0.5
10
+ version: 0.1.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-22 00:00:00 +01:00
18
+ date: 2011-07-25 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies: []
21
21