auxiliary_rails_resourceable 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4626f97cfe6bb22a70da7b258f5fe4f742f34f2b1584c76fb032125969f35cbb
4
- data.tar.gz: b08f4f1a0dcdb4a2a01c9ced689032c176ed843a63301686534590712ed38760
3
+ metadata.gz: 2652d651562f292661ab1e5bab3e5f360092dce54bb8d6a26dae20833d3534e4
4
+ data.tar.gz: 75deec7ba21c12b6d746ab5c3c3bb91e55cd781beec2a4749896c46225756eb3
5
5
  SHA512:
6
- metadata.gz: 7138e711032c8cbb19a8542770f30aab514b2646069d2a5a71a4315b992d5e4f246e7f7dabc63560dbf9acfafd6f3c6819e19e944a625c6cdd8fa784d39c993e
7
- data.tar.gz: 68cf0c091e8a4b95661b51abf4f39d4e00faca9ee3030e5ea0cd361c03d6e7a599ded540e0e131255ec019a21584bd6e3fcb15bdce51d602b8b9b5f81030b3d4
6
+ metadata.gz: 55a32aaa16651fed49f565d09d9e43f690172d2ec4f4875bac6e20e25bd983e01586db22a08eb24bf805a1a7ad182626f128a180b8ff5859d726b90060875dba
7
+ data.tar.gz: b49c1bd45598e9da86439cf5ec29bd0203600aadb079895f5027a083fad3a22d1558cfcd94416d06c094b5e0f4cd9e3e1cb1a5fa56afeacf4393ff765742c64e
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:
@@ -33,3 +40,8 @@ Style/IfUnlessModifier:
33
40
 
34
41
  Style/EmptyMethod:
35
42
  EnforcedStyle: expanded
43
+
44
+ Style/Documentation:
45
+ Exclude:
46
+ - lib/auxiliary_rails/view_helpers/human_name_helper.rb
47
+ - lib/auxiliary_rails_resourceable/railtie.rb
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.1.2] - 2022-08-17
4
+
5
+ - Added `human_name` and `human_name_plural` helpers
6
+ - Share views between modules via `resourceable` directory
7
+ - Respond format.json instead of redirects
8
+
9
+ ## [0.1.1] - 2022-08-27
10
+
3
11
  ## [0.1.0] - 2022-08-20
4
12
 
5
13
  - 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.2)
5
5
  pagy
6
6
  pundit
7
7
  rails (>= 5.2)
@@ -10,155 +10,179 @@ PATH
10
10
  GEM
11
11
  remote: https://rubygems.org/
12
12
  specs:
13
- actioncable (7.0.3.1)
14
- actionpack (= 7.0.3.1)
15
- activesupport (= 7.0.3.1)
13
+ actioncable (7.2.0)
14
+ actionpack (= 7.2.0)
15
+ activesupport (= 7.2.0)
16
16
  nio4r (~> 2.0)
17
17
  websocket-driver (>= 0.6.1)
18
- actionmailbox (7.0.3.1)
19
- actionpack (= 7.0.3.1)
20
- activejob (= 7.0.3.1)
21
- activerecord (= 7.0.3.1)
22
- activestorage (= 7.0.3.1)
23
- activesupport (= 7.0.3.1)
24
- mail (>= 2.7.1)
25
- net-imap
26
- net-pop
27
- net-smtp
28
- actionmailer (7.0.3.1)
29
- actionpack (= 7.0.3.1)
30
- actionview (= 7.0.3.1)
31
- activejob (= 7.0.3.1)
32
- activesupport (= 7.0.3.1)
33
- mail (~> 2.5, >= 2.5.4)
34
- net-imap
35
- net-pop
36
- net-smtp
37
- rails-dom-testing (~> 2.0)
38
- actionpack (7.0.3.1)
39
- actionview (= 7.0.3.1)
40
- activesupport (= 7.0.3.1)
41
- rack (~> 2.0, >= 2.2.0)
18
+ zeitwerk (~> 2.6)
19
+ actionmailbox (7.2.0)
20
+ actionpack (= 7.2.0)
21
+ activejob (= 7.2.0)
22
+ activerecord (= 7.2.0)
23
+ activestorage (= 7.2.0)
24
+ activesupport (= 7.2.0)
25
+ mail (>= 2.8.0)
26
+ actionmailer (7.2.0)
27
+ actionpack (= 7.2.0)
28
+ actionview (= 7.2.0)
29
+ activejob (= 7.2.0)
30
+ activesupport (= 7.2.0)
31
+ mail (>= 2.8.0)
32
+ rails-dom-testing (~> 2.2)
33
+ actionpack (7.2.0)
34
+ actionview (= 7.2.0)
35
+ activesupport (= 7.2.0)
36
+ nokogiri (>= 1.8.5)
37
+ racc
38
+ rack (>= 2.2.4, < 3.2)
39
+ rack-session (>= 1.0.1)
42
40
  rack-test (>= 0.6.3)
