rack-autocrud 0.1.15 → 0.1.16

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +48 -12
  2. data/lib/rack/autocrud.rb +25 -2
  3. metadata +2 -2
data/README.md CHANGED
@@ -74,7 +74,7 @@ option to this middleware.
74
74
  For example:
75
75
 
76
76
  ```ruby
77
- use Rack::AutoCRUD, :model_namespace => 'Models', :endpoint_namespace => 'Endpoints', :sinatra_opts => { :sessions => false }
77
+ use Rack::AutoCRUD, :model_namespace => 'Models', :endpoint_namespace => 'Endpoints', :sinatra_opts => { :sessions => true }
78
78
  ```
79
79
 
80
80
  Which can be extremely useful if you're handling sessions at a higher level than the
@@ -89,7 +89,7 @@ _config.ru_. This middleware will dynamically create a _Sinatra::Base_ subclass
89
89
 
90
90
  | Route | Action | HTTP Response Code |
91
91
  | ----------- | -------------------------------| ------------------ |
92
- | get / | List all _Person_ entries | 403 |
92
+ | get / | List all _Person_ entries | 200 / 403 |
93
93
  | post / | Create a new _Person_ | 201 / 402 |
94
94
  | get /:id | Retrieve a _Person_ | 200 |
95
95
  | put /:id | Update a _Person_ | 201 / 402 |
@@ -125,26 +125,62 @@ CRUD Processing Hooks
125
125
 
126
126
  There are some basic processing hooks you can define in your endpoint:
127
127
 
128
- | Hook | Description |
129
- | -------------------------------- | ---------------------------------------------------------------- |
130
- | pre_create(env,request,params) | Called before the record is created |
131
- | post_create(env,request,obj) | Called after the record is saved, if it was saved successfully |
132
- | pre_retrieve(env,request,params) | Called before the record is fetched |
133
- | post_retrieve(env,request,obj) | Called after the record is fetched |
134
- | pre_update(env,request,params) | Called before the record is updated |
135
- | post_update(env,request,params) | Called after the record is updated, if it was saved successfully |
136
- | pre_destroy(env,request,params) | Called before the record is destroyed |
137
- | post_destroy(env,request,obj) | Called after the record is destroyed |
128
+ | Hook | Description |
129
+ | --------------------------------------- | ---------------------------------------------------------------- |
130
+ | pre_create(env,request,params) | Called before the record is created |
131
+ | post_create(env,request,obj) | Called after the record is saved, if it was saved successfully |
132
+ | pre_retrieve(env,request,params) | Called before the record is fetched |
133
+ | post_retrieve(env,request,obj) | Called after the record is fetched |
134
+ | pre_update(env,request,params) | Called before the record is updated |
135
+ | post_update(env,request,params) | Called after the record is updated, if it was saved successfully |
136
+ | pre_destroy(env,request,params) | Called before the record is destroyed |
137
+ | post_destroy(env,request,obj) | Called after the record is destroyed |
138
+ | pre_collect(env,request,params) | Called before the record is collected |
139
+ | post_collect(env,request,collection) | Called after the record is collected |
140
+
138
141
 
139
142
  Parameters:
140
143
 
141
144
  * *env* is the current Rack environment
142
145
  * *request* is the current request object
143
146
  * *obj* is the ORM object corresponding to the record in question
147
+ * *collection* is the collection returned by the ORM
144
148
 
145
149
  If any of these hooks returns anything other than _nil_, it is assumed to be a response object, which
146
150
  is returned immediately, and no further processing is performed.
147
151
 
152
+ Collections
153
+ ===========
154
+
155
+ All models in the namespace are *not* collectable by default. To enable collections,
156
+ you need to set the _COLLECTABLE_ constant in the model:
157
+
158
+ ```ruby
159
+ module Models
160
+ class Person
161
+ include DataMapper::Resource
162
+
163
+ property :id, Serial
164
+ property :name, String
165
+
166
+ # Enable collection
167
+ COLLECTABLE = 1
168
+ end
169
+ end
170
+ ```
171
+
172
+ You can set this constant to 0 to disable it. If you want to enable collections
173
+ on all models in your namespace, simply specify the constant there:
174
+
175
+ If you want to *not* expose all models by default, simply define the constant as part of the _Models_ module:
176
+
177
+ ```ruby
178
+ module Models
179
+ # Enable collections on all models by default
180
+ COLLECTABLE = 1
181
+ end
182
+ ```
183
+
148
184
  Selective Exposure
149
185
  ==================
150
186
 
data/lib/rack/autocrud.rb CHANGED
@@ -64,6 +64,11 @@ module Rack
64
64
  model_klass.const_set(:EXPOSE,@model_mod.const_get(:EXPOSE))
65
65
  end
66
66
 
67
+ # Make sure we copy the :COLLECTABLE constant if it's defined upstream
68
+ if !model_klass.nil? && !model_klass.const_defined?(:COLLECTABLE) && @model_mod.const_defined?(:COLLECTABLE)
69
+ model_klass.const_set(:COLLECTABLE,@model_mod.const_get(:COLLECTABLE))
70
+ end
71
+
67
72
  # Now, if we've got something, do our magic.
68
73
  if !model_klass.nil? && (!model_klass.const_defined?(:EXPOSE) || model_klass.const_get(:EXPOSE))
69
74
  # If we don't have an endpoint class, make one
@@ -88,7 +93,24 @@ module Rack
88
93
  end
89
94
 
90
95
  get '/' do
91
- halt [ 403, '{ "error": "Access Denied" }' ]
96
+ halt [ 403, '{ "error": "Access Denied" }' ] unless model_klass.const_defined?(:COLLECTABLE) && model.const_get(:COLLECTABLE)
97
+
98
+ # Call the pre-create hook
99
+ if self.respond_to?(:pre_collect)
100
+ ret = pre_collect(env,request,params)
101
+ return ret unless ret.nil?
102
+ end
103
+
104
+ # Get the collection
105
+ collection = model.all
106
+
107
+ # Call the post-collect hook
108
+ if self.respond_to?(:post_collect)
109
+ ret = post_collect(env,request,collection)
110
+ return ret unless ret.nil?
111
+ end
112
+
113
+ collection.to_json
92
114
  end
93
115
 
94
116
  post '/' do
@@ -185,7 +207,8 @@ module Rack
185
207
  }
186
208
 
187
209
  # Now, call the endpoint class (assuming it will return a response)
188
- env['PATH_INFO'] = '/' + uri.join('/')
210
+ env['PATH_INFO'] = '/' + uri.join('/')
211
+ env['CONTENT_TYPE'] = 'text/json'
189
212
  return endpoint_klass.call(env)
190
213
  end
191
214
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-autocrud
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.15
4
+ version: 0.1.16
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-22 00:00:00.000000000 Z
12
+ date: 2013-01-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json