auxiliary_rails_resourceable 0.1.0 → 0.1.1

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
  SHA256:
3
- metadata.gz: 4626f97cfe6bb22a70da7b258f5fe4f742f34f2b1584c76fb032125969f35cbb
4
- data.tar.gz: b08f4f1a0dcdb4a2a01c9ced689032c176ed843a63301686534590712ed38760
3
+ metadata.gz: 0cccc3798217d2f3f64d9250d46a5797ca20e4d7aba351d36d6c9ae938d0ce22
4
+ data.tar.gz: f014a530c24a038df4b312336c81e3beb5e510eebc6dc6449d43c6cc2080ea46
5
5
  SHA512:
6
- metadata.gz: 7138e711032c8cbb19a8542770f30aab514b2646069d2a5a71a4315b992d5e4f246e7f7dabc63560dbf9acfafd6f3c6819e19e944a625c6cdd8fa784d39c993e
7
- data.tar.gz: 68cf0c091e8a4b95661b51abf4f39d4e00faca9ee3030e5ea0cd361c03d6e7a599ded540e0e131255ec019a21584bd6e3fcb15bdce51d602b8b9b5f81030b3d4
6
+ metadata.gz: 312ac5eaea723c5ddce906ebe9e50d0a8cbd7572b007f4798ec2178d5fc831ab39e6acbff4d85fd0a616b6574c6c03b593e2e8371cf635492a26992eceb2aab2
7
+ data.tar.gz: 76ad31ecd517a89bab159b0c1e672cf9ff2fc3e08669399f4074808624de8519904d4de54ca5c66d18e53d5e15df2b7944c7ad9b8cb129e7b80658788c139f20
data/.rubocop.yml CHANGED
@@ -4,8 +4,15 @@ require:
4
4
  - rubocop-rspec
5
5
 
6
6
  AllCops:
7
+ Exclude:
8
+ - bin/bundle
9
+ - bin/rake
10
+ - bin/rspec
11
+ - bin/rubocop
12
+ - vendor/**/* # fix for CI
7
13
  TargetRubyVersion: 2.5
8
14
  NewCops: enable
15
+
9
16
  #################### Layout ##############################
10
17
 
11
18
  Layout/ArgumentAlignment:
data/CHANGELOG.md CHANGED
@@ -1,5 +1,7 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.1.1] - 2022-08-27
4
+
3
5
  ## [0.1.0] - 2022-08-20
4
6
 
5
7
  - Initial release
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- auxiliary_rails_resourceable (0.1.0)
4
+ auxiliary_rails_resourceable (0.1.1)
5
5
  pagy
6
6
  pundit
7
7
  rails (>= 5.2)
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # AuxiliaryRailsResourceable
2
2
 