43
- rails-dom-testing (~> 2.0)
44
- rails-html-sanitizer (~> 1.0, >= 1.2.0)
45
- actiontext (7.0.3.1)
46
- actionpack (= 7.0.3.1)
47
- activerecord (= 7.0.3.1)
48
- activestorage (= 7.0.3.1)
49
- activesupport (= 7.0.3.1)
41
+ rails-dom-testing (~> 2.2)
42
+ rails-html-sanitizer (~> 1.6)
43
+ useragent (~> 0.16)
44
+ actiontext (7.2.0)
45
+ actionpack (= 7.2.0)
46
+ activerecord (= 7.2.0)
47
+ activestorage (= 7.2.0)
48
+ activesupport (= 7.2.0)
50
49
  globalid (>= 0.6.0)
51
50
  nokogiri (>= 1.8.5)
52
- actionview (7.0.3.1)
53
- activesupport (= 7.0.3.1)
51
+ actionview (7.2.0)
52
+ activesupport (= 7.2.0)
54
53
  builder (~> 3.1)
55
- erubi (~> 1.4)
56
- rails-dom-testing (~> 2.0)
57
- rails-html-sanitizer (~> 1.1, >= 1.2.0)
58
- activejob (7.0.3.1)
59
- activesupport (= 7.0.3.1)
54
+ erubi (~> 1.11)
55
+ rails-dom-testing (~> 2.2)
56
+ rails-html-sanitizer (~> 1.6)
57
+ activejob (7.2.0)
58
+ activesupport (= 7.2.0)
60
59
  globalid (>= 0.3.6)
61
- activemodel (7.0.3.1)
62
- activesupport (= 7.0.3.1)
63
- activerecord (7.0.3.1)
64
- activemodel (= 7.0.3.1)
65
- activesupport (= 7.0.3.1)
66
- activestorage (7.0.3.1)
67
- actionpack (= 7.0.3.1)
68
- activejob (= 7.0.3.1)
69
- activerecord (= 7.0.3.1)
70
- activesupport (= 7.0.3.1)
60
+ activemodel (7.2.0)
61
+ activesupport (= 7.2.0)
62
+ activerecord (7.2.0)
63
+ activemodel (= 7.2.0)
64
+ activesupport (= 7.2.0)
65
+ timeout (>= 0.4.0)
66
+ activestorage (7.2.0)
67
+ actionpack (= 7.2.0)
68
+ activejob (= 7.2.0)
69
+ activerecord (= 7.2.0)
70
+ activesupport (= 7.2.0)
71
71
  marcel (~> 1.0)
72
- mini_mime (>= 1.1.0)
73
- activesupport (7.0.3.1)
74
- concurrent-ruby (~> 1.0, >= 1.0.2)
72
+ activesupport (7.2.0)
73
+ base64
74
+ bigdecimal
75
+ concurrent-ruby (~> 1.0, >= 1.3.1)
76
+ connection_pool (>= 2.2.5)
77
+ drb
75
78
  i18n (>= 1.6, < 2)
79
+ logger (>= 1.4.2)
76
80
  minitest (>= 5.1)
77
- tzinfo (~> 2.0)
81
+ securerandom (>= 0.3)
82
+ tzinfo (~> 2.0, >= 2.0.5)
78
83
  ast (2.4.2)
