auto_view_model 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: 6dda316a58b8a4adbd1789c772fd3a07d86b2df2b3f71c9d6b818a22c188bca9
4
+ data.tar.gz: 22eaa4fc73258bf12eb7b33c8648d06d1c059366849a731b637c96f9767c99b1
5
+ SHA512:
6
+ metadata.gz: 261135d6e20c26a352c6bb41088d0dc3c3f961265758b53e1e1ad4de542d1df38af17a7f7e17148d039c38595e9ddeaa9f36d71cbaefef0e9b74ff3ffff5e108
7
+ data.tar.gz: fe8a175d41452325cf7dab7a946f01acaa87ff5b21b34345f6e2f3a226546914ea3b1adec8f517eaa06e1f9301f8419c885e2014911e6fc6d89c900461e40824
@@ -0,0 +1,18 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 2.7.1
14
+ - name: Run the default task
15
+ run: |
16
+ gem install bundler -v 2.2.14
17
+ bundle install
18
+ bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-04-26
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in auto_view_model.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "rails", "~> 6.1"
10
+
11
+ gem "minitest", "~> 5.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,151 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ auto_view_model (0.1.0)
5
+ actionview (~> 6.1)
6
+ activesupport (~> 6.1)
7
+ pry (~> 0.14)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ actioncable (6.1.3.1)
13
+ actionpack (= 6.1.3.1)
14
+ activesupport (= 6.1.3.1)
15
+ nio4r (~> 2.0)
16
+ websocket-driver (>= 0.6.1)
17
+ actionmailbox (6.1.3.1)
18
+ actionpack (= 6.1.3.1)
19
+ activejob (= 6.1.3.1)
20
+ activerecord (= 6.1.3.1)
21
+ activestorage (= 6.1.3.1)
22
+ activesupport (= 6.1.3.1)
23
+ mail (>= 2.7.1)
24
+ actionmailer (6.1.3.1)
25
+ actionpack (= 6.1.3.1)
26
+ actionview (= 6.1.3.1)
27
+ activejob (= 6.1.3.1)
28
+ activesupport (= 6.1.3.1)
29
+ mail (~> 2.5, >= 2.5.4)
30
+ rails-dom-testing (~> 2.0)
31
+ actionpack (6.1.3.1)
32
+ actionview (= 6.1.3.1)
33
+ activesupport (= 6.1.3.1)
34
+ rack (~> 2.0, >= 2.0.9)
35
+ rack-test (>= 0.6.3)
36
+ rails-dom-testing (~> 2.0)
37
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
38
+ actiontext (6.1.3.1)
39
+ actionpack (= 6.1.3.1)
40
+ activerecord (= 6.1.3.1)
41
+ activestorage (= 6.1.3.1)
42
+ activesupport (= 6.1.3.1)
43
+ nokogiri (>= 1.8.5)
44
+ actionview (6.1.3.1)
45
+ activesupport (= 6.1.3.1)
46
+ builder (~> 3.1)
47
+ erubi (~> 1.4)
48
+ rails-dom-testing (~> 2.0)
49
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
50
+ activejob (6.1.3.1)
51
+ activesupport (= 6.1.3.1)
52
+ globalid (>= 0.3.6)
53
+ activemodel (6.1.3.1)
54
+ activesupport (= 6.1.3.1)
55
+ activerecord (6.1.3.1)
56
+ activemodel (= 6.1.3.1)
57
+ activesupport (= 6.1.3.1)
58
+ activestorage (6.1.3.1)
59
+ actionpack (= 6.1.3.1)
60
+ activejob (= 6.1.3.1)
61
+ activerecord (= 6.1.3.1)
62
+ activesupport (= 6.1.3.1)
63
+ marcel (~> 1.0.0)
64
+ mini_mime (~> 1.0.2)
65
+ activesupport (6.1.3.1)
66
+ concurrent-ruby (~> 1.0, >= 1.0.2)
67
+ i18n (>= 1.6, < 2)
68
+ minitest (>= 5.1)
69
+ tzinfo (~> 2.0)
70
+ zeitwerk (~> 2.3)
71
+ builder (3.2.4)
72
+ coderay (1.1.3)
73
+ concurrent-ruby (1.1.8)
74
+ crass (1.0.6)
75
+ erubi (1.10.0)
76
+ globalid (0.4.2)
77
+ activesupport (>= 4.2.0)
78
+ i18n (1.8.10)
79
+ concurrent-ruby (~> 1.0)
80
+ loofah (2.9.1)
81
+ crass (~> 1.0.2)
82
+ nokogiri (>= 1.5.9)
83
+ mail (2.7.1)
84
+ mini_mime (>= 0.1.1)
85
+ marcel (1.0.1)
86
+ method_source (1.0.0)
87
+ mini_mime (1.0.3)
88
+ minitest (5.14.4)
89
+ nio4r (2.5.7)
90
+ nokogiri (1.11.3-x86_64-darwin)
91
+ racc (~> 1.4)
92
+ pry (0.14.1)
93
+ coderay (~> 1.1)
94
+ method_source (~> 1.0)
95
+ racc (1.5.2)
96
+ rack (2.2.3)
97
+ rack-test (1.1.0)
98
+ rack (>= 1.0, < 3)
99
+ rails (6.1.3.1)
100
+ actioncable (= 6.1.3.1)
101
+ actionmailbox (= 6.1.3.1)
102
+ actionmailer (= 6.1.3.1)
103
+ actionpack (= 6.1.3.1)
104
+ actiontext (= 6.1.3.1)
105
+ actionview (= 6.1.3.1)
106
+ activejob (= 6.1.3.1)
107
+ activemodel (= 6.1.3.1)
108
+ activerecord (= 6.1.3.1)
109
+ activestorage (= 6.1.3.1)
110
+ activesupport (= 6.1.3.1)
111
+ bundler (>= 1.15.0)
112
+ railties (= 6.1.3.1)
113
+ sprockets-rails (>= 2.0.0)
114
+ rails-dom-testing (2.0.3)
115
+ activesupport (>= 4.2.0)
116
+ nokogiri (>= 1.6)
117
+ rails-html-sanitizer (1.3.0)
118
+ loofah (~> 2.3)
119
+ railties (6.1.3.1)
120
+ actionpack (= 6.1.3.1)
121
+ activesupport (= 6.1.3.1)
122
+ method_source
123
+ rake (>= 0.8.7)
124
+ thor (~> 1.0)
125
+ rake (13.0.3)
126
+ sprockets (4.0.2)
127
+ concurrent-ruby (~> 1.0)
128
+ rack (> 1, < 3)
129
+ sprockets-rails (3.2.2)
130
+ actionpack (>= 4.0)
131
+ activesupport (>= 4.0)
132
+ sprockets (>= 3.0.0)
133
+ thor (1.1.0)
134
+ tzinfo (2.0.4)
135
+ concurrent-ruby (~> 1.0)
136
+ websocket-driver (0.7.3)
137
+ websocket-extensions (>= 0.1.0)
138
+ websocket-extensions (0.1.5)
139
+ zeitwerk (2.4.2)
140
+
141
+ PLATFORMS
142
+ x86_64-darwin-20
143
+
144
+ DEPENDENCIES
145
+ auto_view_model!
146
+ minitest (~> 5.0)
147
+ rails (~> 6.1)
148
+ rake (~> 13.0)
149
+
150
+ BUNDLED WITH
151
+ 2.2.14
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Blake Williams
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,81 @@
1
+ # AutoViewModel
2
+
3
+ Experimental gem to encapsulate existing partials via sidecar Ruby files.
4
+
5
+ This gem doesn't replace partials, but instead builds on top of them:
6
+
7
+ * Logic is encapsulated in a co-located Ruby file, not spread across helpers and external view model objects.
8
+ * Partial arguments can be required.
9
+ * Partial arguments can have default values, so no more `<%= my_value = "default" if !local_assigns.has_key?(:my_value) %>`
10
+
11
+ ## Installation
12
+
13
+ 1. Add `auto_view_model` to your Rails application's `Gemfile`.
14
+ 1. Run `bundle install`.
15
+ 1. In `config/application.rb`, add `require "auto_view_model/engine"`.
16
+ 1. Start adding Ruby files to your `views` directory.
17
+
18
+ ## Usage
19
+
20
+ Using AutoViewModel is simple, create a Ruby file in the same directory as any
21
+ partial with a matching name (sans `_` prefix).
22
+
23
+ e.g.
24
+
25
+ Given the template `app/views/posts/_post.html.erb`:
26
+
27
+ ```erb
28
+ <h1><%= title.upcase %></h1>
29
+
30
+ <p>
31
+ <%= markdown(body) %>
32
+ </p>
33
+ ```
34
+
35
+ You would create a sidecar Ruby file: `app/views/posts/post.rb`
36
+
37
+ ```ruby
38
+ class Posts::Post < AutoViewModel::Base
39
+ requires :title # Raises if partial is not passed a title
40
+ accepts :body, default: "" # Allows default parameters for non-required attributes
41
+
42
+ def title
43
+ @title.upcase
44
+ end
45
+
46
+ def rendered_body
47
+ # Application and Rails provided helpers are usable in the view models
48
+ markdown(body)
49
+ end
50
+ end
51
+ ```
52
+
53
+ And update the template to use the `view` object:
54
+
55
+ ```erb
56
+ <h1><%= view.title %></h1>
57
+
58
+ <p>
59
+ <%= view.rendered_body %>
60
+ </p>
61
+ ```
62
+
63
+ To render the partial, use `render` like you would any other partial.
64
+
65
+ ```erb
66
+ <%= render "posts/post", title: "Hello world", body: "Welcome!" %>
67
+ ```
68
+
69
+ ## Development
70
+
71
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
72
+
73
+ 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).
74
+
75
+ ## Contributing
76
+
77
+ Bug reports and pull requests are welcome on GitHub at https://github.com/BlakeWilliams/auto_view_model.
78
+
79
+ ## License
80
+
81
+ 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,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/auto_view_model/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "auto_view_model"
7
+ spec.version = AutoViewModel::VERSION
8
+ spec.authors = ["Blake Williams"]
9
+ spec.email = ["blake@blakewilliams.me"]
10
+
11
+ spec.summary = "Automatic view models"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
14
+
15
+ # Specify which files should be added to the gem when it is released.
16
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
17
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ # Uncomment to register a new dependency of your gem
25
+ # spec.add_dependency "example-gem", "~> 1.0"
26
+
27
+ # For more information and examples about making a new gem, checkout our
28
+ # guide at: https://bundler.io/guides/creating_gem.html
29
+ spec.add_dependency "activesupport", "~> 6.1"
30
+ spec.add_dependency "actionview", "~> 6.1"
31
+ spec.add_dependency "pry", "~> 0.14"
32
+ end
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "auto_view_model"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "action_view"
4
+ require "active_support"
5
+
6
+ require_relative "auto_view_model/version"
7
+ require_relative "auto_view_model/base"
8
+
9
+ module AutoViewModel
10
+ class Error < StandardError; end
11
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AutoViewModel
4
+ module Attributes
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ class_attribute :_optional_attributes, default: {}
9
+ class_attribute :_required_attributes, default: Set.new
10
+ end
11
+
12
+ class_methods do
13
+ def requires(parameter)
14
+ _required_attributes << parameter
15
+
16
+ attr_reader parameter
17
+ end
18
+
19
+ def accepts(parameter, default: nil)
20
+ _optional_attributes[parameter] = default
21
+
22
+ attr_reader parameter
23
+ end
24
+
25
+ def inherited(subclass)
26
+ subclass._optional_attributes = _optional_attributes.dup
27
+ subclass._required_attributes = _required_attributes.dup
28
+ super
29
+ end
30
+ end
31
+
32
+ def initialize(locals)
33
+ _construct_attributes(locals)
34
+ end
35
+
36
+ private
37
+
38
+ def _construct_attributes(args)
39
+ _required_attributes.each do |attr|
40
+ if !args.has_key?(attr)
41
+ raise ArgumentError.new("Missing view keyword argument :#{attr}") # Simulate required kwargs
42
+ end
43
+
44
+ instance_variable_set("@#{attr}", args[attr])
45
+ end
46
+
47
+ _optional_attributes.each do |attr, default|
48
+ if args.has_key?(attr)
49
+ value = args[attr]
50
+ instance_variable_set("@#{attr}", value)
51
+ else
52
+ value = args[attr]
53
+ instance_variable_set("@#{attr}", default)
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "attributes"
4
+
5
+ module AutoViewModel
6
+ class Error < StandardError; end
7
+
8
+ class Base
9
+ include AutoViewModel::Attributes
10
+
11
+ def initialize(local_assigns, view_context:)
12
+ super(local_assigns)
13
+ @view_context = view_context
14
+ end
15
+
16
+ # View models should support helper methods available in
17
+ # the template. By delegating missing methods to view
18
+ # context, we can support all existing Rails and
19
+ # application defined helpers.
20
+ def method_missing(method, *args, &block)
21
+ if @view_context.respond_to?(method)
22
+ @view_context.public_send(method, *args, &block)
23
+ else
24
+ super
25
+ end
26
+ end
27
+ ruby2_keywords :method_missing if respond_to?(:ruby2_keywords, true)
28
+
29
+ def respond_to_missing?(method, include_all)
30
+ @view_context.respond_to?(method, include_all)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails"
4
+
5
+ require_relative "template_patch"
6
+
7
+ module AutoViewModel
8
+ class Engine < Rails::Engine
9
+ initializer :set_view_autoload_path, before: :set_load_path do |app|
10
+ app.config.autoload_paths += [app.root.join("app/views").to_s]
11
+ end
12
+
13
+ initializer "auto_view_model.monkey_patch" do |app|
14
+ ActiveSupport.on_load(:action_view) do
15
+
16
+ ActionView::Template.prepend(AutoViewModel::TemplatePatch)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,13 @@
1
+
2
+ module AutoViewModel
3
+ module TemplatePatch
4
+ def locals_code
5
+ class_name = virtual_path.classify
6
+ Object.const_get(class_name)
7
+
8
+ "view=#{class_name}.new(local_assigns, view_context: self);"
9
+ rescue NameError
10
+ super
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AutoViewModel
4
+ VERSION = "0.1.0"
5
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: auto_view_model
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Blake Williams
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-05-01 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: '6.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '6.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: actionview
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '6.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '6.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.14'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.14'
55
+ description:
56
+ email:
57
+ - blake@blakewilliams.me
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".github/workflows/main.yml"
63
+ - ".gitignore"
64
+ - CHANGELOG.md
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - auto_view_model.gemspec
71
+ - bin/console
72
+ - bin/setup
73
+ - lib/auto_view_model.rb
74
+ - lib/auto_view_model/attributes.rb
75
+ - lib/auto_view_model/base.rb
76
+ - lib/auto_view_model/engine.rb
77
+ - lib/auto_view_model/template_patch.rb
78
+ - lib/auto_view_model/version.rb
79
+ homepage:
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
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.4.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.1.2
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Automatic view models
102
+ test_files: []