resourcerer 0.3.0 → 1.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9e93c37e2be8821f943067ec855db980a6f9d988
4
- data.tar.gz: 0ccf7f26495abab03c38fc3d6e02615c573262ed
3
+ metadata.gz: b06994a58d730b98de201b5c3da0223f380e2dd5
4
+ data.tar.gz: cef7fd6af87ee0d92761c70c22f7ce0796da21e1
5
5
  SHA512:
6
- metadata.gz: 37c61ffbf52613e9bcabe9267a8043195dfb6ba591967c547aafec9958fce936a3cfa80a20ed5b35f36418001b2fe7572df10270a075e7844504d141db1691a3
7
- data.tar.gz: f3c3b7e8d6c420891dd48b8e6185a804c13cb900a73046e688980eb78edf2fdcbd66d06777368f418434e4119085af23d538698e5278e81e5bdaf7c828649be9
6
+ metadata.gz: fde5d21d084804512b05fa28cfc7b09e59a54c34d97ac8f95502274b4e262a8b25264f832767ba1b2e63ce5c359e9e6c2d9415b50785b36b4141bffb9cfe8f6b
7
+ data.tar.gz: 670c8bf4194bf8e3482d1b0cb9c8a9a0acc8e5474a66a63f76bea8d59355826b1621169396ad31b67e134e16bbebc33899790584b036e790e1b222e1291f65af
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- Resourcerer
1
+ Resourcerer [![Gem Version](https://badge.fury.io/rb/resourcerer.svg)](http://badge.fury.io/rb/resourcerer) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/ElMassimo/queryable/blob/master/LICENSE.txt)
2
2
  =====================
3
3
  What `resourcerer` proposes is that you go from this:
4
4
 
@@ -29,7 +29,7 @@ class PersonController < ApplicationController
29
29
  render :edit
30
30
  end
31
31
  end
32
-
32
+
33
33
  private
34
34
  def person_params
35
35
  params.require(:person).permit(:name)
@@ -41,7 +41,9 @@ To something like this:
41
41
 
42
42
  ```ruby
43
43
  class PersonController < ApplicationController
44
- resource :person
44
+ resource :person do
45
+ permit [:name]
46
+ end
45
47
 
46
48
  def create
47
49
  if person.save
@@ -58,11 +60,6 @@ class PersonController < ApplicationController
58
60
  render :edit
59
61
  end
60
62
  end
61
-
62
- private
63
- def person_params
64
- params.require(:person).permit(:name)
65
- end
66
63
  end
67
64
  ```
68
65
 
@@ -103,6 +100,28 @@ resource :person
103
100
 
104
101
  ### Configuration
105
102
 
103
+ ### DSL
104
+ Resourcer also features a nice DSL, which is helpful when you need more control over the resource
105
+ lifecycle.
106
+
107
+ You can also access every configuration option available above:
108
+ ```ruby
109
+ resource(:employee) do
110
+ model :person
111
+ find_by :name
112
+ find {|name| company.find_employee(name) }
113
+ build { company.new_employee }
114
+ assign { params.require(:employee).permit(:name) }
115
+ permit [:name, :description]
116
+ end
117
+ # is the same as:
118
+ resource(:employee, model: :person, finder_attribute: :name, finder: ->(name){ company.find_employee(name) }, builder: ->{ company.new_employee }, attributes: ->{ params.require(:employee).permit(:name) })
119
+ ```
120
+ 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.
121
+
122
+ Configuration options play well together, and the defaults try to make intelligent use of them. For example,
123
+ setting the `finder_attribute` in the example above changes the `finder_param` to `person_name` instead of `person_id`, and the value of that parameter is provided to the finder block.
124
+
106
125
  Let's take a look at some of the things you can do:
107
126
 
108
127
  **Specify the model name:**
@@ -133,27 +152,6 @@ resource(:employee, attributes_method: :person_params)
133
152
  resource(:person, param_key: :employee)
134
153
  ```
135
154
 
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 above changes the `finder_param` to `person_name` instead of `person_id`, and the value of that parameter is provided to the finder block.
156
-
157
155
  ### Setting a distinct object for a single action
158
156
 
159
157
  There are times when one action in a controller is different from the
@@ -163,12 +161,9 @@ controller's setter methods. This example uses [presenter_rails](https://github.
163
161
  ```ruby
164
162
  resource(:article)
165
163
 
166
- def show_oldest
164
+ def oldest
167
165
  self.article = Article.find_oldest
168
- end
169
-
170
- present :article do
171
- ArticlePresenter.new(article)
166
+ render :show
172
167
  end
173
168
  ```
174
169
 
@@ -231,7 +226,7 @@ class PersonController < ApplicationController
231
226
  render :edit
232
227
  end
233
228
  end
234
-
229
+
235
230
  private
236
231
  def person_params
237
232
  params.require(:person).permit(:name)
@@ -243,8 +238,10 @@ To something like this by adding [presenter_rails](https://github.com/ElMassimo/
243
238
 
244
239
  ```ruby
245
240
  class PersonController < ApplicationController
246
- resource(:person)
247
-
241
+ resource :person do
242
+ permit :name
243
+ end
244
+
248
245
  present :person do
249
246
  person.decorate
250
247
  end
@@ -264,11 +261,6 @@ class PersonController < ApplicationController
264
261
  render :edit
265
262
  end
266
263
  end
267
-
268
- private
269
- def person_params
270
- params.require(:person).permit(:name)
271
- end
272
264
  end
273
265
  ```
274
266
 
@@ -280,7 +272,7 @@ Resourcerer is heavily inspired on [decent exposure](https://github.com/voxdolo/
280
272
  Both allow you to find or initialize a resource and assign attributes, removing the boilerplate from most CRUD actions.
281
273
 
282
274
  #### Differences
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.
275
+ 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. It also has better support for strong parameters.
284
276
 
285
277
 
286
278
  ### Caveats
@@ -37,5 +37,13 @@ module Resourcerer
37
37
  def assign(&block)
38
38
  options[:attributes] = block
39
39
  end
40
+
41
+ def collection(proc=nil, &block)
42
+ if proc = (proc || block)
43
+ options[:collection] = proc
44
+ else
45
+ options[:collection]
46
+ end
47
+ end
40
48
  end
41
49
  end
@@ -18,16 +18,12 @@ module Resourcerer
18
18
  @id ||= params[finder_param] || params[finder_attribute]
19
19
  end
20
20
 
21
- def finder_param
22
- config.finder_param || inflector.finder_param
23
- end
24
-
25
21
  def find_resource(id)
26
- controller_eval(config.finder, id) || model.find_by(finder_attribute => id)
22
+ controller_eval(config.finder, id) || collection.find_by(finder_attribute => id)
27
23
  end
28
24
 
29
25
  def build_resource
30
- controller_eval(config.builder) || model.new
26
+ controller_eval(config.builder) || collection.new
31
27
  end
32
28
 
33
29
  protected
@@ -35,6 +31,10 @@ module Resourcerer
35
31
  def controller_eval(proc, *args)
36
32
  controller.instance_exec(*args, &proc) if proc
37
33
  end
34
+
35
+ def collection
36
+ controller_eval(config.collection) || model
37
+ end
38
38
  end
39
39
  end
40
40
  end
@@ -44,6 +44,12 @@ module Resourcerer
44
44
  config.find_by || :id
45
45
  end
46
46
 
47
+ def finder_param
48
+ config.finder_param ||
49
+ (config.param_key && "#{config.param_key}_#{finder_attribute}") ||
50
+ inflector.finder_param
51
+ end
52
+
47
53
  def params
48
54
  controller.params
49
55
  end
metadata CHANGED
@@ -1,41 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resourcerer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Máximo Mussini
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-12 00:00:00.000000000 Z
11
+ date: 2014-12-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: pakiderm
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - '>='
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  description: Define resources to automate finding a record and assigning attributes.
@@ -67,17 +67,17 @@ licenses:
67
67
  metadata: {}
68
68
  post_install_message:
69
69
  rdoc_options:
70
- - "--charset=UTF-8"
70
+ - --charset=UTF-8
71
71
  require_paths:
72
72
  - lib
73
73
  required_ruby_version: !ruby/object:Gem::Requirement
74
74
  requirements:
75
- - - ">="
75
+ - - '>='
76
76
  - !ruby/object:Gem::Version
77
77
  version: '2.0'
78
78
  required_rubygems_version: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ">="
80
+ - - '>='
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  requirements: []