view_component_liquid 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 36d1743b812fccdf8ce58e060cf49ca7b90a9077abb1a47a929a6f7efd42abfc
4
+ data.tar.gz: 8ae09e75a0acd4020d93066550c6c7303f1a04dd1fa47c10c2f773febd347a81
5
+ SHA512:
6
+ metadata.gz: 7304d6fe765bf3f3007183bfd1a3c41288c02f60cc449c0e8674b3951f64b95b686ce9837bf129fe815111971de7bb2a8eacd181d932afa72d0d65c853f9be85
7
+ data.tar.gz: c90a0073cd11cf9e8ef48e0164fd7f00e39ce4a7698cd41aef6c493712842e17bd0d5d2e970a6915425c6ad34ea3f7c27ba16362cf7140a90aaf5f057cce51e1
@@ -0,0 +1,9 @@
1
+ .DS_Store
2
+ /.bundle/
3
+ /.yardoc
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,77 @@
1
+ # Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, sex characteristics, gender identity and expression,
9
+ level of experience, education, socio-economic status, nationality, personal
10
+ appearance, race, religion, or sexual identity and orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at
59
+ [jared@whitefusion.io](mailto:jared@whitefusion.io). All complaints will
60
+ be reviewed and investigated and will result in a response that is deemed
61
+ necessary and appropriate to the circumstances. The project team is obligated
62
+ to maintain confidentiality with regard to the reporter of an incident.
63
+ Further details of specific enforcement policies may be posted separately.
64
+
65
+ Project maintainers who do not follow or enforce the Code of Conduct in good
66
+ faith may face temporary or permanent repercussions as determined by other
67
+ members of the project's leadership.
68
+
69
+ ## Attribution
70
+
71
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
72
+ available at [https://www.contributor-covenant.org/version/1/4/code-of-conduct.html](https://www.contributor-covenant.org/version/1/4/code-of-conduct.html)
73
+
74
+ [homepage]: https://www.contributor-covenant.org
75
+
76
+ For answers to common questions about this code of conduct, see
77
+ [https://www.contributor-covenant.org/faq](https://www.contributor-covenant.org/faq)
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in view-component-liquid.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Jared White
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.
@@ -0,0 +1,91 @@
1
+ # ViewComponentLiquid
2
+
3
+ Use Liquid templates and components from within GitHub's [ViewComponent](https://github.com/github/view_component) and your Rails application.
4
+
5
+
6
+ TODO: Update docs below
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'view_component_liquid'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install view_component_liquid
23
+
24
+
25
+ Next, add this line below where you require `view_component` in `config/application.rb`:
26
+
27
+ ```ruby
28
+ require "view_component_liquid/engine"
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ TODO: Write usage instructions here
34
+
35
+ ## Bridgetown Filters
36
+
37
+ $ bundle add bridgetown-core
38
+
39
+ Then create the file `config/initializers/bridgetown.rb`
40
+
41
+ ```ruby
42
+ require "bridgetown-core/filters"
43
+ Liquid::Template.register_filter Bridgetown::Filters
44
+ ```
45
+
46
+ ## Loading shared components from a Bridgetown site
47
+
48
+ Example:
49
+
50
+ ```ruby
51
+ # config/initializers/bridgetown.rb
52
+
53
+ # Set the path to your Bridgetown folder relative to the Rails root
54
+ bridgetown_path = Rails.root.parent.join("bridgetown-site-repo")
55
+
56
+ # Load in Liquid components and any shared Bridgetown builders
57
+ components_path = bridgetown_path.join("src", "_components")
58
+ plugins_path = bridgetown_path.join("plugins", "shared_rails")
59
+ ActiveSupport::Dependencies.autoload_paths << components_path
60
+ ActiveSupport::Dependencies.autoload_paths << plugins_path
61
+
62
+ # Instantiate any shared builders
63
+ ActiveSupport.on_load(:action_controller) do
64
+ class SiteBuilder < Bridgetown::Builder; end
65
+
66
+ # Pass along a "fake" site object. If you'd to include config to pass to
67
+ # your builder, you can do t
68
+ TagsBuilder.new(
69
+ "TagsBuilder",
70
+ OpenStruct.new(config: {
71
+ rails: "included config!"
72
+ }.with_indifferent_access)
73
+ )
74
+ end
75
+ ```
76
+
77
+ ## Development
78
+
79
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
80
+
81
+ ## Contributing
82
+
83
+ Bug reports and pull requests are welcome on GitHub at https://github.com/view_component_liquid/view_component_liquid.
84
+
85
+ ## License
86
+
87
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
88
+
89
+ ## Code of Conduct
90
+
91
+ Everyone interacting in the View::Component::Liquid project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/bridgetownrb/view_component_liquid/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "action_view"
4
+ require "active_support/dependencies/autoload"
5
+ require "active_support/concern"
6
+ require "liquid"
7
+ require "liquid-render-tag"
8
+ require "liquid-component"
9
+
10
+ module ViewComponentLiquid
11
+ extend ActiveSupport::Autoload
12
+
13
+ autoload :TemplateHandler
14
+ autoload :FileSystem
15
+ end
16
+
17
+ # Mixin to let ViewComponent work with Liquid
18
+ module ViewComponent
19
+ module Liquify
20
+ extend ActiveSupport::Concern
21
+
22
+ included do
23
+ class_attribute :liquid_keys
24
+ end
25
+
26
+ class_methods do
27
+ def liquid_accessor(*varnames)
28
+ self.liquid_keys ||= Set.new
29
+ varnames.each do |varname|
30
+ self.liquid_keys << varname
31
+ attr_accessor varname
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ # Look for both compname.liquid and compname_component.liquid
38
+ def matching_views_in_source_location
39
+ location_without_extension = source_location.chomp(File.extname(source_location)).split("/").tap do |pathname|
40
+ pathname.last.delete_suffix! "_component"
41
+ end.join("/")
42
+ Dir["#{location_without_extension}.liquid", "#{location_without_extension}_component.liquid"]
43
+ end
44
+ end
45
+
46
+ def local_assigns
47
+ {}.yield_self do |assigns|
48
+ self.class.liquid_keys.each do |key|
49
+ assigns[key] = send(key)
50
+ end
51
+ assigns
52
+ end
53
+ end
54
+ end
55
+ end
56
+
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails"
4
+ require "view_component_liquid"
5
+
6
+ module ViewComponentLiquid
7
+ class Engine < Rails::Engine
8
+ initializer "view_component_liquid.add_liquid_template_handler" do
9
+ ActiveSupport.on_load(:action_view) do
10
+ ActionView::Template.register_template_handler(:liquid, ViewComponentLiquid::TemplateHandler)
11
+ end
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Provide Liquid views/partials/components to the template handler and render tag
4
+ module ViewComponentLiquid
5
+ class FileSystem
6
+ def initialize(view)
7
+ @view = view
8
+ end
9
+
10
+ def read_template_file(template_path)
11
+ controller_path = view.controller_path
12
+
13
+ template_name = File.basename(template_path)
14
+ template_folders = unless template_path.include?("/")
15
+ [
16
+ "",
17
+ controller_path,
18
+ "application",
19
+ "shared"
20
+ ]
21
+ else
22
+ [File.dirname(template_path)]
23
+ end
24
+
25
+ # Search Rails' configured view paths first
26
+ result = view.view_paths.find_all(
27
+ template_name,
28
+ template_folders,
29
+ true,
30
+ lookup_details
31
+ )
32
+
33
+ if result.present?
34
+ LiquidComponent.parse(result.first.source).content
35
+ else
36
+ # Time to look through autoload paths for component folders
37
+ components_folders = Zeitwerk::Loader.all_dirs.select {|item| item.ends_with?("_components")}
38
+
39
+ template = nil
40
+
41
+ components_folders.each do |components_folder|
42
+ tmpl = components_folder + "/#{template_path}.liquid"
43
+ if File.exist?(tmpl)
44
+ template = LiquidComponent.parse(File.read(tmpl)).content
45
+ break
46
+ end
47
+ end
48
+
49
+ template.presence || raise(ActionView::MissingTemplate.new(template_folders + components_folders, template_name, template_folders, false, "liquid views"))
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ attr_reader :view
56
+
57
+ def lookup_details
58
+ {
59
+ locale: [view.locale, :en],
60
+ formats: view.formats,
61
+ variants: [],
62
+ handlers: [:liquid],
63
+ versions: []
64
+ }
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Hook into ActiveView's template handling system
4
+ module ViewComponentLiquid
5
+ class TemplateHandler
6
+ class << self
7
+ def call(template, source)
8
+ "ViewComponentLiquid::TemplateHandler.new(self).render(#{source.inspect}, local_assigns)"
9
+ end
10
+ end
11
+
12
+ def initialize(view)
13
+ @view = view
14
+ @controller = @view.controller
15
+ end
16
+
17
+ def render(template, local_assigns={})
18
+ component = LiquidComponent.parse(template)
19
+ template = component.content
20
+
21
+ assigns = local_assigns.stringify_keys
22
+ assigns["component"] = component.to_h.deep_stringify_keys
23
+ assigns["controller"] = {
24
+ "controller_name" => @controller.controller_name,
25
+ "action_name" => @controller.action_name
26
+ }
27
+ if @view.respond_to?(:assigns)
28
+ assigns["controller"].merge! @view.assigns.to_h
29
+ @view.instance_variable_set(:@liquid_page, component)
30
+ end
31
+
32
+ p "ASSIGNS", assigns
33
+
34
+ liquid = Liquid::Template.parse(template)
35
+ liquid.send(render_method, assigns, filters: filters, registers: registers).html_safe
36
+ end
37
+
38
+ def filters
39
+ if @view.respond_to?(:liquid_filters, true)
40
+ @view.send(:liquid_filters)
41
+ end
42
+ end
43
+
44
+ def registers
45
+ {
46
+ view: @view,
47
+ file_system: ViewComponentLiquid::FileSystem.new(@view)
48
+ }
49
+ end
50
+
51
+ def compilable?
52
+ false
53
+ end
54
+
55
+ def render_method
56
+ (Rails.env.development? || Rails.env.test?) ? :render! : :render
57
+ end
58
+
59
+ ActiveSupport.run_load_hooks(:view_component_liquid, self)
60
+ end
61
+ end
62
+
@@ -0,0 +1,4 @@
1
+ module ViewComponentLiquid
2
+ VERSION = "1.0.0"
3
+ end
4
+
@@ -0,0 +1,28 @@
1
+ require_relative "lib/view_component_liquid/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "view_component_liquid"
5
+ spec.version = ViewComponentLiquid::VERSION
6
+ spec.authors = ["Jared White"]
7
+ spec.email = ["jared@whitefusion.io"]
8
+
9
+ spec.summary = %q{Use Liquid templates and components from within ViewComponent}
10
+ spec.description = spec.summary
11
+ spec.homepage = "https://github.com/bridgetownrb/view_component_liquid"
12
+ spec.license = "MIT"
13
+
14
+ # Specify which files should be added to the gem when it is released.
15
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
16
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
17
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ end
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "liquid", "~> 4.0"
22
+ spec.add_runtime_dependency "liquid-render-tag", "~> 0.2"
23
+ spec.add_runtime_dependency "liquid-component", ">= 0.1"
24
+ spec.add_runtime_dependency "activesupport", [">= 5.0.0", "< 7.0"]
25
+
26
+ spec.add_development_dependency "bundler"
27
+ spec.add_development_dependency "rake", "~> 12.0"
28
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: view_component_liquid
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jared White
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-05-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: liquid
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: liquid-render-tag
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: liquid-component
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 5.0.0
62
+ - - "<"
63
+ - !ruby/object:Gem::Version
64
+ version: '7.0'
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 5.0.0
72
+ - - "<"
73
+ - !ruby/object:Gem::Version
74
+ version: '7.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: bundler
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rake
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '12.0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '12.0'
103
+ description: Use Liquid templates and components from within ViewComponent
104
+ email:
105
+ - jared@whitefusion.io
106
+ executables: []
107
+ extensions: []
108
+ extra_rdoc_files: []
109
+ files:
110
+ - ".gitignore"
111
+ - CODE_OF_CONDUCT.md
112
+ - Gemfile
113
+ - LICENSE.txt
114
+ - README.md
115
+ - Rakefile
116
+ - lib/view_component_liquid.rb
117
+ - lib/view_component_liquid/engine.rb
118
+ - lib/view_component_liquid/file_system.rb
119
+ - lib/view_component_liquid/template_handler.rb
120
+ - lib/view_component_liquid/version.rb
121
+ - view_component_liquid.gemspec
122
+ homepage: https://github.com/bridgetownrb/view_component_liquid
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubygems_version: 3.0.6
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: Use Liquid templates and components from within ViewComponent
145
+ test_files: []