resources 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +57 -37
- data/lib/resources/manager.rb +22 -14
- data/lib/resources/rest_actions.rb +11 -11
- data/lib/resources/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bf722e722d2f471c57224f21a678dd0a054bf163
|
4
|
+
data.tar.gz: b029230fa7ccb38892f8d2f1d1577633ae027b48
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e177b3c014074f81c74f6b27597a7b539c1cb0373f2e16ae45cdef394644f19125c20f24c87b4f7327277160e6abfd92b1d1c3b8d7db600c86635ef62215979
|
7
|
+
data.tar.gz: 95e8cbacafed2eaa4f97e8d5e60651a9650c8dd6449cdaea6a666c73f1e9a9b3f4de305996f4f92eb788d505bc90a1b5d462df089cf53428bc81889cedc7d5aa
|
data/README.md
CHANGED
@@ -34,12 +34,12 @@ Rub the configuration generator, this will create the **resources initializer**
|
|
34
34
|
```
|
35
35
|
rails g resources:install
|
36
36
|
```
|
37
|
-
|
37
|
+
|
38
38
|
----------
|
39
39
|
|
40
40
|
USAGE
|
41
41
|
-------------
|
42
|
-
|
42
|
+
|
43
43
|
Just add `resource_for` to your controller and give the model's name
|
44
44
|
|
45
45
|
```
|
@@ -60,7 +60,7 @@ And that's it, you have to do anything else on the controller to create a **CRUD
|
|
60
60
|
|
61
61
|
**`resources`:** this will have a collection of `Country`, objects so you can use on the `index` actions or any other collection route actions of your controller
|
62
62
|
|
63
|
-
**`resource`:** this will have a `Country`, object so you can use on the member route actions like `new, create, edit, update, show, destroy`
|
63
|
+
**`resource`:** this will have a `Country`, object so you can use on the member route actions like `new, create, edit, update, show, destroy`
|
64
64
|
|
65
65
|
**`resources_search`:** this is a **[ransack](https://github.com/activerecord-hackery/ransack)** object to you in the `search_form_for` helper
|
66
66
|
|
@@ -84,23 +84,46 @@ And that's it, you have to do anything else on the controller to create a **CRUD
|
|
84
84
|
|
85
85
|
|
86
86
|
|
87
|
-
#### **RESOURCE PARAMS - `params_resource`**
|
87
|
+
#### **RESOURCE PARAMS - `params_resource`**
|
88
88
|
|
89
|
-
This options allows you to change the default params key used for create or update the record. So you have to add the `as: :resource` to the `simple_form_for
|
89
|
+
This options allows you to change the default params key used for create or update the record. So you don't have to add the `as: :resource` to the `simple_form_for`.
|
90
90
|
|
91
91
|
```
|
92
92
|
class Admin::CountriesController < ApplicationController
|
93
|
-
resource_for :"Country",
|
94
|
-
params_resource: :country
|
93
|
+
resource_for :"Country",
|
94
|
+
params_resource: :country
|
95
|
+
end
|
96
|
+
|
97
|
+
# For rails 4
|
98
|
+
|
99
|
+
class Admin::CountriesController < ApplicationController
|
100
|
+
resource_for :"Country",
|
101
|
+
params_resource: :country_params
|
102
|
+
|
103
|
+
private
|
104
|
+
|
105
|
+
def country_params
|
106
|
+
params.require(:country).permit(:name)
|
107
|
+
end
|
95
108
|
end
|
96
109
|
```
|
110
|
+
**Note:** For Rails 4 the `params_resource: :country` as symbol it will call a method on the controller instead the key of the params hash `params[:country]`
|
97
111
|
|
98
112
|
Or you can use the `lambda` syntax.
|
99
113
|
|
100
114
|
```
|
101
115
|
class Admin::CountriesController < ApplicationController
|
102
|
-
resource_for :"Country",
|
103
|
-
params_resource: lambda{ |params| params[:country] }
|
116
|
+
resource_for :"Country",
|
117
|
+
params_resource: lambda{ |params| params[:country] }
|
118
|
+
end
|
119
|
+
|
120
|
+
# For rails 4
|
121
|
+
|
122
|
+
class Admin::CountriesController < ApplicationController
|
123
|
+
resource_for :"Country",
|
124
|
+
params_resource: lambda{ |params|
|
125
|
+
params.require(:country).permit(:name)
|
126
|
+
}
|
104
127
|
end
|
105
128
|
```
|
106
129
|
|
@@ -110,12 +133,12 @@ This options allows you to alias the `resource` method in case you don't like th
|
|
110
133
|
|
111
134
|
```
|
112
135
|
class Admin::CountriesController < ApplicationController
|
113
|
-
resource_for :"Country",
|
136
|
+
resource_for :"Country",
|
114
137
|
params_resource: :country,
|
115
|
-
resource_method_name: :country
|
138
|
+
resource_method_name: :country
|
116
139
|
end
|
117
140
|
```
|
118
|
-
**NOTE:** making a change in `resource_method_name` and `resources_method_name` options requires a **server restart**.
|
141
|
+
**NOTE:** making a change in `resource_method_name` and `resources_method_name` options requires a **server restart**.
|
119
142
|
|
120
143
|
Now in your view you can use `country` instead of `resource`
|
121
144
|
|
@@ -134,10 +157,10 @@ This option enable pagination in case that you use any of the most popular pagin
|
|
134
157
|
|
135
158
|
```
|
136
159
|
class Admin::CountriesController < ApplicationController
|
137
|
-
resource_for :"Country",
|
138
|
-
pagination: true,
|
160
|
+
resource_for :"Country",
|
161
|
+
pagination: true,
|
139
162
|
params_resource: :country,
|
140
|
-
resource_method_name: :country
|
163
|
+
resource_method_name: :country
|
141
164
|
end
|
142
165
|
```
|
143
166
|
|
@@ -145,10 +168,10 @@ Or you can use the `lambda` syntax.
|
|
145
168
|
|
146
169
|
```
|
147
170
|
class Admin::CountriesController < ApplicationController
|
148
|
-
resource_for :"Country",
|
149
|
-
pagination: lambda{ |params, controller| params[:disable_pagination].blank? },
|
171
|
+
resource_for :"Country",
|
172
|
+
pagination: lambda{ |params, controller| params[:disable_pagination].blank? },
|
150
173
|
params_resource: :country,
|
151
|
-
resource_method_name: :country
|
174
|
+
resource_method_name: :country
|
152
175
|
end
|
153
176
|
```
|
154
177
|
|
@@ -159,11 +182,11 @@ This options are to set a default `scope` for the `resources`, and `resource` ob
|
|
159
182
|
|
160
183
|
```
|
161
184
|
class Admin::CountriesController < ApplicationController
|
162
|
-
resource_for :"Country",
|
185
|
+
resource_for :"Country",
|
163
186
|
resources_scope: :active,
|
164
|
-
pagination: true,
|
187
|
+
pagination: true,
|
165
188
|
params_resource: :country,
|
166
|
-
resource_method_name: :country
|
189
|
+
resource_method_name: :country
|
167
190
|
end
|
168
191
|
```
|
169
192
|
**Note:** this will execute the scope `active` and get you only the countries that are currently actives.
|
@@ -172,13 +195,13 @@ You can use the `lambda` syntax for a **more complex scope**.
|
|
172
195
|
|
173
196
|
```
|
174
197
|
class Admin::CountriesController < ApplicationController
|
175
|
-
resource_for :"Country",
|
198
|
+
resource_for :"Country",
|
176
199
|
resources_scope: lambda{ |scope, params, controller|
|
177
200
|
scope.by_active_state(params[:active_state]).includes(:cities)
|
178
201
|
},
|
179
|
-
pagination: true,
|
202
|
+
pagination: true,
|
180
203
|
params_resource: :country,
|
181
|
-
resource_method_name: :country
|
204
|
+
resource_method_name: :country
|
182
205
|
end
|
183
206
|
```
|
184
207
|
|
@@ -191,21 +214,21 @@ The `search` option enable [ransack](https://github.com/activerecord-hackery/ran
|
|
191
214
|
```
|
192
215
|
class Admin::CountriesController < ApplicationController
|
193
216
|
resource_for :"Country",
|
194
|
-
search: true,
|
195
|
-
search_options: {distinct: true},
|
217
|
+
search: true,
|
218
|
+
search_options: {distinct: true},
|
196
219
|
resources_scope: :active,
|
197
|
-
pagination: true,
|
220
|
+
pagination: true,
|
198
221
|
params_resource: :country,
|
199
|
-
resource_method_name: :country
|
222
|
+
resource_method_name: :country
|
200
223
|
end
|
201
224
|
```
|
202
225
|
**NOTE:** For the use of the** `search_form_for`** you have the `resources_search` helper that is a racksack object or in this case `countries_search` because we used the resource alias `resource_method_name`
|
203
226
|
|
204
227
|
```
|
205
228
|
= search_form_for [:admin, countries_search] do |f|
|
206
|
-
= f.label :name_cont
|
207
|
-
= f.search_field :name_cont
|
208
|
-
= f.submit
|
229
|
+
= f.label :name_cont
|
230
|
+
= f.search_field :name_cont
|
231
|
+
= f.submit
|
209
232
|
```
|
210
233
|
#### **Overriding methods **
|
211
234
|
|
@@ -220,7 +243,7 @@ By default, after any of the REST actions that modify the record (create, updat
|
|
220
243
|
root_url
|
221
244
|
end
|
222
245
|
|
223
|
-
```
|
246
|
+
```
|
224
247
|
**NOTE:** After creating the record it will redirect to the **root_url**
|
225
248
|
|
226
249
|
##### **default REST actions**
|
@@ -242,13 +265,13 @@ If you want to override any of the REST actions, to add any extra logic that you
|
|
242
265
|
end
|
243
266
|
end
|
244
267
|
|
245
|
-
```
|
268
|
+
```
|
246
269
|
|
247
270
|
**NOTE:** Remember that you can make use of the **`resource_saved?`** method to know if the record has been saved
|
248
271
|
|
249
272
|
#### **GLOBAL CONFIGURATION **
|
250
273
|
|
251
|
-
You can change the default parameters of any configuration by change the options in the `config/initializers/resources.rb` that was generated by running `rails g resources:install`
|
274
|
+
You can change the default parameters of any configuration by change the options in the `config/initializers/resources.rb` that was generated by running `rails g resources:install`
|
252
275
|
|
253
276
|
```
|
254
277
|
Resources.config do |config|
|
@@ -264,6 +287,3 @@ end
|
|
264
287
|
## **Copyright**
|
265
288
|
|
266
289
|
Copyright (c) 2015 Emilio Forrer. See LICENSE.txt for further details.
|
267
|
-
|
268
|
-
|
269
|
-
|
data/lib/resources/manager.rb
CHANGED
@@ -14,21 +14,21 @@ module Resources
|
|
14
14
|
|
15
15
|
def resources_scope
|
16
16
|
scope = option_with_scope(:resources_scope)
|
17
|
-
@resources_scope =
|
18
|
-
case
|
17
|
+
@resources_scope =
|
18
|
+
case
|
19
19
|
when pagination?
|
20
20
|
scope = scope.page(params_page) if scope.respond_to?(:page)
|
21
21
|
scope = scope.paginate(page: params_page) if scope.respond_to?(:paginate)
|
22
22
|
scope
|
23
23
|
else
|
24
|
-
scope
|
24
|
+
scope
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
28
|
def pagination?
|
29
29
|
settings.pagination.is_a?(Proc) ? settings.pagination.call(params, controller) : settings.pagination
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
def resource_scope
|
33
33
|
@resource_scope = option_with_scope(:resource_scope)
|
34
34
|
end
|
@@ -38,11 +38,11 @@ module Resources
|
|
38
38
|
end
|
39
39
|
|
40
40
|
|
41
|
-
def resources
|
41
|
+
def resources
|
42
42
|
@resources ||= settings.search ? resources_search.result(settings.search_options) : resources_scope
|
43
43
|
end
|
44
44
|
|
45
|
-
def resource
|
45
|
+
def resource
|
46
46
|
@resource ||=
|
47
47
|
case controller.action_name
|
48
48
|
when "new", "create"
|
@@ -57,7 +57,7 @@ module Resources
|
|
57
57
|
end
|
58
58
|
|
59
59
|
def params
|
60
|
-
|
60
|
+
controller.params
|
61
61
|
end
|
62
62
|
|
63
63
|
def params_search
|
@@ -71,17 +71,17 @@ module Resources
|
|
71
71
|
def params_resource
|
72
72
|
@params_resource ||= option_with_params(:params_resource)
|
73
73
|
end
|
74
|
-
|
74
|
+
|
75
75
|
def build_resource
|
76
76
|
resource_class.new()
|
77
77
|
end
|
78
78
|
|
79
|
-
|
79
|
+
|
80
80
|
|
81
81
|
private
|
82
82
|
|
83
83
|
|
84
|
-
def option_with_scope name
|
84
|
+
def option_with_scope name
|
85
85
|
scope = Rails::VERSION::MAJOR >= 4 ? resource_class.all : resource_class.scoped
|
86
86
|
if settings.send(name)
|
87
87
|
settings.send(name).is_a?(Proc) ? settings.send(name).call(scope,params,controller) : scope.send(settings.send(name))
|
@@ -90,8 +90,16 @@ module Resources
|
|
90
90
|
end
|
91
91
|
end
|
92
92
|
|
93
|
-
def option_with_params name
|
94
|
-
settings.send(name).is_a?(Proc)
|
93
|
+
def option_with_params name
|
94
|
+
if settings.send(name).is_a?(Proc)
|
95
|
+
settings.send(name).call(params)
|
96
|
+
else
|
97
|
+
if Rails::VERSION::MAJOR >= 4
|
98
|
+
controller.send(name)
|
99
|
+
else
|
100
|
+
params[settings.send(name)]
|
101
|
+
end
|
102
|
+
end
|
95
103
|
end
|
96
104
|
|
97
105
|
def controller
|
@@ -102,8 +110,8 @@ module Resources
|
|
102
110
|
@request
|
103
111
|
end
|
104
112
|
|
105
|
-
|
113
|
+
|
106
114
|
|
107
115
|
|
108
116
|
end
|
109
|
-
end
|
117
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module Resources
|
2
2
|
module RestActions
|
3
3
|
extend ActiveSupport::Concern
|
4
|
-
|
4
|
+
|
5
5
|
included do
|
6
6
|
respond_to :html, :js, :json
|
7
7
|
helper_method :resource_saved?
|
@@ -16,25 +16,25 @@ module Resources
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def index
|
19
|
-
|
19
|
+
|
20
20
|
end
|
21
21
|
|
22
22
|
def new
|
23
|
-
|
23
|
+
|
24
24
|
end
|
25
25
|
|
26
26
|
def create
|
27
|
-
save_resource
|
27
|
+
save_resource
|
28
28
|
end
|
29
29
|
|
30
30
|
def edit
|
31
|
-
|
31
|
+
|
32
32
|
end
|
33
33
|
|
34
34
|
def update
|
35
|
-
save_resource
|
35
|
+
save_resource
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
def destroy
|
39
39
|
destroy_resource
|
40
40
|
end
|
@@ -46,10 +46,10 @@ module Resources
|
|
46
46
|
end
|
47
47
|
|
48
48
|
def save_resource &block
|
49
|
-
resource.
|
49
|
+
resource.assign_attributes(resource_manager.params_resource)
|
50
50
|
@resource_saved = resource.save
|
51
51
|
after_redirect_for = "after_#{action_name}_path_for"
|
52
|
-
action_path_for =
|
52
|
+
action_path_for =
|
53
53
|
case action_name
|
54
54
|
when "create"
|
55
55
|
:new
|
@@ -59,7 +59,7 @@ module Resources
|
|
59
59
|
:index
|
60
60
|
end
|
61
61
|
if block_given?
|
62
|
-
block.call(resource)
|
62
|
+
block.call(resource)
|
63
63
|
else
|
64
64
|
if self.respond_to?(after_redirect_for, true)
|
65
65
|
respond_with resource, location: send(after_redirect_for)
|
@@ -73,7 +73,7 @@ module Resources
|
|
73
73
|
@destroy_resource = resource.destroy
|
74
74
|
after_redirect_for = "after_#{action_name}_path_for"
|
75
75
|
if block_given?
|
76
|
-
block.call(@destroy_resource)
|
76
|
+
block.call(@destroy_resource)
|
77
77
|
else
|
78
78
|
if self.respond_to?(after_redirect_for, true)
|
79
79
|
respond_with resource, location: send(after_redirect_for), action: :destroy
|
data/lib/resources/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: resources
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Emilio Forrer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-08 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: It's a gem that help's you to create CRUD controllers for your models
|
14
14
|
in a very fast and flexible way.
|