active_model-inherited_partials 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7f2ce52b8c1400c27c4710709dd7d9fff4ea752f272442150c4cf5b8e10117a8
4
+ data.tar.gz: 432e20371935301896f1df236e1f2107efce162fe628965dd621c8e796519aa4
5
+ SHA512:
6
+ metadata.gz: e38ad659b6607b7cb8b87185d014b11d58f27997648361a026a75ba3e6972ac811f8cc119506abb9a3e55b11c570d4e15249ad00db12a458e1bbfdf343a3ef5a
7
+ data.tar.gz: dfd3148f9f372b8c6577516b9731687e2a73a10b226f807771d7027ef46003c218dfc354da479733e93caa92b58f0a4f9aa0a67e38f2e8d3ab16af1a7f4b004c
data/CHANGELOG.md ADDED
File without changes
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright Alexander Senko
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ ## Rails partials
2
+
3
+ Rails partials are great when it comes to rendering a model. But they still seem to have inconsistencies and a room for further improvements.
4
+
5
+ Consider the following example: we have a model inheritance chain like `Message` → `Message::Text` → `Message::HTML`.
6
+
7
+ ### Issue #1: your code gets WET
8
+ To render both text and HTML messages we need to define both `message/texts/text` & `message/htmls/html` partials — even when they are identical and just rendering `message.title` and `message.body`.
9
+
10
+ ### Issue #2: inconsistent variable naming
11
+ In the above case we get `text` & `html` variables inside the partials, while it's just `message` for the basic `messages/message` one. You can't even copy the identical code from one partial into another!
12
+
13
+ ### Issue #3: inconsistent paths
14
+ Both texts & HTML end up in `views/message`, while the basic message goes to `views/messages`. Thus we get two directories instead of one.
15
+
16
+ So, meet
17
+ # Inherited Partials
18
+
19
+ This Rails Engine brings you:
20
+
21
+ 1. **Nesting** directories to respect model namespaces.
22
+ 2. Consistent variable **naming** in namespaced partials.
23
+ 3. **Inheritance** when a partial is missing.
24
+
25
+ It provides the above example case with the following partial paths:
26
+
27
+ ```
28
+ messages/message
29
+ messages/text/message
30
+ messages/html/message
31
+ ```
32
+
33
+ When a model's partial is missing, the parent's one is looked up, upto the basic one.
34
+
35
+ ## Usage
36
+
37
+ No explicit code nor configuration needed, just install it!
38
+
39
+ ## Installation
40
+ Add this line to your application's Gemfile:
41
+
42
+ ```ruby
43
+ gem "active_model-inherited_partials"
44
+ ```
45
+
46
+ And then execute:
47
+ ```bash
48
+ $ bundle
49
+ ```
50
+
51
+ Or install it yourself as:
52
+ ```bash
53
+ $ gem install active_model-inherited_partials
54
+ ```
55
+
56
+ ## Contributing
57
+
58
+ 1. Fork it
59
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
60
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
61
+ 4. Push to the branch (`git push origin my-new-feature`)
62
+ 5. Create new Pull Request
63
+
64
+ ## License
65
+ 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,8 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ load "rails/tasks/statistics.rake"
7
+
8
+ require "bundler/gem_tasks"
@@ -0,0 +1 @@
1
+ //= link_directory ../stylesheets/active_model/inherited_partials .css
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,6 @@
1
+ module ActiveModel
2
+ module InheritedPartials
3
+ class ApplicationController < ActionController::Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module ActiveModel
2
+ module InheritedPartials
3
+ module ApplicationHelper
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module ActiveModel
2
+ module InheritedPartials
3
+ class ApplicationJob < ActiveJob::Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,8 @@
1
+ module ActiveModel
2
+ module InheritedPartials
3
+ class ApplicationMailer < ActionMailer::Base
4
+ default from: "from@example.com"
5
+ layout "mailer"
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ module ActiveModel
2
+ module InheritedPartials
3
+ class ApplicationRecord < ActiveRecord::Base
4
+ self.abstract_class = true
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Active model inherited partials</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= stylesheet_link_tag "active_model/inherited_partials/application", media: "all" %>
9
+ </head>
10
+ <body>
11
+
12
+ <%= yield %>
13
+
14
+ </body>
15
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ ActiveModel::InheritedPartials::Engine.routes.draw do
2
+ end
@@ -0,0 +1,15 @@
1
+ Gem::Author ||= Struct.new(
2
+ :name,
3
+ :email,
4
+ :github_url,
5
+ )
6
+
7
+ module ActiveModel::InheritedPartials
8
+ AUTHORS = [
9
+ Gem::Author.new(
10
+ name: 'Alexander Senko',
11
+ email: 'Alexander.Senko@gmail.com',
12
+ github_url: 'https://github.com/Alexander-Senko',
13
+ ),
14
+ ]
15
+ end
@@ -0,0 +1,7 @@
1
+ module ActiveModel
2
+ module InheritedPartials
3
+ class Engine < ::Rails::Engine
4
+ isolate_namespace ActiveModel::InheritedPartials
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveModel
2
+ module InheritedPartials
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,55 @@
1
+ require "active_model/inherited_partials/version"
2
+ require "active_model/inherited_partials/engine"
3
+
4
+ module ActiveModel
5
+ module InheritedPartials
6
+ extend ActiveSupport::Concern
7
+
8
+ class_methods do
9
+ def _to_partial_path
10
+ @_to_partial_path ||=
11
+ find_or_inherit namespaced_partial_path || super
12
+ end
13
+
14
+ protected
15
+
16
+ def partial_dir = Pathname(_to_partial_path).dirname
17
+ def partial_name = Pathname(_to_partial_path).basename.to_s
18
+
19
+ private
20
+
21
+ def namespaced_partial_path
22
+ namespace_model
23
+ &.then { _1.partial_dir / model_name.element / _1.partial_name }
24
+ &.to_s
25
+ end
26
+
27
+ def namespace_model
28
+ namespace_classes
29
+ .reverse.find { _1.respond_to? :model_name }
30
+ end
31
+
32
+ def namespace_classes
33
+ name
34
+ .deconstantize
35
+ .split('::')
36
+ .map(&:constantize)
37
+ .select { _1.in? ancestors }
38
+ end
39
+
40
+ def find_or_inherit path
41
+ return path if partial_exists? path
42
+ return path if base_class?
43
+
44
+ superclass._to_partial_path
45
+ end
46
+
47
+ def partial_exists? path
48
+ ApplicationController.new.lookup_context
49
+ .exists? path, [], true
50
+ end
51
+ end
52
+ end
53
+
54
+ Conversion::ClassMethods.prepend InheritedPartials::ClassMethods
55
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :active_model_inherited_partials do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_model-inherited_partials
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Senko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-01-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '7.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '7.1'
27
+ description: A Rails Engine to let models inherit partials until overridden.
28
+ email:
29
+ - Alexander.Senko@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - CHANGELOG.md
35
+ - MIT-LICENSE
36
+ - README.md
37
+ - Rakefile
38
+ - app/assets/config/active_model_inherited_partials_manifest.js
39
+ - app/assets/stylesheets/active_model/inherited_partials/application.css
40
+ - app/controllers/active_model/inherited_partials/application_controller.rb
41
+ - app/helpers/active_model/inherited_partials/application_helper.rb
42
+ - app/jobs/active_model/inherited_partials/application_job.rb
43
+ - app/mailers/active_model/inherited_partials/application_mailer.rb
44
+ - app/models/active_model/inherited_partials/application_record.rb
45
+ - app/views/layouts/active_model/inherited_partials/application.html.erb
46
+ - config/routes.rb
47
+ - lib/active_model/inherited_partials.rb
48
+ - lib/active_model/inherited_partials/authors.rb
49
+ - lib/active_model/inherited_partials/engine.rb
50
+ - lib/active_model/inherited_partials/version.rb
51
+ - lib/tasks/active_model/inherited_partials_tasks.rake
52
+ homepage: https://github.com/Alexander-Senko/active_model-inherited_partials
53
+ licenses:
54
+ - MIT
55
+ metadata:
56
+ homepage_uri: https://github.com/Alexander-Senko/active_model-inherited_partials
57
+ source_code_uri: https://github.com/Alexander-Senko/active_model-inherited_partials
58
+ changelog_uri: https://github.com/Alexander-Senko/active_model-inherited_partials/CHANGELOG.md
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '3.2'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubygems_version: 3.5.3
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Inheritable partials for Rails
78
+ test_files: []