79
- builder (3.2.4)
80
- concurrent-ruby (1.1.10)
84
+ base64 (0.2.0)
85
+ bigdecimal (3.1.8)
86
+ builder (3.3.0)
87
+ concurrent-ruby (1.3.4)
88
+ connection_pool (2.4.1)
81
89
  crass (1.0.6)
90
+ date (3.3.4)
82
91
  diff-lcs (1.5.0)
83
- digest (3.1.0)
84
- erubi (1.11.0)
85
- globalid (1.0.0)
86
- activesupport (>= 5.0)
87
- i18n (1.12.0)
92
+ drb (2.2.1)
93
+ erubi (1.13.0)
94
+ globalid (1.2.1)
95
+ activesupport (>= 6.1)
96
+ i18n (1.14.5)
88
97
  concurrent-ruby (~> 1.0)
98
+ io-console (0.7.2)
99
+ irb (1.14.0)
100
+ rdoc (>= 4.0.0)
101
+ reline (>= 0.4.2)
89
102
  json (2.6.2)
90
- loofah (2.18.0)
103
+ logger (1.6.0)
104
+ loofah (2.22.0)
91
105
  crass (~> 1.0.2)
92
- nokogiri (>= 1.5.9)
93
- mail (2.7.1)
106
+ nokogiri (>= 1.12.0)
107
+ mail (2.8.1)
94
108
  mini_mime (>= 0.1.1)
95
- marcel (1.0.2)
96
- method_source (1.0.0)
97
- mini_mime (1.1.2)
98
- mini_portile2 (2.8.0)
99
- minitest (5.16.3)
100
- net-imap (0.2.3)
101
- digest
109
+ net-imap
110
+ net-pop
111
+ net-smtp
112
+ marcel (1.0.4)
113
+ mini_mime (1.1.5)
114
+ mini_portile2 (2.8.7)
115
+ minitest (5.25.1)
116
+ net-imap (0.4.14)
117
+ date
102
118
  net-protocol
103
- strscan
104
- net-pop (0.1.1)
105
- digest
119
+ net-pop (0.1.2)
106
120
  net-protocol
121
+ net-protocol (0.2.2)
107
122
  timeout
108
- net-protocol (0.1.3)
109
- timeout
110
- net-smtp (0.3.1)
111
- digest
123
+ net-smtp (0.5.0)
112
124
  net-protocol
113
- timeout
114
- nio4r (2.5.8)
115
- nokogiri (1.13.8)
116
- mini_portile2 (~> 2.8.0)
125
+ nio4r (2.7.3)
126
+ nokogiri (1.16.7)
127
+ mini_portile2 (~> 2.8.2)
117
128
  racc (~> 1.4)
118
- pagy (5.10.1)
119
- activesupport
129
+ pagy (9.0.5)
120
130
  parallel (1.22.1)
121
131
  parser (3.1.2.1)
122
132
  ast (~> 2.4.1)
123
- pundit (2.2.0)
133
+ psych (5.1.2)
134
+ stringio
135
+ pundit (2.3.2)
124
136
  activesupport (>= 3.0.0)
125
- racc (1.6.0)
126
- rack (2.2.4)
127
- rack-test (2.0.2)
137
+ racc (1.8.1)
138
+ rack (3.1.7)
139
+ rack-session (2.0.0)
140
+ rack (>= 3.0.0)
141
+ rack-test (2.1.0)
128
142
  rack (>= 1.3)
129
- rails (7.0.3.1)
130
- actioncable (= 7.0.3.1)
131
- actionmailbox (= 7.0.3.1)
132
- actionmailer (= 7.0.3.1)
133
- actionpack (= 7.0.3.1)
134
- actiontext (= 7.0.3.1)
135
- actionview (= 7.0.3.1)
136
- activejob (= 7.0.3.1)
137
- activemodel (= 7.0.3.1)
138
- activerecord (= 7.0.3.1)
139
- activestorage (= 7.0.3.1)
140
- activesupport (= 7.0.3.1)
143
+ rackup (2.1.0)
144
+ rack (>= 3)
145
+ webrick (~> 1.8)
146
+ rails (7.2.0)
147
+ actioncable (= 7.2.0)
148
+ actionmailbox (= 7.2.0)
149
+ actionmailer (= 7.2.0)
150
+ actionpack (= 7.2.0)
151
+ actiontext (= 7.2.0)
152
+ actionview (= 7.2.0)
153
+ activejob (= 7.2.0)
154
+ activemodel (= 7.2.0)
155
+ activerecord (= 7.2.0)
156
+ activestorage (= 7.2.0)
157
+ activesupport (= 7.2.0)
141
158
  bundler (>= 1.15.0)