3
+ [![Gem](https://img.shields.io/gem/v/auxiliary_rails_resourceable.svg)](https://rubygems.org/gems/auxiliary_rails_resourceable)
4
+
3
5
  ## Installation
4
6
 
5
7
  Add this line to your application's `Gemfile`:
@@ -31,8 +33,8 @@ end
31
33
  # - show.html.erb
32
34
  # - edit.html.erb
33
35
  # - _form.html.erb
34
- # - _search_form.html.erb
35
36
  # - _list.html.erb
37
+ # - _search_form.html.erb
36
38
 
37
39
  # /config/locales/resources.en.yml
38
40
  en:
@@ -10,7 +10,7 @@ module AuxiliaryRails
10
10
  before_action only: %i[index new] do
11
11
  authorize resource_class
12
12
  end
13
- before_action :resource, only: %i[show edit destroy] do
13
+ before_action :resource, only: %i[show edit] do
14
14
  authorize resource
15
15
  end
16
16
 
@@ -27,9 +27,9 @@ module AuxiliaryRails
27
27
  end
28
28
 
29
29
  def index
30
- @q = collection.ransack(params[:q])
31
- @q.sorts = default_sorts if @q.sorts.empty?
32
- @pagy, @pagy_records = pagy(@q.result(distinct: true))
30
+ @ransack = collection.ransack(search_params)
31
+ @ransack.sorts = default_sorts if @ransack.sorts.empty?
32
+ @pagy, self.collection = pagy(@ransack.result)
33
33
  end
34
34
 
35
35
  def new
@@ -37,14 +37,8 @@ module AuxiliaryRails
37
37
  end
38
38
 
39
39
  def create
40
- self.resource = build_resource(resource_params)
41
-
42
- authorize resource
43
-
44
- if resource.save
45
- redirect_to resource_path(resource),
46
- notice: t('resources.create.notice',
47
- resource_name: resource_class.model_name.human)
40
+ if create_resource(resource_params)
41
+ redirect_after_create
48
42
  else
49
43
  render :new
50
44
  end
@@ -57,56 +51,95 @@ module AuxiliaryRails
57
51
  end
58
52
 
59
53
  def update
60
- resource.assign_attributes(resource_params)
61
-
62
- authorize resource
63
-
64
- if resource.save
65
- redirect_to resource_path(resource),
66
- notice: t('resources.update.notice',
67
- resource_name: resource_class.model_name.human)
54
+ if update_resource(resource_params)
55
+ redirect_after_update
68
56
  else
69
57
  render :edit
70
58
  end
71
59
  end
72
60
 
73
61
  def destroy
74
- resource.destroy
62
+ destroy_resource
75
63
 
76
- redirect_to collection_path,
77
- notice: t('resources.destroy.notice',
78
- resource_name: resource_class.model_name.human)
64
+ redirect_after_destroy
79
65
  end
80
66
 
81
67
  protected
82
68
 
83
- # config
69
+ # configs
84
70
 
85
71
  def default_sorts
86
- ['id asc']
72
+ %w[name title id].find { |v| v.in?(resource_class.column_names) } || []
73
+ end
74
+
75
+ # Defines an I18n scope for flash messages
76
+ #
77
+ # Use `controller_name` for scoping as the name of the controller.
78
+ def i18n_scope
79
+ :resources
87
80
  end
88
81
 
89
82
  def id_param
90
83
  params[:id]
91
84
  end
92
85
 
86
+ def search_params
87
+ params[:q]
88
+ end
89
+
93
90
  def collection_name
94
91
  @collection_name ||= controller_name
95
92
  end
96
93
 
94
+ def collection_scope
95
+ policy_scope(resource_class).all
96
+ end
97
+
97
98
  def resource_class
98
99
  @resource_class ||= resource_name.camelize.constantize
99
100
  end
100
101
 
101
102
  def resource_name
102
- @resource_name ||= controller_name.singularize
103
+ @resource_name ||= collection_name.singularize
104
+ end
105
+
106
+ # actions
107
+
108
+ def build_resource(attributes = {})
109
+ resource_class.new(attributes)
110
+ end
111
+
112
+ def create_resource(attributes = {})
113
+ self.resource = build_resource(attributes)
114
+
115
+ authorize resource, :create?
116
+
117
+ resource.save
118
+ end
119
+
120
+ def find_resource
121
+ resource_class.find(id_param)
122
+ end
123
+
124
+ def update_resource(attributes = {})
125
+ resource.assign_attributes(attributes)
126
+
127
+ authorize resource, :update?
128
+
129
+ resource.save
130
+ end
131
+
132
+ def destroy_resource
133
+ authorize resource, :destroy?
134
+
135
+ resource.destroy
103
136
  end
104
137
 
105
138
  # helpers
106
139
 
107
140
  def collection
108
141
  instance_variable_get("@#{collection_name}") ||
109
- (self.collection = resource_scope)
142
+ (self.collection = collection_scope)
110
143
  end
111
144
 
112
145
  def collection=(object)
@@ -128,18 +161,6 @@ module AuxiliaryRails
128
161
  .permit(policy(resource_class).permitted_attributes)
129
162
  end
130
163
 
131
- def resource_scope
132
- policy_scope(resource_class.all)
133
- end
134
-
135
- def build_resource(attributes = {})
136
- resource_class.new(attributes)
137
- end
138
-
139
- def find_resource
140
- resource_class.find(id_param)
141
- end
142
-
143
164
  def collection_path
144
165
  public_send(path_method_name(:collection))
145
166
  end
@@ -152,35 +173,47 @@ module AuxiliaryRails
152
173
  public_send(path_method_name(:resource, :new))
153
174
  end
154
175
 
176
+ # redirects
177
+
178
+ def redirect_after_create
179
+ redirect_to resource_path(resource),
180
+ notice: t('create.notice',
181
+ resource_name: resource_class.model_name.human,
182
+ scope: i18n_scope)
183
+ end
184
+
185
+ def redirect_after_update
186
+ redirect_to resource_path(resource),
187
+ notice: t('update.notice',
188
+ resource_name: resource_class.model_name.human,
189
+ scope: i18n_scope)
190
+ end
191
+
192
+ def redirect_after_destroy
193
+ redirect_to collection_path,
194
+ notice: t('destroy.notice',
195
+ resource_name: resource_class.model_name.human,
196
+ scope: i18n_scope)
197
+ end
198
+
155
199
  # system
156
200
 
157
- def controller_module_parent
201
+ def controller_module_parent_name
158
202
  if Rails.version < '6'
159
- raise NotImplementedError,
160
- '`controller_module_parent` needs to be implented because ' \
161
- 'Rails < 6 does not supports `module_parent`'
203
+ return self.class.parent_name
162
204
  end
163
205
 
164
- self.class.module_parent
206
+ self.class.module_parent_name
165
207
  end
166
208
 
167
- # rubocop:disable Metrics/MethodLength
168
209
  def path_method_name(type, action = nil)
169
- path_parts = [
170
- 'path',
171
- (type.to_sym == :resource ? resource_name : controller_name)
172
- ]
173
-
174
- if controller_module_parent != Object
175
- path_parts << controller_module_parent.to_s.underscore
176
- end
177
- if action.present?
178
- path_parts << action
179
- end
180
-
181
- path_parts.reverse.join('_')
210
+ [
211
+ action,
212
+ controller_module_parent_name&.underscore,
213
+ (type.to_sym == :resource ? resource_name : controller_name),
214
+ 'path'
215
+ ].compact.join('_')
182
216
  end
183
- # rubocop:enable Metrics/MethodLength
184
217
  end
185
218
  end
186
219
  end
@@ -1,3 +1,3 @@
1
1
  module AuxiliaryRailsResourceable
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.1.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: auxiliary_rails_resourceable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Babenko
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2022-08-20 00:00:00.000000000 Z
12
+ date: 2022-08-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: pagy