resourcerer 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +41 -5
- data/lib/resourcerer/strategy.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 55448f1a54313470f4c84d50243ad79ce0f13750
|
4
|
+
data.tar.gz: 30de7964ebdff67394987395145c00e156cdafe9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0201a4f045934bd7b2bfc9fd7ca32b324386d618c84ee9da16963d73002472d801c7487972151a29d856c3f926725748f05a08e05844819cd29fec7c736ee4d5
|
7
|
+
data.tar.gz: b06fb4cbbc6216be391e6bf9af24019596a4af8bd0459fd30f2e3178c6d99be48743ee17474b0497f4c5f2e773686a918fe9f504f43fb1d5fa1f1186a0b3f593
|
data/README.md
CHANGED
@@ -3,7 +3,7 @@ Resourcerer
|
|
3
3
|
What `resourcerer` proposes is that you go from this:
|
4
4
|
|
5
5
|
```ruby
|
6
|
-
class
|
6
|
+
class PersonController < ApplicationController
|
7
7
|
def new
|
8
8
|
@person = Person.new
|
9
9
|
end
|
@@ -40,7 +40,7 @@ end
|
|
40
40
|
To something like this:
|
41
41
|
|
42
42
|
```ruby
|
43
|
-
class
|
43
|
+
class PersonController < ApplicationController
|
44
44
|
resource :person
|
45
45
|
|
46
46
|
def create
|
@@ -70,7 +70,9 @@ The idea is that you don't have to write boilerplate for standard CRUD actions,
|
|
70
70
|
|
71
71
|
## Usage
|
72
72
|
|
73
|
-
Let's see what Resourcerer is doing behind the curtains :smiley:.
|
73
|
+
Let's see what Resourcerer is doing behind the curtains :smiley:.
|
74
|
+
|
75
|
+
This examples assume that you are using Rails 4 Strong Parameters.
|
74
76
|
|
75
77
|
### Obtaining a resource:
|
76
78
|
|
@@ -115,6 +117,12 @@ resource(:company, model: :enterprise)
|
|
115
117
|
resource(:enterprise, finder_param: :company_id)
|
116
118
|
```
|
117
119
|
|
120
|
+
**Specify the model attribute to use to perform the search:**
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
resource(:enterprise, find_by: :name)
|
124
|
+
```
|
125
|
+
|
118
126
|
**Specify how to obtain the object attributes:**
|
119
127
|
|
120
128
|
```ruby
|
@@ -125,6 +133,27 @@ resource(:employee, attributes_method: :person_params)
|
|
125
133
|
resource(:person, param_key: :employee)
|
126
134
|
```
|
127
135
|
|
136
|
+
### DSL
|
137
|
+
Resourcer also features a nice DSL, which is helpful when you need more control over the resource
|
138
|
+
lifecycle.
|
139
|
+
|
140
|
+
You can also access every configuration option available above:
|
141
|
+
```ruby
|
142
|
+
resource(:employee) do
|
143
|
+
model :person
|
144
|
+
find_by :name
|
145
|
+
find {|name| company.find_employee(name) }
|
146
|
+
build { company.new_employee }
|
147
|
+
assign { params.require(:employee).permit(:name) }
|
148
|
+
end
|
149
|
+
# is the same as:
|
150
|
+
resource(:employee, model: :person, finder_attribute: :name, finder: ->(name){ company.find_employee(name) }, builder: ->{ company.new_employee }, attributes: ->{ params.require(:employee).permit(:name) })
|
151
|
+
```
|
152
|
+
The DSL is more convenient when you have an object oriented design and want to allow an object to handle its collections, or as a quick way to set the StrongParameters method.
|
153
|
+
|
154
|
+
Configuration options play well together, and the defaults try to make intelligent use of them. For example,
|
155
|
+
setting the `finder_attribute` in the example abovie changes the `finder_param` to `person_name` instead of `person_id`, and the value of that parameter is provided to the finder block.
|
156
|
+
|
128
157
|
### Setting a distinct object for a single action
|
129
158
|
|
130
159
|
There are times when one action in a controller is different from the
|
@@ -174,7 +203,7 @@ resource(:post, strategy: VerifiableStrategy)
|
|
174
203
|
If you use decorators, you can go from something like this:
|
175
204
|
|
176
205
|
```ruby
|
177
|
-
class
|
206
|
+
class PersonController < ApplicationController
|
178
207
|
def new
|
179
208
|
@person = Person.new.decorate
|
180
209
|
end
|
@@ -213,7 +242,7 @@ end
|
|
213
242
|
To something like this by adding [presenter_rails](https://github.com/ElMassimo/presenter_rails) to the mix:
|
214
243
|
|
215
244
|
```ruby
|
216
|
-
class
|
245
|
+
class PersonController < ApplicationController
|
217
246
|
resource(:person)
|
218
247
|
|
219
248
|
present :person do
|
@@ -254,5 +283,12 @@ Both allow you to find or initialize a resource and assign attributes, removing
|
|
254
283
|
Resourcerer does not expose an object to the view in any way, scope the query to a collection method if defined, nor deal with collections.
|
255
284
|
|
256
285
|
|
286
|
+
### Caveats
|
287
|
+
#### When using StrongParametersStrategy
|
288
|
+
Since attributes are assigned on every POST, PUT, and PATCH request, sometimes when using Strong Parameters it's not desirable that the attributes method is called. For that reason, the presence of `params[param_key]` is checked before assigning attributes.
|
289
|
+
##### Troubleshooting
|
290
|
+
- _The attributes are not being assigned_: Check that the resource name matches the param used in the attributes method, and set the `param_key` configuration if they are different.
|
291
|
+
- _Need an error to be thrown if the params are not present_: Use the `EagerStrongParametersStrategy`, available in the sample strategies in this repository, you can set it using the `strategy` configuration option.
|
292
|
+
|
257
293
|
### Special Thanks
|
258
294
|
Resourcerer was inspired by [decent_exposure](https://github.com/voxdolo/decent_exposure).
|
data/lib/resourcerer/strategy.rb
CHANGED