auxiliary_rails_resourceable 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4626f97cfe6bb22a70da7b258f5fe4f742f34f2b1584c76fb032125969f35cbb
4
+ data.tar.gz: b08f4f1a0dcdb4a2a01c9ced689032c176ed843a63301686534590712ed38760
5
+ SHA512:
6
+ metadata.gz: 7138e711032c8cbb19a8542770f30aab514b2646069d2a5a71a4315b992d5e4f246e7f7dabc63560dbf9acfafd6f3c6819e19e944a625c6cdd8fa784d39c993e
7
+ data.tar.gz: 68cf0c091e8a4b95661b51abf4f39d4e00faca9ee3030e5ea0cd361c03d6e7a599ded540e0e131255ec019a21584bd6e3fcb15bdce51d602b8b9b5f81030b3d4
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,35 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rake
4
+ - rubocop-rspec
5
+
6
+ AllCops:
7
+ TargetRubyVersion: 2.5
8
+ NewCops: enable
9
+ #################### Layout ##############################
10
+
11
+ Layout/ArgumentAlignment:
12
+ EnforcedStyle: with_fixed_indentation
13
+
14
+ Layout/LineEndStringConcatenationIndentation:
15
+ EnforcedStyle: indented
16
+
17
+ Layout/MultilineMethodCallIndentation:
18
+ EnforcedStyle: indented
19
+
20
+ #################### Metrics ##############################
21
+
22
+ Metrics/ModuleLength:
23
+ Exclude:
24
+ - lib/auxiliary_rails/concerns/resourceable.rb
25
+
26
+ #################### Style ###############################
27
+
28
+ Style/FrozenStringLiteralComment:
29
+ Enabled: false
30
+
31
+ Style/IfUnlessModifier:
32
+ Enabled: false
33
+
34
+ Style/EmptyMethod:
35
+ EnforcedStyle: expanded
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-08-20
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at dmitry@ergoserv.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rake', '~> 13.0'
6
+ gem 'rspec', '~> 3.0'
7
+ gem 'rubocop', '~> 1.21'
8
+ gem 'rubocop-performance'
9
+ gem 'rubocop-rake'
10
+ gem 'rubocop-rspec'
data/Gemfile.lock ADDED
@@ -0,0 +1,220 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ auxiliary_rails_resourceable (0.1.0)
5
+ pagy
6
+ pundit
7
+ rails (>= 5.2)
8
+ ransack
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ actioncable (7.0.3.1)
14
+ actionpack (= 7.0.3.1)
15
+ activesupport (= 7.0.3.1)
16
+ nio4r (~> 2.0)
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)
42
+ 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)
50
+ globalid (>= 0.6.0)
51
+ nokogiri (>= 1.8.5)
52
+ actionview (7.0.3.1)
53
+ activesupport (= 7.0.3.1)
54
+ 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)
60
+ 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)
71
+ marcel (~> 1.0)
72
+ mini_mime (>= 1.1.0)
73
+ activesupport (7.0.3.1)
74
+ concurrent-ruby (~> 1.0, >= 1.0.2)
75
+ i18n (>= 1.6, < 2)
76
+ minitest (>= 5.1)
77
+ tzinfo (~> 2.0)
78
+ ast (2.4.2)
79
+ builder (3.2.4)
80
+ concurrent-ruby (1.1.10)
81
+ crass (1.0.6)
82
+ 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)
88
+ concurrent-ruby (~> 1.0)
89
+ json (2.6.2)
90
+ loofah (2.18.0)
91
+ crass (~> 1.0.2)
92
+ nokogiri (>= 1.5.9)
93
+ mail (2.7.1)
94
+ 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
102
+ net-protocol
103
+ strscan
104
+ net-pop (0.1.1)
105
+ digest
106
+ net-protocol
107
+ timeout
108
+ net-protocol (0.1.3)
109
+ timeout
110
+ net-smtp (0.3.1)
111
+ digest
112
+ net-protocol
113
+ timeout
114
+ nio4r (2.5.8)
115
+ nokogiri (1.13.8)
116
+ mini_portile2 (~> 2.8.0)
117
+ racc (~> 1.4)
118
+ pagy (5.10.1)
119
+ activesupport
120
+ parallel (1.22.1)
121
+ parser (3.1.2.1)
122
+ ast (~> 2.4.1)
123
+ pundit (2.2.0)
124
+ activesupport (>= 3.0.0)
125
+ racc (1.6.0)
126
+ rack (2.2.4)
127
+ rack-test (2.0.2)
128
+ 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)
141
+ bundler (>= 1.15.0)
142
+ railties (= 7.0.3.1)
143
+ rails-dom-testing (2.0.3)
144
+ activesupport (>= 4.2.0)
145
+ 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
152
+ rake (>= 12.2)
153
+ thor (~> 1.0)
154
+ zeitwerk (~> 2.5)
155
+ rainbow (3.1.1)
156
+ rake (13.0.6)
157
+ ransack (3.2.1)
158
+ activerecord (>= 6.1.5)
159
+ activesupport (>= 6.1.5)
160
+ i18n
161
+ regexp_parser (2.5.0)
162
+ rexml (3.2.5)
163
+ rspec (3.11.0)
164
+ rspec-core (~> 3.11.0)
165
+ rspec-expectations (~> 3.11.0)
166
+ rspec-mocks (~> 3.11.0)
167
+ rspec-core (3.11.0)
168
+ rspec-support (~> 3.11.0)
169
+ rspec-expectations (3.11.0)
170
+ diff-lcs (>= 1.2.0, < 2.0)
171
+ rspec-support (~> 3.11.0)
172
+ rspec-mocks (3.11.1)
173
+ diff-lcs (>= 1.2.0, < 2.0)
174
+ rspec-support (~> 3.11.0)
175
+ rspec-support (3.11.0)
176
+ rubocop (1.35.0)
177
+ json (~> 2.3)
178
+ parallel (~> 1.10)
179
+ parser (>= 3.1.2.1)
180
+ rainbow (>= 2.2.2, < 4.0)
181
+ regexp_parser (>= 1.8, < 3.0)
182
+ rexml (>= 3.2.5, < 4.0)
183
+ rubocop-ast (>= 1.20.1, < 2.0)
184
+ ruby-progressbar (~> 1.7)
185
+ unicode-display_width (>= 1.4.0, < 3.0)
186
+ rubocop-ast (1.21.0)
187
+ parser (>= 3.1.1.0)
188
+ rubocop-performance (1.14.3)
189
+ rubocop (>= 1.7.0, < 2.0)
190
+ rubocop-ast (>= 0.4.0)
191
+ rubocop-rake (0.6.0)
192
+ rubocop (~> 1.0)
193
+ rubocop-rspec (2.12.1)
194
+ rubocop (~> 1.31)
195
+ 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)
200
+ concurrent-ruby (~> 1.0)
201
+ unicode-display_width (2.2.0)
202
+ websocket-driver (0.7.5)
203
+ websocket-extensions (>= 0.1.0)
204
+ websocket-extensions (0.1.5)
205
+ zeitwerk (2.6.0)
206
+
207
+ PLATFORMS
208
+ ruby
209
+
210
+ DEPENDENCIES
211
+ auxiliary_rails_resourceable!
212
+ rake (~> 13.0)
213
+ rspec (~> 3.0)
214
+ rubocop (~> 1.21)
215
+ rubocop-performance
216
+ rubocop-rake
217
+ rubocop-rspec
218
+
219
+ BUNDLED WITH
220
+ 2.1.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Dmitry Babenko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,72 @@
1
+ # AuxiliaryRailsResourceable
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's `Gemfile`:
6
+
7
+ ```ruby
8
+ gem 'auxiliary_rails_resourceable'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle install
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install auxiliary_rails_resourceable
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ # app/controllers/resources_controller.rb
23
+ # @abstract
24
+ class ResourcesController < ApplicationController
25
+ include AuxiliaryRails::Concerns::Resourceable
26
+ end
27
+
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."
46
+ ```
47
+
48
+ ## Development
49
+
50
+ 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.
51
+
52
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
53
+
54
+ ## Contributing
55
+
56
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ergoserv/auxiliary_rails_resourceable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/ergoserv/auxiliary_rails_resourceable/blob/master/CODE_OF_CONDUCT.md).
57
+
58
+ ## License
59
+
60
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
61
+
62
+ ## Code of Conduct
63
+
64
+ Everyone interacting in the AuxiliaryRailsResourceable project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ergoserv/auxiliary_rails_resourceable/blob/master/CODE_OF_CONDUCT.md).
65
+
66
+ -------------------------------------------------------------------------------
67
+
68
+ [![alt text](https://raw.githubusercontent.com/ergoserv/auxiliary_rails/master/assets/ErgoServ_horizontalColor@sign+text+bg.png "ErgoServ - Web and Mobile Development Company")](https://www.ergoserv.com)
69
+
70
+ This gem was created and is maintained by [ErgoServ](https://www.ergoserv.com).
71
+
72
+ If you like what you see and would like to hire us or join us, [get in touch](https://www.ergoserv.com)!
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ require 'rubocop/rake_task'
7
+
8
+ RuboCop::RakeTask.new
9
+
10
+ task default: %i[spec rubocop]
@@ -0,0 +1,34 @@
1
+ require_relative 'lib/auxiliary_rails_resourceable/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'auxiliary_rails_resourceable'
5
+ spec.version = AuxiliaryRailsResourceable::VERSION
6
+ spec.authors = ['Dmitry Babenko', 'ErgoServ']
7
+ spec.email = ['dmitry@ergoserv.com', 'hello@ergoserv.com']
8
+
9
+ spec.summary = 'AuxiliaryRails - Resourceable Controller'
10
+ spec.homepage = 'https://github.com/ergoserv/auxiliary_rails_resourceable'
11
+ spec.license = 'MIT'
12
+ spec.required_ruby_version = '>= 2.5'
13
+
14
+ spec.metadata['homepage_uri'] = spec.homepage
15
+ spec.metadata['source_code_uri'] = spec.homepage
16
+ spec.metadata['changelog_uri'] = spec.homepage
17
+ spec.metadata['rubygems_mfa_required'] = 'true'
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject do |f|
23
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
24
+ end
25
+ end
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_dependency 'pagy'
31
+ spec.add_dependency 'pundit'
32
+ spec.add_dependency 'rails', '>= 5.2'
33
+ spec.add_dependency 'ransack'
34
+ end
@@ -0,0 +1,186 @@
1
+ module AuxiliaryRails
2
+ module Concerns
3
+ # Resourceable - concern for controllers
4
+ module Resourceable
5
+ extend ActiveSupport::Concern
6
+ include Pagy::Backend
7
+ include Pundit::Authorization
8
+
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
+
17
+ after_action :verify_authorized
18
+ after_action :verify_policy_scoped, only: %i[index]
19
+
20
+ helper_method :collection
21
+ helper_method :collection_path
22
+ helper_method :resource
23
+ helper_method :resource_class
24
+ helper_method :resource_params
25
+ helper_method :resource_path
26
+ helper_method :new_resource_path
27
+ end
28
+
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))
33
+ end
34
+
35
+ def new
36
+ self.resource = build_resource
37
+ end
38
+
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)
48
+ else
49
+ render :new
50
+ end
51
+ end
52
+
53
+ def show
54
+ end
55
+
56
+ def edit
57
+ end
58
+
59
+ 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)
68
+ else
69
+ render :edit
70
+ end
71
+ end
72
+
73
+ def destroy
74
+ resource.destroy
75
+
76
+ redirect_to collection_path,
77
+ notice: t('resources.destroy.notice',
78
+ resource_name: resource_class.model_name.human)
79
+ end
80
+
81
+ protected
82
+
83
+ # config
84
+
85
+ def default_sorts
86
+ ['id asc']
87
+ end
88
+
89
+ def id_param
90
+ params[:id]
91
+ end
92
+
93
+ def collection_name
94
+ @collection_name ||= controller_name
95
+ end
96
+
97
+ def resource_class
98
+ @resource_class ||= resource_name.camelize.constantize
99
+ end
100
+
101
+ def resource_name
102
+ @resource_name ||= controller_name.singularize
103
+ end
104
+
105
+ # helpers
106
+
107
+ def collection
108
+ instance_variable_get("@#{collection_name}") ||
109
+ (self.collection = resource_scope)
110
+ end
111
+
112
+ def collection=(object)
113
+ instance_variable_set("@#{collection_name}", object)
114
+ end
115
+
116
+ def resource
117
+ instance_variable_get("@#{resource_name}") ||
118
+ (self.resource = find_resource)
119
+ end
120
+
121
+ def resource=(object)
122
+ instance_variable_set("@#{resource_name}", object)
123
+ end
124
+
125
+ def resource_params
126
+ params
127
+ .require(resource_name)
128
+ .permit(policy(resource_class).permitted_attributes)
129
+ end
130
+
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
+ def collection_path
144
+ public_send(path_method_name(:collection))
145
+ end
146
+
147
+ def resource_path(resource, action = nil)
148
+ public_send(path_method_name(:resource, action), resource)
149
+ end
150
+
151
+ def new_resource_path
152
+ public_send(path_method_name(:resource, :new))
153
+ end
154
+
155
+ # system
156
+
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`'
162
+ end
163
+
164
+ self.class.module_parent
165
+ end
166
+
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
+ ]
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('_')
182
+ end
183
+ # rubocop:enable Metrics/MethodLength
184
+ end
185
+ end
186
+ end
@@ -0,0 +1,3 @@
1
+ module AuxiliaryRailsResourceable
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,10 @@
1
+ require_relative 'auxiliary_rails_resourceable/version'
2
+
3
+ require 'pagy'
4
+ require 'pundit'
5
+
6
+ require_relative 'auxiliary_rails/concerns/resourceable'
7
+
8
+ module AuxiliaryRailsResourceable
9
+ class Error < StandardError; end
10
+ end
@@ -0,0 +1,4 @@
1
+ module AuxiliaryRailsResourceable
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: auxiliary_rails_resourceable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dmitry Babenko
8
+ - ErgoServ
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2022-08-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: pagy
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: pundit
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rails
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '5.2'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '5.2'
56
+ - !ruby/object:Gem::Dependency
57
+ name: ransack
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ description:
71
+ email:
72
+ - dmitry@ergoserv.com
73
+ - hello@ergoserv.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".rspec"
79
+ - ".rubocop.yml"
80
+ - CHANGELOG.md
81
+ - CODE_OF_CONDUCT.md
82
+ - Gemfile
83
+ - Gemfile.lock
84
+ - LICENSE.txt
85
+ - README.md
86
+ - Rakefile
87
+ - auxiliary_rails_resourceable.gemspec
88
+ - lib/auxiliary_rails/concerns/resourceable.rb
89
+ - lib/auxiliary_rails_resourceable.rb
90
+ - lib/auxiliary_rails_resourceable/version.rb
91
+ - sig/auxiliary_rails_resourceable.rbs
92
+ homepage: https://github.com/ergoserv/auxiliary_rails_resourceable
93
+ licenses:
94
+ - MIT
95
+ metadata:
96
+ homepage_uri: https://github.com/ergoserv/auxiliary_rails_resourceable
97
+ source_code_uri: https://github.com/ergoserv/auxiliary_rails_resourceable
98
+ changelog_uri: https://github.com/ergoserv/auxiliary_rails_resourceable
99
+ rubygems_mfa_required: 'true'
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '2.5'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubygems_version: 3.1.2
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: AuxiliaryRails - Resourceable Controller
119
+ test_files: []