phlex-rails-template 0.1.1

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: 7e022001e3e5566f7db0993ffdecfb1882e9f4be95261e3de53f9069b30be35d
4
+ data.tar.gz: 76e91257fcea31409418c57a18009113828abf25a15baf8eab60f71d7a9bb57e
5
+ SHA512:
6
+ metadata.gz: b28168c3e642d9e3d69ab2b3344e5f2a7496294ccc74efc712f392e594b279e3e9ee5f416c3ab87fddb69517fa0f5f449b89f63614d7a73892fa8d8f27502dd8
7
+ data.tar.gz: 98ef56aed1738586da1915935a2a3f1bd6056853084a329495f7bce32a29899744dac141b95eb695cf89edfd1c13ecee7127d91c53233a483bcdcb5b420693fe
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # Phlex::Rails::Template
2
+
3
+ A Rails template handler that lets you write Phlex components directly in `.html.rb` view files.
4
+
5
+ ## Installation
6
+
7
+ Run the following command from the root of your Rails project:
8
+
9
+ ```ruby
10
+ # Make sure you've installed phlex-rails
11
+ bundle add 'phlex-rails-template'
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ Create view files with the `.html.rb` extension:
17
+
18
+ ```ruby
19
+ # app/views/posts/show.html.rb
20
+ h1 { @post.title }
21
+
22
+ div(class: "content") do
23
+ p { @post.body }
24
+ end
25
+ ```
26
+
27
+ In your controller:
28
+
29
+ ```ruby
30
+ class PostsController < ApplicationController
31
+ def show
32
+ @post = Post.find(params[:id])
33
+ # Renders app/views/posts/show.html.rb automatically
34
+ end
35
+ end
36
+ ```
37
+
38
+ Controller instance variables are automatically available in your templates.
39
+
40
+ ## Configuration
41
+
42
+ You can customize how components are instantiated and how variables are assigned by passing a block to `register`:
43
+
44
+ ```ruby
45
+ # config/initializers/phlex_rails_template.rb
46
+ Phlex::Rails::Template.register :rb do
47
+ # Override the base component class
48
+ def component_class
49
+ ApplicationComponent
50
+ end
51
+
52
+ # Override to instantiate the component with custom arguments
53
+ def create_component(component_class)
54
+ component_class.new(view_context.session, request: view_context.request)
55
+ end
56
+
57
+ # Override to customize how controller variables are assigned
58
+ def assign_variables
59
+ view_context.assigns.each do |key, value|
60
+ component.instance_variable_set(:"@#{key}", "PREFIX: #{value}")
61
+ end
62
+ end
63
+ end
64
+ ```
65
+
66
+ Or you can pass a configurator class directly:
67
+
68
+ ```ruby
69
+ class CustomConfigurator < Phlex::Rails::Template::Configurator
70
+ def component_class
71
+ ApplicationComponent
72
+ end
73
+ end
74
+
75
+ Phlex::Rails::Template.register :rb, CustomConfigurator
76
+ ```
77
+
78
+ You can also register additional template handlers with different configurators:
79
+
80
+ ```ruby
81
+ Phlex::Rails::Template.register :customrb, MyCustomConfigurator
82
+ ```
83
+
84
+ ## License
85
+
86
+ 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
+ # 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
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phlex
4
+ module Rails
5
+ module Template
6
+ class Engine < ::Rails::Engine
7
+ initializer "phlex.rails.template.register_handler" do
8
+ ActiveSupport.on_load(:action_view) do
9
+ ActionView::Template.register_template_handler :rb, Phlex::Rails::Template::Handler
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phlex
4
+ module Rails
5
+ module Template
6
+ class Handler
7
+ def self.call(template, source = nil)
8
+ src = source || template.source
9
+
10
+ <<~RUBY
11
+ __component__ = Phlex::Rails::Template.build(self, :rb) do
12
+ #{src}
13
+ end
14
+ __component__.render_in(self).to_s
15
+ RUBY
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phlex
4
+ module Rails
5
+ module Template
6
+ class Registry
7
+ def initialize
8
+ @handlers = {}
9
+ end
10
+
11
+ def register(handler_name, configurator_class = nil, &block)
12
+ if block_given?
13
+ @handlers[handler_name] = Class.new(Configurator, &block)
14
+ else
15
+ @handlers[handler_name] = configurator_class
16
+ end
17
+ end
18
+
19
+ def configurator_class_for(handler_name)
20
+ @handlers[handler_name] || Configurator
21
+ end
22
+
23
+ def build(view_context, handler_name, &template_block)
24
+ configurator_class = configurator_class_for(handler_name)
25
+ configurator = configurator_class.new(view_context)
26
+
27
+ component_class = Class.new(configurator.component_class) do
28
+ define_method(:view_template, &template_block)
29
+ end
30
+
31
+ configurator.build(component_class)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phlex
4
+ module Rails
5
+ module Template
6
+ VERSION = "0.1.1"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "forwardable"
4
+
5
+ require_relative "template/version"
6
+ require_relative "template/registry"
7
+ require_relative "template/handler"
8
+
9
+ module Phlex
10
+ module Rails
11
+ module Template
12
+ class Error < StandardError; end
13
+
14
+ class Configurator
15
+ attr_reader :view_context, :component
16
+
17
+ def initialize(view_context)
18
+ @view_context = view_context
19
+ end
20
+
21
+ def component_class
22
+ ::Views::Base
23
+ end
24
+
25
+ def create_component(component_class)
26
+ component_class.new
27
+ end
28
+
29
+ def assign_variables
30
+ assigns = view_context.respond_to?(:view_assigns) ? view_context.view_assigns : view_context.assigns
31
+ assigns.each do |key, value|
32
+ ivar = :"@#{key}"
33
+ if component.instance_variable_defined?(ivar)
34
+ raise ArgumentError,
35
+ "Refusing to overwrite #{ivar} on #{component.class}. " \
36
+ "It was already set by the component before assigns were applied."
37
+ end
38
+ component.instance_variable_set(ivar, value)
39
+ end
40
+ end
41
+
42
+ def build(component_class)
43
+ @component = component_class.new
44
+ assign_variables
45
+ component
46
+ end
47
+ end
48
+
49
+ @registry = Registry.new
50
+
51
+ class << self
52
+ extend Forwardable
53
+
54
+ attr_reader :registry
55
+
56
+ def_delegators :registry, :register, :build
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ require_relative "template/engine" if defined?(Rails)
@@ -0,0 +1,8 @@
1
+ module Phlex
2
+ module Rails
3
+ module Template
4
+ VERSION: String
5
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
6
+ end
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: phlex-rails-template
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Brad Gessler
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 2025-11-14 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: phlex
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '1.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '1.0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: actionview
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '6.0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '6.0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: railties
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '6.0'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '6.0'
54
+ description: Write Phlex components directly in .html.rb view files
55
+ email:
56
+ - bradgessler@gmail.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - ".rspec"
62
+ - README.md
63
+ - Rakefile
64
+ - lib/phlex/rails/template.rb
65
+ - lib/phlex/rails/template/engine.rb
66
+ - lib/phlex/rails/template/handler.rb
67
+ - lib/phlex/rails/template/registry.rb
68
+ - lib/phlex/rails/template/version.rb
69
+ - sig/phlex/rails/template.rbs
70
+ homepage: https://github.com/beautifulruby/phlex-rails-template
71
+ licenses: []
72
+ metadata:
73
+ homepage_uri: https://github.com/beautifulruby/phlex-rails-template
74
+ source_code_uri: https://github.com/beautifulruby/phlex-rails-template
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 3.1.0
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubygems_version: 3.6.2
90
+ specification_version: 4
91
+ summary: Rails template handler for Phlex components
92
+ test_files: []