142
- railties (= 7.0.3.1)
143
- rails-dom-testing (2.0.3)
144
- activesupport (>= 4.2.0)
159
+ railties (= 7.2.0)
160
+ rails-dom-testing (2.2.0)
161
+ activesupport (>= 5.0.0)
162
+ minitest
145
163
  nokogiri (>= 1.6)
146
- rails-html-sanitizer (1.4.3)
147
- loofah (~> 2.3)
148
- railties (7.0.3.1)
149
- actionpack (= 7.0.3.1)
150
- activesupport (= 7.0.3.1)
151
- method_source
164
+ rails-html-sanitizer (1.6.0)
165
+ loofah (~> 2.21)
166
+ nokogiri (~> 1.14)
167
+ railties (7.2.0)
168
+ actionpack (= 7.2.0)
169
+ activesupport (= 7.2.0)
170
+ irb (~> 1.13)
171
+ rackup (>= 1.0.0)
152
172
  rake (>= 12.2)
153
- thor (~> 1.0)
154
- zeitwerk (~> 2.5)
173
+ thor (~> 1.0, >= 1.2.2)
174
+ zeitwerk (~> 2.6)
155
175
  rainbow (3.1.1)
156
176
  rake (13.0.6)
157
- ransack (3.2.1)
177
+ ransack (4.2.1)
158
178
  activerecord (>= 6.1.5)
159
179
  activesupport (>= 6.1.5)
160
180
  i18n
181
+ rdoc (6.7.0)
182
+ psych (>= 4.0.0)
161
183
  regexp_parser (2.5.0)
184
+ reline (0.5.9)
185
+ io-console (~> 0.5)
162
186
  rexml (3.2.5)
163
187
  rspec (3.11.0)
164
188
  rspec-core (~> 3.11.0)
@@ -193,16 +217,19 @@ GEM
193
217
  rubocop-rspec (2.12.1)
194
218
  rubocop (~> 1.31)
195
219
  ruby-progressbar (1.11.0)
196
- strscan (3.0.4)
197
- thor (1.2.1)
198
- timeout (0.3.0)
199
- tzinfo (2.0.5)
220
+ securerandom (0.3.1)
221
+ stringio (3.1.1)
222
+ thor (1.3.1)
223
+ timeout (0.4.1)
224
+ tzinfo (2.0.6)
200
225
  concurrent-ruby (~> 1.0)
201
226
  unicode-display_width (2.2.0)
202
- websocket-driver (0.7.5)
227
+ useragent (0.16.10)
228
+ webrick (1.8.1)
229
+ websocket-driver (0.7.6)
203
230
  websocket-extensions (>= 0.1.0)
204
231
  websocket-extensions (0.1.5)
205
- zeitwerk (2.6.0)
232
+ zeitwerk (2.6.17)
206
233
 
207
234
  PLATFORMS
208
235
  ruby
