auxiliary_rails_resourceable 0.1.1 → 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: 0cccc3798217d2f3f64d9250d46a5797ca20e4d7aba351d36d6c9ae938d0ce22
4
- data.tar.gz: f014a530c24a038df4b312336c81e3beb5e510eebc6dc6449d43c6cc2080ea46
3
+ metadata.gz: 2652d651562f292661ab1e5bab3e5f360092dce54bb8d6a26dae20833d3534e4
4
+ data.tar.gz: 75deec7ba21c12b6d746ab5c3c3bb91e55cd781beec2a4749896c46225756eb3
5
5
  SHA512:
6
- metadata.gz: 312ac5eaea723c5ddce906ebe9e50d0a8cbd7572b007f4798ec2178d5fc831ab39e6acbff4d85fd0a616b6574c6c03b593e2e8371cf635492a26992eceb2aab2
7
- data.tar.gz: 76ad31ecd517a89bab159b0c1e672cf9ff2fc3e08669399f4074808624de8519904d4de54ca5c66d18e53d5e15df2b7944c7ad9b8cb129e7b80658788c139f20
6
+ metadata.gz: 55a32aaa16651fed49f565d09d9e43f690172d2ec4f4875bac6e20e25bd983e01586db22a08eb24bf805a1a7ad182626f128a180b8ff5859d726b90060875dba
7
+ data.tar.gz: b49c1bd45598e9da86439cf5ec29bd0203600aadb079895f5027a083fad3a22d1558cfcd94416d06c094b5e0f4cd9e3e1cb1a5fa56afeacf4393ff765742c64e
data/.rubocop.yml CHANGED
@@ -40,3 +40,8 @@ Style/IfUnlessModifier:
40
40
 
41
41
  Style/EmptyMethod:
42
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,11 @@
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
+
3
9
  ## [0.1.1] - 2022-08-27
4
10
 
5
11
  ## [0.1.0] - 2022-08-20
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- auxiliary_rails_resourceable (0.1.1)
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,22 +1,46 @@
1
- # AuxiliaryRailsResourceable
1
+ # AuxiliaryRails::Resourceable
2
2
 
