prefab-rails 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: 36fa92e24129e08b97863e761ca264ae7f9b66201b88e993b202b2c6dc518da6
4
+ data.tar.gz: ddad80be43e110f1450a81dcd32cadf193d7f8dbad72d92bfba8c015ad4fc252
5
+ SHA512:
6
+ metadata.gz: aa933651fbe518479f1a5705b1d5e5c8203eee791dd9dbcb3b733b3f2d6c5798f8eab4aac0d36e0667149200b263fbafedd161588343f3d59a6c57b3c0656002
7
+ data.tar.gz: 40de439031905a366add2b3167d4f55e40101fe1230319f689b36e8d8b364932eba603a6d795b7bc1307cda4281a63a88b0f1f86a1655689c38368e18e545399
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
3
+ ruby_version: 2.6
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in prefab.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "standard", "~> 1.3"
data/Gemfile.lock ADDED
@@ -0,0 +1,126 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ prefab (0.1.0)
5
+ actionpack
6
+ activesupport
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionpack (7.0.5)
12
+ actionview (= 7.0.5)
13
+ activesupport (= 7.0.5)
14
+ rack (~> 2.0, >= 2.2.4)
15
+ rack-test (>= 0.6.3)
16
+ rails-dom-testing (~> 2.0)
17
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
18
+ actionview (7.0.5)
19
+ activesupport (= 7.0.5)
20
+ builder (~> 3.1)
21
+ erubi (~> 1.4)
22
+ rails-dom-testing (~> 2.0)
23
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
24
+ activesupport (7.0.5)
25
+ concurrent-ruby (~> 1.0, >= 1.0.2)
26
+ i18n (>= 1.6, < 2)
27
+ minitest (>= 5.1)
28
+ tzinfo (~> 2.0)
29
+ ast (2.4.2)
30
+ builder (3.2.4)
31
+ concurrent-ruby (1.2.2)
32
+ crass (1.0.6)
33
+ diff-lcs (1.5.0)
34
+ erubi (1.12.0)
35
+ i18n (1.14.1)
36
+ concurrent-ruby (~> 1.0)
37
+ json (2.6.3)
38
+ language_server-protocol (3.17.0.2)
39
+ loofah (2.21.3)
40
+ crass (~> 1.0.2)
41
+ nokogiri (>= 1.12.0)
42
+ method_source (1.0.0)
43
+ minitest (5.18.1)
44
+ nokogiri (1.15.2-arm64-darwin)
45
+ racc (~> 1.4)
46
+ nokogiri (1.15.2-x86_64-linux)
47
+ racc (~> 1.4)
48
+ parallel (1.22.1)
49
+ parser (3.2.0.0)
50
+ ast (~> 2.4.1)
51
+ racc (1.7.1)
52
+ rack (2.2.7)
53
+ rack-test (2.1.0)
54
+ rack (>= 1.3)
55
+ rails-dom-testing (2.0.3)
56
+ activesupport (>= 4.2.0)
57
+ nokogiri (>= 1.6)
58
+ rails-html-sanitizer (1.6.0)
59
+ loofah (~> 2.21)
60
+ nokogiri (~> 1.14)
61
+ railties (7.0.5)
62
+ actionpack (= 7.0.5)
63
+ activesupport (= 7.0.5)
64
+ method_source
65
+ rake (>= 12.2)
66
+ thor (~> 1.0)
67
+ zeitwerk (~> 2.5)
68
+ rainbow (3.1.1)
69
+ rake (13.0.6)
70
+ regexp_parser (2.8.0)
71
+ rexml (3.2.5)
72
+ rspec-core (3.12.2)
73
+ rspec-support (~> 3.12.0)
74
+ rspec-expectations (3.12.3)
75
+ diff-lcs (>= 1.2.0, < 2.0)
76
+ rspec-support (~> 3.12.0)
77
+ rspec-mocks (3.12.5)
78
+ diff-lcs (>= 1.2.0, < 2.0)
79
+ rspec-support (~> 3.12.0)
80
+ rspec-rails (6.0.3)
81
+ actionpack (>= 6.1)
82
+ activesupport (>= 6.1)
83
+ railties (>= 6.1)
84
+ rspec-core (~> 3.12)
85
+ rspec-expectations (~> 3.12)
86
+ rspec-mocks (~> 3.12)
87
+ rspec-support (~> 3.12)
88
+ rspec-support (3.12.0)
89
+ rubocop (1.42.0)
90
+ json (~> 2.3)
91
+ parallel (~> 1.10)
92
+ parser (>= 3.1.2.1)
93
+ rainbow (>= 2.2.2, < 4.0)
94
+ regexp_parser (>= 1.8, < 3.0)
95
+ rexml (>= 3.2.5, < 4.0)
96
+ rubocop-ast (>= 1.24.1, < 2.0)
97
+ ruby-progressbar (~> 1.7)
98
+ unicode-display_width (>= 1.4.0, < 3.0)
99
+ rubocop-ast (1.24.1)
100
+ parser (>= 3.1.1.0)
101
+ rubocop-performance (1.15.2)
102
+ rubocop (>= 1.7.0, < 2.0)
103
+ rubocop-ast (>= 0.4.0)
104
+ ruby-progressbar (1.11.0)
105
+ standard (1.22.0)
106
+ language_server-protocol (~> 3.17.0.2)
107
+ rubocop (= 1.42.0)
108
+ rubocop-performance (= 1.15.2)
109
+ thor (1.2.2)
110
+ tzinfo (2.0.6)
111
+ concurrent-ruby (~> 1.0)
112
+ unicode-display_width (2.4.2)
113
+ zeitwerk (2.6.8)
114
+
115
+ PLATFORMS
116
+ arm64-darwin-22
117
+ x86_64-linux
118
+
119
+ DEPENDENCIES
120
+ prefab!
121
+ rake (~> 13.0)
122
+ rspec-rails
123
+ standard (~> 1.3)
124
+
125
+ BUNDLED WITH
126
+ 2.4.6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Bradly Feeley
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,42 @@
1
+ # Prefab
2
+
3
+ A lightweight library for managing resources in your Rails application's controllers
4
+
5
+ ## Introduction
6
+
7
+ This gem provides a lightweight library that simplifies the controllers of your resources in a Rails application. It offers a set of intuitive and convenient methods for handling common CRUD operations, including creating, reading, updating, and deleting resources.
8
+
9
+ ## Installation
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add prefab
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install prefab
18
+
19
+ ## Usage
20
+ Include Prefab::Base in your top-level controller.
21
+
22
+ ```ruby
23
+ class ApplicationController < ActionController::Base
24
+ include Prefab::Base
25
+ end
26
+ ```
27
+
28
+ That will give your other controllers access to the prefab class method. Then you can prefab your specific resources:
29
+
30
+ ```ruby
31
+ class PostsController < ApplicationController
32
+ prefab :posts
33
+ end
34
+ ```
35
+
36
+ ## Contributing
37
+
38
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bradly/prefab.
39
+
40
+ ## License
41
+
42
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Prefab
4
+ module Base
5
+ extend ActiveSupport::Concern
6
+
7
+ class_methods do
8
+ def prefab(resource_name, parent: nil, permitted_params: nil)
9
+ include Prefab::Controller
10
+
11
+ @resource_name = resource_name.to_s.singularize
12
+ @parent_name = parent&.to_s&.singularize
13
+ @permitted_params = permitted_params
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,112 @@
1
+ module Prefab::Controller
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ before_action :collection, only: :index
6
+ before_action :resource, only: [:show, :new, :create, :edit, :update, :destroy]
7
+ before_action :parent
8
+ end
9
+
10
+ def index; end
11
+ def show; end
12
+ def new; end
13
+ def edit; end
14
+
15
+ def create
16
+ if resource.save
17
+ redirect_to resource, notice: save_success_message
18
+ else
19
+ render :new, alert: save_failed_message
20
+ end
21
+ end
22
+
23
+ def update
24
+ if resource.update(resource_params)
25
+ redirect_to resource, notice: save_success_message
26
+ else
27
+ render :edit, alert: save_failed_message
28
+ end
29
+ end
30
+
31
+ def destroy
32
+ resource.destroy!
33
+ redirect_back(fallback_location: root_path)
34
+ end
35
+
36
+ private
37
+
38
+ def resource_name
39
+ self.class.instance_variable_get(:"@resource_name")
40
+ end
41
+
42
+ def parent_name
43
+ self.class.instance_variable_get(:"@parent_name")
44
+ end
45
+
46
+ def permitted_params
47
+ self.class.instance_variable_get(:"@permitted_params")
48
+ end
49
+
50
+ def collection_name
51
+ resource_name.pluralize
52
+ end
53
+
54
+ def collection
55
+ @collection ||= instance_variable_set("@#{collection_name}", resource_scope.all)
56
+ end
57
+
58
+ def parent_class
59
+ @parent_class ||= parent_name.to_s.camelize.constantize
60
+ end
61
+
62
+ def parent
63
+ return unless has_parent?
64
+
65
+ @parent ||= begin
66
+ @parent = parent_class.find(params["#{parent_name}_id"])
67
+ instance_variable_set("@#{parent_name}", @parent)
68
+ @parent
69
+ end
70
+ end
71
+
72
+ def has_parent?
73
+ parent_name.present?
74
+ end
75
+
76
+ def resource_scope
77
+ if has_parent?
78
+ parent.public_send(resource_name.pluralize)
79
+ else
80
+ resource_name.to_s.camelize.constantize
81
+ end
82
+ end
83
+
84
+ def resource
85
+ @resource ||= begin
86
+ @resource = case action_name
87
+ when 'new'
88
+ resource_scope.new
89
+ when 'create'
90
+ resource_scope.new(resource_params)
91
+ else
92
+ resource_scope.find(params[:id])
93
+ end
94
+
95
+ instance_variable_set("@#{resource_name}", @resource)
96
+
97
+ @resource
98
+ end
99
+ end
100
+
101
+ def resource_params
102
+ params.require(resource_name).permit(*permitted_params)
103
+ end
104
+
105
+ def save_success_message
106
+ "#{resource_name} saved"
107
+ end
108
+
109
+ def save_failed_message
110
+ "There was a problem saving this #{resource_name}"
111
+ end
112
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Prefab
4
+ VERSION = "0.1.0"
5
+ end
data/lib/prefab.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support'
4
+
5
+ require_relative "prefab/version"
6
+ require_relative "prefab/controller"
7
+ require_relative "prefab/base"
8
+
9
+ module Prefab
10
+ end
data/sig/prefab.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Prefab
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prefab-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bradly Feeley
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-06-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: actionpack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: This gem provides a lightweight library that simplifies the controllers
56
+ of your resources in a Rails application. It offers a set of intuitive and convenient
57
+ methods for handling common CRUD operations, including creating, reading, updating,
58
+ and deleting resources.
59
+ email:
60
+ - 4227+bradly@users.noreply.github.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".rspec"
66
+ - ".standard.yml"
67
+ - Gemfile
68
+ - Gemfile.lock
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - lib/prefab.rb
73
+ - lib/prefab/base.rb
74
+ - lib/prefab/controller.rb
75
+ - lib/prefab/version.rb
76
+ - sig/prefab.rbs
77
+ homepage: https://github.com/bradly/prefab
78
+ licenses:
79
+ - MIT
80
+ metadata:
81
+ homepage_uri: https://github.com/bradly/prefab
82
+ source_code_uri: https://github.com/bradly/prefab
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: 2.6.0
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubygems_version: 3.4.6
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: A lightweight library for managing resources in your Rails application's
102
+ controllers
103
+ test_files: []