action_crud 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/action_crud.rb +0 -3
- data/lib/action_crud/controller.rb +32 -50
- data/lib/action_crud/helpers/link.rb +2 -10
- data/lib/action_crud/helpers/route.rb +5 -15
- data/lib/action_crud/helpers/url.rb +0 -8
- data/lib/action_crud/helpers/view.rb +0 -6
- data/lib/action_crud/pagination.rb +23 -31
- data/lib/action_crud/version.rb +1 -1
- metadata +5 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c20e55aaf0acfe2a95c431b090cafedd3ea5718ed8e73e4995281782dc0c969b
|
4
|
+
data.tar.gz: 0c297533a30364e41575d82efb723484f035aab02d6cfb8f84c3ab6186f18a57
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ee2214afeb91977c9959eda5122a57d7389617789eeab0fad6884f099453d237c4052c1b14c7ff0dcbd6a63915fa5634f64e0ae0ed93f62488bc6a28b06c7f18
|
7
|
+
data.tar.gz: 0276fafa9641249240ebbf6b7e25a93e55a797042502ffab2f5e37a8b7b48dce22ccee6f0d716ec4e2486265fa77febd878fd7a81b89a8bc490b29057ab604c4
|
data/lib/action_crud.rb
CHANGED
@@ -11,19 +11,16 @@ module ActionCrud
|
|
11
11
|
extend ActiveSupport::Concern
|
12
12
|
|
13
13
|
included do
|
14
|
-
# Include submodules
|
15
14
|
include ActionCrud::Controller
|
16
15
|
include ActionCrud::Pagination
|
17
16
|
end
|
18
17
|
end
|
19
18
|
|
20
|
-
# Include action view helpers
|
21
19
|
if defined? ActionView::Base
|
22
20
|
ActionView::Base.send :include, ActionCrud::Helpers::Url
|
23
21
|
ActionView::Base.send :include, ActionCrud::Helpers::View
|
24
22
|
end
|
25
23
|
|
26
|
-
# Include action controller helpers
|
27
24
|
if defined? ActionController::Base
|
28
25
|
ActionController::Base.send :include, ActionCrud::Helpers::Url
|
29
26
|
end
|
@@ -3,7 +3,6 @@ module ActionCrud
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
included do
|
6
|
-
# Class attributes
|
7
6
|
class_attribute :namespace, instance_predicate: false
|
8
7
|
class_attribute :model_name, instance_predicate: false
|
9
8
|
class_attribute :model_class, instance_predicate: false
|
@@ -12,21 +11,17 @@ module ActionCrud
|
|
12
11
|
class_attribute :index_scope, instance_predicate: false
|
13
12
|
class_attribute :permitted_params, instance_predicate: false
|
14
13
|
|
15
|
-
# Class attributes defaults
|
16
14
|
self.permitted_params = []
|
17
15
|
|
18
|
-
# Set class attributes
|
19
16
|
set_model_name
|
20
17
|
set_index_scope
|
21
18
|
|
22
|
-
# Action callbacks
|
23
19
|
before_action :set_record, only: [:show, :edit, :update, :destroy]
|
24
20
|
before_action :set_permitted_params, if: -> { permitted_params.empty? }
|
25
21
|
end
|
26
22
|
|
27
23
|
class_methods do
|
28
|
-
|
29
|
-
def permit_params(options={})
|
24
|
+
def permit_params(options = {})
|
30
25
|
attribs = model_class.try(:attribute_names).to_a
|
31
26
|
default = { only: attribs, except: [], also: [], array: [], hash: [] }
|
32
27
|
options = Hash[default.merge(options).map { |k, v| [k, Array(v).map(&:to_sym)] }]
|
@@ -39,14 +34,12 @@ module ActionCrud
|
|
39
34
|
self.permitted_params = permit
|
40
35
|
end
|
41
36
|
|
42
|
-
|
43
|
-
def set_namespace(name=nil)
|
37
|
+
def set_namespace(name = nil)
|
44
38
|
self.namespace = name
|
45
39
|
set_model_name
|
46
40
|
end
|
47
41
|
|
48
|
-
|
49
|
-
def set_model_name(name=nil)
|
42
|
+
def set_model_name(name = nil)
|
50
43
|
name = name || _guess_controller_model
|
51
44
|
name = name.constantize if name.is_a? String
|
52
45
|
|
@@ -59,27 +52,25 @@ module ActionCrud
|
|
59
52
|
end
|
60
53
|
end
|
61
54
|
|
62
|
-
|
63
|
-
def set_model_class(klass=nil)
|
55
|
+
def set_model_class(klass = nil)
|
64
56
|
klass = klass.constantize if klass.is_a? String
|
65
57
|
self.model_class = klass || model_name.instance_variable_get('@klass')
|
66
58
|
end
|
67
59
|
|
68
|
-
|
69
|
-
def set_index_scope(scope='all')
|
60
|
+
def set_index_scope(scope = 'all')
|
70
61
|
self.index_scope = scope.to_sym
|
71
62
|
end
|
72
63
|
|
73
64
|
private
|
74
65
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
end
|
66
|
+
def _guess_controller_model
|
67
|
+
if namespace.nil?
|
68
|
+
controller_name.classify.safe_constantize ||
|
69
|
+
name.gsub(/controller$/i, '').classify.safe_constantize
|
70
|
+
else
|
71
|
+
name.gsub(/^#{namespace.to_s}|controller$/i, '').classify.safe_constantize
|
82
72
|
end
|
73
|
+
end
|
83
74
|
end
|
84
75
|
|
85
76
|
# GET /model
|
@@ -158,21 +149,18 @@ module ActionCrud
|
|
158
149
|
end
|
159
150
|
end
|
160
151
|
|
161
|
-
# Get model
|
162
152
|
def model
|
163
153
|
record.nil? ? model_class : record.class
|
164
154
|
end
|
165
155
|
|
166
156
|
alias :current_model :model
|
167
157
|
|
168
|
-
# Get single record
|
169
158
|
def record
|
170
159
|
instance_variable_get "@#{instance_name}"
|
171
160
|
end
|
172
161
|
|
173
162
|
alias :current_record :record
|
174
163
|
|
175
|
-
# Get records collection
|
176
164
|
def records
|
177
165
|
instance_variable_get "@#{collection_name}"
|
178
166
|
end
|
@@ -181,37 +169,31 @@ module ActionCrud
|
|
181
169
|
|
182
170
|
private
|
183
171
|
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
end
|
172
|
+
def record=(value)
|
173
|
+
instance_variable_set "@#{instance_name}", value
|
174
|
+
end
|
188
175
|
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
end
|
176
|
+
def records=(value)
|
177
|
+
instance_variable_set "@#{collection_name}", value
|
178
|
+
end
|
193
179
|
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
end
|
180
|
+
def set_record
|
181
|
+
self.record = model.find(params[:id])
|
182
|
+
end
|
198
183
|
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
end
|
184
|
+
def record_params
|
185
|
+
params.require(:"#{instance_name}").permit permitted_params
|
186
|
+
end
|
203
187
|
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
attributes = record.try(att_method) || model.try(att_method)
|
188
|
+
def set_permitted_params
|
189
|
+
att_method = :permitted_attributes
|
190
|
+
attributes = record.try(att_method) || model.try(att_method)
|
208
191
|
|
209
|
-
|
210
|
-
|
192
|
+
self.permitted_params = Array(attributes)
|
193
|
+
end
|
211
194
|
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
end
|
195
|
+
def should_search?
|
196
|
+
params[:search].present? && model.respond_to?(:search)
|
197
|
+
end
|
216
198
|
end
|
217
199
|
end
|
@@ -3,8 +3,7 @@ module ActionCrud
|
|
3
3
|
class Link
|
4
4
|
attr_accessor :record, :action, :label, :options
|
5
5
|
|
6
|
-
|
7
|
-
def initialize(context, record=nil, action=nil, *args)
|
6
|
+
def initialize(context, record = nil, action = nil, *args)
|
8
7
|
@options = args.extract_options!
|
9
8
|
@context = context
|
10
9
|
@record = record || @context.try(:current_record)
|
@@ -12,27 +11,21 @@ module ActionCrud
|
|
12
11
|
@label = options.fetch :label, nil
|
13
12
|
@options = options.except :label
|
14
13
|
|
15
|
-
if delete?
|
16
|
-
@options.reverse_merge!(method: :delete, data: { confirm: 'Are you sure?' })
|
17
|
-
end
|
14
|
+
@options.reverse_merge!(method: :delete, data: { confirm: 'Are you sure?' }) if delete?
|
18
15
|
end
|
19
16
|
|
20
|
-
# Is delete action
|
21
17
|
def delete?
|
22
18
|
action.in? [:delete, :destroy]
|
23
19
|
end
|
24
20
|
|
25
|
-
# Get link label
|
26
21
|
def action_label
|
27
22
|
label || "#{action}".humanize
|
28
23
|
end
|
29
24
|
|
30
|
-
# Get record path
|
31
25
|
def record_path
|
32
26
|
ActionCrud::Helpers::Route.new(@context, record, action).path || "#{action}"
|
33
27
|
end
|
34
28
|
|
35
|
-
# Render multiple
|
36
29
|
def render_multiple(*args)
|
37
30
|
default = options
|
38
31
|
options = args.extract_options!
|
@@ -47,7 +40,6 @@ module ActionCrud
|
|
47
40
|
links.join.html_safe
|
48
41
|
end
|
49
42
|
|
50
|
-
# Render link
|
51
43
|
def render
|
52
44
|
@context.link_to action_label, record_path, options if record_path.present?
|
53
45
|
end
|
@@ -3,8 +3,7 @@ module ActionCrud
|
|
3
3
|
class Route
|
4
4
|
attr_accessor :model, :record, :action, :options, :path, :url
|
5
5
|
|
6
|
-
|
7
|
-
def initialize(context, record=nil, action=nil, *options)
|
6
|
+
def initialize(context, record = nil, action = nil, *options)
|
8
7
|
@context = context
|
9
8
|
@record = record || @context.try(:current_record)
|
10
9
|
@action = action
|
@@ -13,24 +12,18 @@ module ActionCrud
|
|
13
12
|
@url = route_uri false
|
14
13
|
end
|
15
14
|
|
16
|
-
|
17
|
-
def to_s(type=:path)
|
15
|
+
def to_s(type = :path)
|
18
16
|
instance_variable_get("@#{type}").to_s
|
19
17
|
end
|
20
18
|
|
21
|
-
# Should include record
|
22
19
|
def record?
|
23
20
|
action.in? [:show, :edit, :delete, :destroy]
|
24
21
|
end
|
25
22
|
|
26
|
-
|
27
|
-
|
28
|
-
unless @record.nil?
|
29
|
-
@record.class.model_name.send(method).to_sym
|
30
|
-
end
|
23
|
+
def model(method = 'singular')
|
24
|
+
@record.class.model_name.send(method).to_sym unless @record.nil?
|
31
25
|
end
|
32
26
|
|
33
|
-
# Get route namespace
|
34
27
|
def namespace
|
35
28
|
if @context.respond_to? :controller
|
36
29
|
@context.controller.try(:namespace)
|
@@ -39,7 +32,6 @@ module ActionCrud
|
|
39
32
|
end
|
40
33
|
end
|
41
34
|
|
42
|
-
# Get namespaced record
|
43
35
|
def namespaced_record
|
44
36
|
if action == :edit
|
45
37
|
namespace.present? ? [:edit, namespace, record] : [:edit, record]
|
@@ -48,7 +40,6 @@ module ActionCrud
|
|
48
40
|
end
|
49
41
|
end
|
50
42
|
|
51
|
-
# Get namespaced model
|
52
43
|
def namespaced_model
|
53
44
|
if action == :new
|
54
45
|
namespace.present? ? [:new, namespace, model] : [:new, model]
|
@@ -57,8 +48,7 @@ module ActionCrud
|
|
57
48
|
end
|
58
49
|
end
|
59
50
|
|
60
|
-
|
61
|
-
def route_uri(only_path=false)
|
51
|
+
def route_uri(only_path = false)
|
62
52
|
args = record? ? namespaced_record : namespaced_model
|
63
53
|
args = [args, options.merge(only_path: only_path)].flatten.reject(&:blank?)
|
64
54
|
|
@@ -1,7 +1,6 @@
|
|
1
1
|
module ActionCrud
|
2
2
|
module Helpers
|
3
3
|
module Url
|
4
|
-
# Get record path
|
5
4
|
def record_path(*args)
|
6
5
|
options = args.extract_options!
|
7
6
|
record = args.first
|
@@ -9,7 +8,6 @@ module ActionCrud
|
|
9
8
|
ActionCrud::Helpers::Route.new(self, record, :show, options).path
|
10
9
|
end
|
11
10
|
|
12
|
-
# Get record absolute url
|
13
11
|
def record_url(*args)
|
14
12
|
options = args.extract_options!
|
15
13
|
record = args.first
|
@@ -17,7 +15,6 @@ module ActionCrud
|
|
17
15
|
ActionCrud::Helpers::Route.new(self, record, :show, options).url
|
18
16
|
end
|
19
17
|
|
20
|
-
# Get records index path
|
21
18
|
def records_path(*args)
|
22
19
|
options = args.extract_options!
|
23
20
|
record = args.first
|
@@ -25,7 +22,6 @@ module ActionCrud
|
|
25
22
|
ActionCrud::Helpers::Route.new(self, record, :index, options).path
|
26
23
|
end
|
27
24
|
|
28
|
-
# Get records index absolute url
|
29
25
|
def records_url(*args)
|
30
26
|
options = args.extract_options!
|
31
27
|
record = args.first
|
@@ -33,7 +29,6 @@ module ActionCrud
|
|
33
29
|
ActionCrud::Helpers::Route.new(self, record, :index, options).url
|
34
30
|
end
|
35
31
|
|
36
|
-
# Get record new path
|
37
32
|
def new_record_path(*args)
|
38
33
|
options = args.extract_options!
|
39
34
|
record = args.first
|
@@ -41,7 +36,6 @@ module ActionCrud
|
|
41
36
|
ActionCrud::Helpers::Route.new(self, record, :new, options).path
|
42
37
|
end
|
43
38
|
|
44
|
-
# Get record new absolute url
|
45
39
|
def new_record_url(*args)
|
46
40
|
options = args.extract_options!
|
47
41
|
record = args.first
|
@@ -49,7 +43,6 @@ module ActionCrud
|
|
49
43
|
ActionCrud::Helpers::Route.new(self, record, :new, options).url
|
50
44
|
end
|
51
45
|
|
52
|
-
# Get record edit path
|
53
46
|
def edit_record_path(*args)
|
54
47
|
options = args.extract_options!
|
55
48
|
record = args.first
|
@@ -57,7 +50,6 @@ module ActionCrud
|
|
57
50
|
ActionCrud::Helpers::Route.new(self, record, :edit, options).path
|
58
51
|
end
|
59
52
|
|
60
|
-
# Get record edit absolute url
|
61
53
|
def edit_record_url(*args)
|
62
54
|
options = args.extract_options!
|
63
55
|
record = args.first
|
@@ -1,27 +1,22 @@
|
|
1
1
|
module ActionCrud
|
2
2
|
module Helpers
|
3
3
|
module View
|
4
|
-
# Get current model
|
5
4
|
def current_model
|
6
5
|
controller.try(:current_model)
|
7
6
|
end
|
8
7
|
|
9
|
-
# Get current record
|
10
8
|
def current_record
|
11
9
|
controller.try(:current_record) || current_model.try(:new)
|
12
10
|
end
|
13
11
|
|
14
|
-
# Get current records
|
15
12
|
def current_records
|
16
13
|
controller.try(:current_records) || []
|
17
14
|
end
|
18
15
|
|
19
|
-
# Get permitted parameters
|
20
16
|
def permitted_params
|
21
17
|
controller.try(:permitted_params) || []
|
22
18
|
end
|
23
19
|
|
24
|
-
# Get record link
|
25
20
|
def record_link_to(record=nil, *args)
|
26
21
|
options = args.extract_options!
|
27
22
|
action = args.first
|
@@ -29,7 +24,6 @@ module ActionCrud
|
|
29
24
|
ActionCrud::Helpers::Link.new(self, record, action, options).render
|
30
25
|
end
|
31
26
|
|
32
|
-
# Get record links
|
33
27
|
def record_links_to(record=nil, *args)
|
34
28
|
options = args.extract_options!
|
35
29
|
default = options.fetch :html, {}
|
@@ -3,18 +3,14 @@ module ActionCrud
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
included do
|
6
|
-
# Class attributes
|
7
6
|
class_attribute :per_page, instance_predicate: false
|
8
7
|
|
9
|
-
# Class attributes defaults
|
10
8
|
self.per_page = 20
|
11
9
|
|
12
|
-
# Action callbacks
|
13
10
|
before_action :set_pagination_params, only: [:index]
|
14
11
|
end
|
15
12
|
|
16
13
|
class_methods do
|
17
|
-
# Set per page limit
|
18
14
|
def set_per_page(limit)
|
19
15
|
self.per_page = limit.to_i
|
20
16
|
end
|
@@ -22,36 +18,32 @@ module ActionCrud
|
|
22
18
|
|
23
19
|
private
|
24
20
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
end
|
21
|
+
def pagination_params
|
22
|
+
@pagination_params ||= params.permit(:page, :per_page).to_h.deep_symbolize_keys
|
23
|
+
end
|
29
24
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
25
|
+
def set_pagination_params
|
26
|
+
pagination = pagination_params[:page].is_a?(Hash) ? pagination_params[:page] : {}
|
27
|
+
pagination_params[:page] = pagination[:number] || pagination_params.fetch(:page, 1)
|
28
|
+
pagination_params[:per_page] = pagination[:size] || pagination_params.fetch(:per_page, per_page)
|
29
|
+
end
|
36
30
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
records
|
43
|
-
end
|
31
|
+
def paginate(records)
|
32
|
+
if records.respond_to? :paginate
|
33
|
+
records.paginate(pagination_params.select { |k, _v| k.in? [:page, :per_page] })
|
34
|
+
else
|
35
|
+
records
|
44
36
|
end
|
37
|
+
end
|
45
38
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
end
|
39
|
+
def pagination_meta(records)
|
40
|
+
{
|
41
|
+
current_page: pagination_params.fetch(:page, 1).to_i,
|
42
|
+
next_page: records.try(:next_page),
|
43
|
+
previous_page: records.try(:previous_page),
|
44
|
+
total_pages: records.try(:total_pages),
|
45
|
+
total_entries: records.try(:total_entries)
|
46
|
+
}
|
47
|
+
end
|
56
48
|
end
|
57
49
|
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.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonian Guveli
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-03-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|
@@ -44,14 +44,14 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '13.0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '13.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: minitest
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -104,8 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
104
|
- !ruby/object:Gem::Version
|
105
105
|
version: '0'
|
106
106
|
requirements: []
|
107
|
-
|
108
|
-
rubygems_version: 2.7.7
|
107
|
+
rubygems_version: 3.0.6
|
109
108
|
signing_key:
|
110
109
|
specification_version: 4
|
111
110
|
summary: Inherited restful actions
|