3
3
  [![Gem](https://img.shields.io/gem/v/auxiliary_rails_resourceable.svg)](https://rubygems.org/gems/auxiliary_rails_resourceable)
4
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
+
13
+
5
14
  ## Installation
6
15
 
7
- Add this line to your application's `Gemfile`:
16
+ Add one of these lines to your application's `Gemfile`:
8
17
 
9
18
  ```ruby
19
+ # version released to RubyGems (recommended)
10
20
  gem 'auxiliary_rails_resourceable'
11
- ```
12
21
 
13
- And then execute:
14
-
15
- $ 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
+ ```
16
30
 
17
- Or install it yourself as:
31
+ Copy `resources.en.yml` to `config/locales/resources.en.yml`.
18
32
 
19
- $ 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
+ ```
20
44
 
21
45
  ## Usage
22
46
 
@@ -27,26 +51,46 @@ class ResourcesController < ApplicationController
27
51
  include AuxiliaryRails::Concerns::Resourceable
28
52
  end
29
53
 
30
- # app/views/resources/
31
- # - index.html.erb
32
- # - new.html.erb
33
- # - show.html.erb
34
- # - edit.html.erb
35
- # - _form.html.erb
36
- # - _list.html.erb
37
- # - _search_form.html.erb
38
-
39
- # /config/locales/resources.en.yml
40
- en:
41
- resources:
42
- create:
43
- notice: "%{resource_name} was successfully created."
44
- update:
45
- notice: "%{resource_name} was successfully updated."
46
- destroy:
47
- notice: "%{resource_name} was successfully destroyed."
54
+ # app/controllers/products_controller.rb
55
+ class ProductsController < ResourcesController
56
+ end
48
57
  ```
49
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
+
50
94
  ## Development
51
95
 
52
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] 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
@@ -33,12 +30,22 @@ module AuxiliaryRails
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
- if create_resource(resource_params)
41
- redirect_after_create
42
+ self.resource = build_resource(resource_params)
43
+
44
+ if resource.save
45
+ respond_to do |format|
46
+ format.html { redirect_after_create }
47
+ format.json
48
+ end
42
49
  else
43
50
  render :new
44
51
  end
@@ -51,30 +58,50 @@ module AuxiliaryRails
51
58
  end
52
59
 
53
60
  def update
54
- if update_resource(resource_params)
55
- redirect_after_update
61
+ resource.assign_attributes(resource_params)
62
+
63
+ if resource.save
64
+ respond_to do |format|
65
+ format.html { redirect_after_update }
66
+ format.json
67
+ end
56
68
  else
57
69
  render :edit
58
70
  end
59
71
  end
60
72
 
61
73
  def destroy
62
- destroy_resource
74
+ resource.destroy
63
75
 
64
- redirect_after_destroy
76
+ respond_to do |format|
77
+ format.html { redirect_after_destroy }
78
+ format.json
79
+ end
65
80
  end
66
81
 
67
82
  protected
68
83
 
69
84
  # configs
70
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
93
+
71
94
  def default_sorts
72
95
  %w[name title id].find { |v| v.in?(resource_class.column_names) } || []
73
96
  end
74
97
 
75
98
  # Defines an I18n scope for flash messages
76
99
  #
77
- # Use `controller_name` for scoping as the name of the controller.
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
78
105
  def i18n_scope
79
106
  :resources
80
107
  end
@@ -109,50 +136,28 @@ module AuxiliaryRails
109
136
  resource_class.new(attributes)
110
137
  end
111
138
 
112
- def create_resource(attributes = {})
113
- self.resource = build_resource(attributes)
114
-
115
- authorize resource, :create?
116
-
117
- resource.save
118
- end
119
-
120
139
  def find_resource
121
140
  resource_class.find(id_param)
122
141
  end
123
142
 
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
136
- end
137
-
138
143
  # helpers
139
144
 
140
145
  def collection
141
- instance_variable_get("@#{collection_name}") ||
146
+ instance_variable_get(:"@#{collection_name}") ||
142
147
  (self.collection = collection_scope)
143
148
  end
144
149
 
145
150
  def collection=(object)
146
- instance_variable_set("@#{collection_name}", object)
151
+ instance_variable_set(:"@#{collection_name}", object)
147
152
  end
148
153
 
149
154
  def resource
150
- instance_variable_get("@#{resource_name}") ||
155
+ instance_variable_get(:"@#{resource_name}") ||
151
156
  (self.resource = find_resource)
152
157
  end
153
158
 
154
159
  def resource=(object)
155
- instance_variable_set("@#{resource_name}", object)
160
+ instance_variable_set(:"@#{resource_name}", object)
156
161
  end
157
162
 
158
163
  def resource_params
@@ -161,6 +166,14 @@ module AuxiliaryRails
161
166
  .permit(policy(resource_class).permitted_attributes)
162
167
  end
163
168
 
169
+ def form_resource
170
+ if controller_module_name.blank?
171
+ resource
172
+ else
173
+ [controller_module_name.to_sym, resource]
174
+ end
175
+ end
176
+
164
177
  def collection_path
165
178
  public_send(path_method_name(:collection))
166
179
  end
@@ -176,44 +189,57 @@ module AuxiliaryRails
176
189
  # redirects
177
190
 
178
191
  def redirect_after_create
179
- redirect_to resource_path(resource),
180
- notice: t('create.notice',
192
+ if flash[:notice].blank?
193
+ flash[:notice] = t('create.notice',
181
194
  resource_name: resource_class.model_name.human,
182
195
  scope: i18n_scope)
196
+ end
197
+
198
+ redirect_to resource_path(resource)
183
199
  end
184
200
 
185
201
  def redirect_after_update
186
- redirect_to resource_path(resource),
187
- notice: t('update.notice',
202
+ if flash[:notice].blank?
203
+ flash[:notice] = t('update.notice',
188
204
  resource_name: resource_class.model_name.human,
189
205
  scope: i18n_scope)
206
+ end
207
+
208
+ redirect_to resource_path(resource)
190
209
  end
191
210
 
192
211
  def redirect_after_destroy
193
- redirect_to collection_path,
194
- notice: t('destroy.notice',
212
+ if flash[:notice].blank?
213
+ flash[:notice] = t('destroy.notice',
195
214
  resource_name: resource_class.model_name.human,
196
215
  scope: i18n_scope)
216
+ end
217
+
218
+ redirect_to collection_path
197
219
  end
198
220
 
199
221
  # system
200
222
 
201
- def controller_module_parent_name
223
+ def controller_module_name
202
224
  if Rails.version < '6'
203
- return self.class.parent_name
225
+ return self.class.parent_name&.underscore
204
226
  end
205
227
 
206
- self.class.module_parent_name
228
+ self.class.module_parent_name&.underscore
207
229
  end
208
230
 
209
231
  def path_method_name(type, action = nil)
210
232
  [
211
233
  action,
212
- controller_module_parent_name&.underscore,
234
+ controller_module_name,
213
235
  (type.to_sym == :resource ? resource_name : controller_name),
214
236
  'path'
215
237
  ].compact.join('_')
216
238
  end
239
+
240
+ def set_lookup_context_prefixes
241
+ lookup_context.prefixes << 'resourceable'
242
+ end
217
243
  end
218
244
  end
219
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.1'.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.1
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-27 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: []