data/README.md CHANGED
@@ -1,20 +1,46 @@
1
- # AuxiliaryRailsResourceable
1
+ # AuxiliaryRails::Resourceable
2
+
3
+ [![Gem](https://img.shields.io/gem/v/auxiliary_rails_resourceable.svg)](https://rubygems.org/gems/auxiliary_rails_resourceable)
4
+
5
+ `Resourceable` module speeds up development by loading your controllers with RESTful (CRUD) actions.
6
+
7
+ It is powered with search ([ransack](https://github.com/activerecord-hackery/ransack)), pagination ([pagy](https://github.com/ddnexus/pagy)) and policies ([pundit](https://github.com/varvet/pundit)).
8
+ All these make your controllers and views much cleaner, so you can just focus on what is important.
9
+
10
+ `Resourceable` aims to be as simple and flexible as possible and suggests customization using regular method overwriting instead of tricky configs or DSLs.
11
+ Of course, it may not cover all possible cases, but if you need something really custom - just write custom code.
12
+
2
13
 
3
14
  ## Installation
4
15
 
5
- Add this line to your application's `Gemfile`:
16
+ Add one of these lines to your application's `Gemfile`:
6
17
 
7
18
  ```ruby
19
+ # version released to RubyGems (recommended)
8
20
  gem 'auxiliary_rails_resourceable'
9
- ```
10
-
11
- And then execute:
12
21
 
13
- $ bundle install
22
+ # or latest development version from a specific branch of the GitHub repository
23
+ gem 'auxiliary_rails_resourceable',
24
+ git: 'https://github.com/ergoserv/auxiliary_rails_resourceable',
25
+ branch: 'develop'
26
+ # or from a local path (for development and testing purposes)
27
+ gem 'auxiliary_rails_resourceable',
28
+ path: '../auxiliary_rails_resourceable'
29
+ ```
14
30
 
15
- Or install it yourself as:
31
+ Copy `resources.en.yml` to `config/locales/resources.en.yml`.
16
32
 
17
- $ gem install auxiliary_rails_resourceable
33
+ Create shared views in `app/views/resources/` (or `app/views/resourceable/` if your need to share views for modules).
34
+ Recommended structure for views:
35
+ ```
36
+ index.html.erb
37
+ new.html.erb
38
+ show.html.erb
39
+ edit.html.erb
40
+ _form.html.erb
41
+ _list.html.erb
42
+ _search_form.html.erb
43
+ ```
18
44
 
19
45
  ## Usage
20
46
 
@@ -25,26 +51,46 @@ class ResourcesController < ApplicationController
25
51
  include AuxiliaryRails::Concerns::Resourceable
26
52
  end
27
53
 
28
- # app/views/resources/
29
- # - index.html.erb
30
- # - new.html.erb
31
- # - show.html.erb
32
- # - edit.html.erb
33
- # - _form.html.erb
34
- # - _search_form.html.erb
35
- # - _list.html.erb
36
-
37
- # /config/locales/resources.en.yml
38
- en:
39
- resources:
40
- create:
41
- notice: "%{resource_name} was successfully created."
42
- update:
43
- notice: "%{resource_name} was successfully updated."
44
- destroy:
45
- notice: "%{resource_name} was successfully destroyed."
54
+ # app/controllers/products_controller.rb
55
+ class ProductsController < ResourcesController
56
+ end
46
57
  ```
47
58
 
59
+ ### Customization
60
+
61
+ `Resourceable` is a very simple and clear module, just read its code and overwrite
62
+ what you need in `ResourcesController` or any of its subclasses, e.g.:
63
+
64
+ ```ruby
65
+ # app/controllers/products_controller.rb
66
+ class ProductsController < ResourcesController
67
+ # Load additional data for views
68
+ def index
69
+ super
70
+ self.collection = collection.includes([:category])
71
+ end
72
+
73
+ protected
74
+
75
+ # Add any additional scopes to the collection scope
76
+ def collection_scope
77
+ policy_scope(Product).active.in_stock
78
+ end
79
+
80
+ # Overwrite Pagy configs using Pagy's methods
81
+ # https://github.com/ddnexus/pagy/blob/master/lib/pagy/backend.rb#L19
82
+ def pagy_get_vars(collection, vars)
83
+ vars[:items] = 250
84
+ super
85
+ end
86
+ end
87
+ ```
88
+
89
+ ## References
90
+
91
+ This gem is heavily inspired by [inherited_resources](https://github.com/activeadmin/inherited_resources) and [activeadmin](https://github.com/activeadmin/activeadmin).
92
+ It even follows the naming for helpers of `inherited_resources` and, in simple cases, can be used as a drop-in replacement with no or just a few code changes.
93
+
48
94
  ## Development
49
95
 
50
96
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -0,0 +1,8 @@
1
+ en:
2
+ resources:
3
+ create:
4
+ notice: "%{resource_name} was successfully created."
5
+ update:
6
+ notice: "%{resource_name} was successfully updated."
7
+ destroy:
8
+ notice: "%{resource_name} was successfully destroyed."
@@ -7,18 +7,15 @@ module AuxiliaryRails
7
7
  include Pundit::Authorization
8
8
 
9
9
  included do
10
- before_action only: %i[index new] do
11
- authorize resource_class
12
- end
13
- before_action :resource, only: %i[show edit destroy] do
14
- authorize resource
15
- end
16
-
10
+ before_action :authorize_resource_class, only: %i[index new create]
11
+ before_action :authorize_resource, only: %i[show edit update destroy]
12
+ before_action :set_lookup_context_prefixes
17
13
  after_action :verify_authorized
18
14
  after_action :verify_policy_scoped, only: %i[index]
19
15
 
20
16
  helper_method :collection
21
17
  helper_method :collection_path
18
+ helper_method :form_resource
22
19
  helper_method :resource
23
20
  helper_method :resource_class
24
21
  helper_method :resource_params
@@ -27,24 +24,28 @@ module AuxiliaryRails
27
24
  end
28
25
 
29
26
  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))
27
+ @ransack = collection.ransack(search_params)
28
+ @ransack.sorts = default_sorts if @ransack.sorts.empty?
29
+ @pagy, self.collection = pagy(@ransack.result)
33
30
  end
34
31
 
35
32
  def new
36
- self.resource = build_resource
33
+ self.resource =
34
+ if params.key?(resource_name)
35
+ build_resource(resource_params)
36
+ else
37
+ build_resource
38
+ end
37
39
  end
38
40
 
39
41
  def create
40
42
  self.resource = build_resource(resource_params)
41
43
 
42
- authorize resource
43
-
44
44
  if resource.save
45
- redirect_to resource_path(resource),
46
- notice: t('resources.create.notice',
47
- resource_name: resource_class.model_name.human)
45
+ respond_to do |format|
46
+ format.html { redirect_after_create }
47
+ format.json
48
+ end
48
49
  else
49
50
  render :new
50
51
  end
@@ -59,12 +60,11 @@ module AuxiliaryRails
59
60
  def update
60
61
  resource.assign_attributes(resource_params)
61
62
 
62
- authorize resource
63
-
64
63
  if resource.save
65
- redirect_to resource_path(resource),
66
- notice: t('resources.update.notice',
67
- resource_name: resource_class.model_name.human)
64
+ respond_to do |format|
65
+ format.html { redirect_after_update }
66
+ format.json
67
+ end
68
68
  else
69
69
  render :edit
70
70
  end
@@ -73,53 +73,91 @@ module AuxiliaryRails
73
73
  def destroy
74
74
  resource.destroy
75
75
 
76
- redirect_to collection_path,
77
- notice: t('resources.destroy.notice',
78
- resource_name: resource_class.model_name.human)
76
+ respond_to do |format|
77
+ format.html { redirect_after_destroy }
78
+ format.json
79
+ end
79
80
  end
80
81
 
81
82
  protected
82
83
 
83
- # config
84
+ # configs
85
+
86
+ def authorize_resource(query = nil, policy_class: nil)
87
+ authorize resource, query, policy_class: policy_class
88
+ end
89
+
90
+ def authorize_resource_class(query = nil, policy_class: nil)
91
+ authorize resource_class, query, policy_class: policy_class
92
+ end
84
93
 
85
94
  def default_sorts
86
- ['id asc']
95
+ %w[name title id].find { |v| v.in?(resource_class.column_names) } || []
96
+ end
97
+
98
+ # Defines an I18n scope for flash messages
99
+ #
100
+ # Possible options to use instead of `:resources`:
101
+ # - `controller_name`
102
+ # - `"#{controller_module_name}/resources"`
103
+ # - `"#{controller_module_name}/#{controller_name}"`
104
+ # - any other manual scope
105
+ def i18n_scope
106
+ :resources
87
107
  end
88
108
 
89
109
  def id_param
90
110
  params[:id]
91
111
  end
92
112
 
113
+ def search_params
114
+ params[:q]
115
+ end
116
+
93
117
  def collection_name
94
118
  @collection_name ||= controller_name
95
119
  end
96
120
 
121
+ def collection_scope
122
+ policy_scope(resource_class).all
123
+ end
124
+
97
125
  def resource_class
98
126
  @resource_class ||= resource_name.camelize.constantize
99
127
  end
100
128
 
101
129
  def resource_name
102
- @resource_name ||= controller_name.singularize
130
+ @resource_name ||= collection_name.singularize
131
+ end
132
+
133
+ # actions
134
+
135
+ def build_resource(attributes = {})
136
+ resource_class.new(attributes)
137
+ end
138
+
139
+ def find_resource
140
+ resource_class.find(id_param)
103
141
  end
104
142
 
105
143
  # helpers
106
144
 
107
145
  def collection
108
- instance_variable_get("@#{collection_name}") ||
109
- (self.collection = resource_scope)
146
+ instance_variable_get(:"@#{collection_name}") ||
147
+ (self.collection = collection_scope)
110
148
  end
111
149
 
112
150
  def collection=(object)
113
- instance_variable_set("@#{collection_name}", object)
151
+ instance_variable_set(:"@#{collection_name}", object)
114
152
  end
115
153
 
116
154
  def resource
117
- instance_variable_get("@#{resource_name}") ||
155
+ instance_variable_get(:"@#{resource_name}") ||
118
156
  (self.resource = find_resource)
119
157
  end
120
158
 
121
159
  def resource=(object)
122
- instance_variable_set("@#{resource_name}", object)
160
+ instance_variable_set(:"@#{resource_name}", object)
123
161
  end
124
162
 
125
163
  def resource_params
@@ -128,16 +166,12 @@ module AuxiliaryRails
128
166
  .permit(policy(resource_class).permitted_attributes)
129
167
  end
130
168
 
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)
169
+ def form_resource
170
+ if controller_module_name.blank?
171
+ resource
172
+ else
173
+ [controller_module_name.to_sym, resource]
174
+ end
141
175
  end
142
176
 
143
177
  def collection_path
@@ -152,35 +186,60 @@ module AuxiliaryRails
152
186
  public_send(path_method_name(:resource, :new))
153
187
  end
154
188
 
155
- # system
189
+ # redirects
156
190
 
157
- def controller_module_parent
158
- if Rails.version < '6'
159
- raise NotImplementedError,
160
- '`controller_module_parent` needs to be implented because ' \
161
- 'Rails < 6 does not supports `module_parent`'
191
+ def redirect_after_create
192
+ if flash[:notice].blank?
193
+ flash[:notice] = t('create.notice',
194
+ resource_name: resource_class.model_name.human,
195
+ scope: i18n_scope)
162
196
  end
163
197
 
164
- self.class.module_parent
198
+ redirect_to resource_path(resource)
165
199
  end
166
200
 
167
- # rubocop:disable Metrics/MethodLength
168
- def path_method_name(type, action = nil)
169
- path_parts = [
170
- 'path',
171
- (type.to_sym == :resource ? resource_name : controller_name)
172
- ]
201
+ def redirect_after_update
202
+ if flash[:notice].blank?
203
+ flash[:notice] = t('update.notice',
204
+ resource_name: resource_class.model_name.human,
205
+ scope: i18n_scope)
206
+ end
173
207
 
174
- if controller_module_parent != Object
175
- path_parts << controller_module_parent.to_s.underscore
208
+ redirect_to resource_path(resource)
209
+ end
210
+
211
+ def redirect_after_destroy
212
+ if flash[:notice].blank?
213
+ flash[:notice] = t('destroy.notice',
214
+ resource_name: resource_class.model_name.human,
215
+ scope: i18n_scope)
176
216
  end
177
- if action.present?
178
- path_parts << action
217
+
218
+ redirect_to collection_path
219
+ end
220
+
221
+ # system
222
+
223
+ def controller_module_name
224
+ if Rails.version < '6'
225
+ return self.class.parent_name&.underscore
179
226
  end
180
227
 
181
- path_parts.reverse.join('_')
228
+ self.class.module_parent_name&.underscore
229
+ end
230
+
231
+ def path_method_name(type, action = nil)
232
+ [
233
+ action,
234
+ controller_module_name,
235
+ (type.to_sym == :resource ? resource_name : controller_name),
236
+ 'path'
237
+ ].compact.join('_')
238
+ end
239
+
240
+ def set_lookup_context_prefixes
241
+ lookup_context.prefixes << 'resourceable'
182
242
  end
183
- # rubocop:enable Metrics/MethodLength
184
243
  end
185
244
  end
186
245
  end
@@ -0,0 +1,32 @@
1
+ module AuxiliaryRails
2
+ module ViewHelpers
3
+ module HumanNameHelper
4
+ # Human-readable name for a model or a model's attribute
5
+ #
6
+ # @param model [Class, Object] The model class or an instance of the model
7
+ # @param attribute [Symbol, String, nil] The attribute name (optional)
8
+ # @return [String] The human-readable name
9
+ def human_name(model, attribute = nil, count: 1)
10
+ if attribute.nil?
11
+ model.model_name.human(count: count)
12
+ else
13
+ model = model.class unless model.is_a?(Class)
14
+ if count > 1
15
+ model.human_attribute_name(attribute).pluralize(count)
16
+ else
17
+ model.human_attribute_name(attribute)
18
+ end
19
+ end
20
+ end
21
+
22
+ # Human-readable name for a model or a model's attribute plural form
23
+ #
24
+ # @param model [Class, Object] The model class or an instance of the model
25
+ # @param attribute [Symbol, String, nil] The attribute name (optional)
26
+ # @return [String] The human-readable name in plural form
27
+ def human_name_plural(model, attribute = nil, count: 2)
28
+ human_name(model, attribute, count: count)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,10 @@
1
+ require 'rails/railtie'
2
+ require 'auxiliary_rails/view_helpers/human_name_helper'
3
+
4
+ module AuxiliaryRails
5
+ class Railtie < Rails::Railtie
6
+ initializer 'auxiliary_rails.view_helpers' do
7
+ ActionView::Base.include AuxiliaryRails::ViewHelpers::HumanNameHelper
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module AuxiliaryRailsResourceable
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.1.2'.freeze
3
3
  end
@@ -1,4 +1,5 @@
1
1
  require_relative 'auxiliary_rails_resourceable/version'
2
+ require_relative 'auxiliary_rails_resourceable/railtie'
2
3
 
3
4
  require 'pagy'
4
5
  require 'pundit'
metadata CHANGED
@@ -1,15 +1,15 @@
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.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Babenko
8
8
  - ErgoServ
9
- autorequire:
9
+ autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2022-08-20 00:00:00.000000000 Z
12
+ date: 2024-08-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: pagy
@@ -67,7 +67,7 @@ dependencies:
67
67
  - - ">="
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
- description:
70
+ description:
71
71
  email:
72
72
  - dmitry@ergoserv.com
73
73
  - hello@ergoserv.com
@@ -85,8 +85,11 @@ files:
85
85
  - README.md
86
86
  - Rakefile
87
87
  - auxiliary_rails_resourceable.gemspec
88
+ - config/locales/resources.en.yml
88
89
  - lib/auxiliary_rails/concerns/resourceable.rb
90
+ - lib/auxiliary_rails/view_helpers/human_name_helper.rb
89
91
  - lib/auxiliary_rails_resourceable.rb
92
+ - lib/auxiliary_rails_resourceable/railtie.rb
90
93
  - lib/auxiliary_rails_resourceable/version.rb
91
94
  - sig/auxiliary_rails_resourceable.rbs
92
95
  homepage: https://github.com/ergoserv/auxiliary_rails_resourceable
@@ -97,7 +100,7 @@ metadata:
97
100
  source_code_uri: https://github.com/ergoserv/auxiliary_rails_resourceable
98
101
  changelog_uri: https://github.com/ergoserv/auxiliary_rails_resourceable
99
102
  rubygems_mfa_required: 'true'
100
- post_install_message:
103
+ post_install_message:
101
104
  rdoc_options: []
102
105
  require_paths:
103
106
  - lib
@@ -112,8 +115,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
115
  - !ruby/object:Gem::Version
113
116
  version: '0'
114
117
  requirements: []
115
- rubygems_version: 3.1.2
116
- signing_key:
118
+ rubygems_version: 3.5.17
119
+ signing_key:
117
120
  specification_version: 4
118
121
  summary: AuxiliaryRails - Resourceable Controller
119
122
  test_files: []