action_crud 0.1.1 → 0.1.2
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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +22 -4
- data/lib/action_crud.rb +1 -0
- data/lib/action_crud/controller.rb +31 -25
- data/lib/action_crud/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: d3ed1ca5401fa0b8ec39ec1bb97d36698754f201
|
4
|
+
data.tar.gz: cc888e1468207682c216dc767f62fe6a24143503
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '038320aa145c60b315278c9a42f8888e1cdde284a3ebf0066ccfb21a3ef039a644ca35920604129ea250660a6ed13369485f74b8d9495af7a9c961125cc76115'
|
7
|
+
data.tar.gz: 6daa58a28127542dec00fd6718f3972c22aa1dfbd7814a5cffcf575bd96a9e433bf4ee3b1bec1c82d275f09632d597e9508e76a8d0f230bac57960114a7c8bab
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -4,7 +4,7 @@ ActionCrud speeds up development by making your controllers inherit all restful
|
|
4
4
|
|
5
5
|
[](https://badge.fury.io/rb/action_crud)
|
6
6
|
[](https://travis-ci.org/hardpixel/action-crud)
|
7
|
-
[](https://codeclimate.com/github/hardpixel/
|
7
|
+
[](https://codeclimate.com/github/hardpixel/action-crud)
|
8
8
|
|
9
9
|
## Installation
|
10
10
|
|
@@ -32,7 +32,7 @@ class Post < ActionController::Base
|
|
32
32
|
end
|
33
33
|
```
|
34
34
|
|
35
|
-
To set the controller model, use the `set_model_name`
|
35
|
+
To set the controller model and class, use the `set_model_name` and `set_model_class` functions:
|
36
36
|
|
37
37
|
```ruby
|
38
38
|
class Post < ActionController::Base
|
@@ -41,6 +41,12 @@ class Post < ActionController::Base
|
|
41
41
|
|
42
42
|
# Or by setting the model_name class attribute
|
43
43
|
self.model_name = 'Post'
|
44
|
+
|
45
|
+
# Using the set_model_class function
|
46
|
+
set_model_class 'Post'
|
47
|
+
|
48
|
+
# Or by setting the model_class class attribute
|
49
|
+
self.model_class = 'Post'
|
44
50
|
end
|
45
51
|
```
|
46
52
|
|
@@ -51,7 +57,7 @@ class Post < ActionController::Base
|
|
51
57
|
# Using the set_index_scope function
|
52
58
|
set_index_scope :published
|
53
59
|
|
54
|
-
# Or by setting the
|
60
|
+
# Or by setting the index_scope class attribute
|
55
61
|
self.index_scope = :published
|
56
62
|
end
|
57
63
|
```
|
@@ -75,7 +81,19 @@ class Post < ActionController::Base
|
|
75
81
|
end
|
76
82
|
```
|
77
83
|
|
78
|
-
|
84
|
+
If you use a pagination gem like [SmartPagination](https://github.com/hardpixel/smart-pagination), the index records will be automagically paginated. To set the results limit per page (default: 20), use the `set_per_page` function:
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
class Post < ActionController::Base
|
88
|
+
# Using the set_per_page function
|
89
|
+
set_per_page 10
|
90
|
+
|
91
|
+
# Or by setting the per_page class attribute
|
92
|
+
self.per_page = 10
|
93
|
+
end
|
94
|
+
```
|
95
|
+
|
96
|
+
After setting up the controller, like the examples above, you will have a fully working CRUD controller with instance variables `@post` and `@posts` available.
|
79
97
|
|
80
98
|
ActionCrud also injects in your views and controllers the following helpers:
|
81
99
|
|
data/lib/action_crud.rb
CHANGED
@@ -5,14 +5,20 @@ module ActionCrud
|
|
5
5
|
included do
|
6
6
|
# Class attributes
|
7
7
|
class_attribute :model_name, instance_predicate: false
|
8
|
+
class_attribute :model_class, instance_predicate: false
|
9
|
+
class_attribute :instance_name, instance_predicate: false
|
10
|
+
class_attribute :collection_name, instance_predicate: false
|
8
11
|
class_attribute :index_scope, instance_predicate: false
|
9
12
|
class_attribute :permitted_params, instance_predicate: false
|
10
13
|
|
11
14
|
# Class attributes defaults
|
12
|
-
self.model_name = self.controller_name
|
13
|
-
self.index_scope = :all
|
14
15
|
self.permitted_params = []
|
15
16
|
|
17
|
+
# Set class attributes
|
18
|
+
set_model_name
|
19
|
+
set_model_class
|
20
|
+
set_index_scope
|
21
|
+
|
16
22
|
# Action callbacks
|
17
23
|
before_action :set_record, only: [:show, :edit, :update, :destroy]
|
18
24
|
end
|
@@ -20,8 +26,7 @@ module ActionCrud
|
|
20
26
|
class_methods do
|
21
27
|
# Set permitted parameters
|
22
28
|
def permit_params(options={})
|
23
|
-
|
24
|
-
default = { only: model.attribute_names, except: [], also: [], array: [], hash: [] }
|
29
|
+
default = { only: model_class.attribute_names, except: [], also: [], array: [], hash: [] }
|
25
30
|
options = Hash[default.merge(options).map { |k, v| [k, Array(v).map(&:to_sym)] }]
|
26
31
|
permit = options.except(:except).values.flatten.uniq
|
27
32
|
|
@@ -33,19 +38,30 @@ module ActionCrud
|
|
33
38
|
end
|
34
39
|
|
35
40
|
# Set model name
|
36
|
-
def set_model_name(name)
|
37
|
-
|
41
|
+
def set_model_name(name=nil)
|
42
|
+
name = name || controller_name.classify
|
43
|
+
name = name.constantize if name.is_a? String
|
44
|
+
|
45
|
+
self.model_name = name.model_name
|
46
|
+
self.instance_name = model_name.singular
|
47
|
+
self.collection_name = model_name.plural
|
48
|
+
end
|
49
|
+
|
50
|
+
# Set model class
|
51
|
+
def set_model_class(klass=nil)
|
52
|
+
klass = klass.constantize if klass.is_a? String
|
53
|
+
self.model_class = klass || model_name.instance_variable_get('@klass')
|
38
54
|
end
|
39
55
|
|
40
56
|
# Set index scope
|
41
|
-
def set_index_scope(scope)
|
42
|
-
self.index_scope = scope
|
57
|
+
def set_index_scope(scope='all')
|
58
|
+
self.index_scope = scope.to_sym
|
43
59
|
end
|
44
60
|
end
|
45
61
|
|
46
62
|
# GET /model
|
47
63
|
def index
|
48
|
-
self.records = model.send
|
64
|
+
self.records = model.send index_scope
|
49
65
|
self.records = paginate(records) if respond_to? :per_page
|
50
66
|
|
51
67
|
respond_to do |format|
|
@@ -120,45 +136,35 @@ module ActionCrud
|
|
120
136
|
|
121
137
|
# Get model
|
122
138
|
def model
|
123
|
-
|
139
|
+
model_class
|
124
140
|
end
|
125
141
|
|
126
142
|
alias :current_model :model
|
127
143
|
|
128
144
|
# Get single record
|
129
145
|
def record
|
130
|
-
instance_variable_get "@#{
|
146
|
+
instance_variable_get "@#{instance_name}"
|
131
147
|
end
|
132
148
|
|
133
149
|
alias :current_record :record
|
134
150
|
|
135
151
|
# Get records collection
|
136
152
|
def records
|
137
|
-
instance_variable_get "@#{
|
153
|
+
instance_variable_get "@#{collection_name}"
|
138
154
|
end
|
139
155
|
|
140
156
|
alias :current_records :records
|
141
157
|
|
142
158
|
private
|
143
159
|
|
144
|
-
# Get singular name
|
145
|
-
def singular_name
|
146
|
-
model_name.demodulize.singularize
|
147
|
-
end
|
148
|
-
|
149
|
-
# Get plural name
|
150
|
-
def plural_name
|
151
|
-
model_name.demodulize.pluralize
|
152
|
-
end
|
153
|
-
|
154
160
|
# Set single record
|
155
161
|
def record=(value)
|
156
|
-
instance_variable_set "@#{
|
162
|
+
instance_variable_set "@#{instance_name}", value
|
157
163
|
end
|
158
164
|
|
159
165
|
# Set records collection
|
160
166
|
def records=(value)
|
161
|
-
instance_variable_set "@#{
|
167
|
+
instance_variable_set "@#{collection_name}", value
|
162
168
|
end
|
163
169
|
|
164
170
|
# Use callbacks to share common setup or constraints between actions.
|
@@ -168,7 +174,7 @@ module ActionCrud
|
|
168
174
|
|
169
175
|
# Only allow a trusted parameter "white list" through.
|
170
176
|
def record_params
|
171
|
-
params.require(:"#{
|
177
|
+
params.require(:"#{instance_name}").permit permitted_params
|
172
178
|
end
|
173
179
|
end
|
174
180
|
end
|
data/lib/action_crud/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: action_crud
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonian Guveli
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-10-
|
11
|
+
date: 2017-10-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|