rbexy 0.2.3 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 65318a8a1a6c9bc0dc13f0b653dd18df24af2eee0ba7b316c92987becfd10a5b
4
- data.tar.gz: c5bfae261147560bd41b85a3d18b1f672d99294eec70038687315dadeaddcb4a
3
+ metadata.gz: deef1d8ce0aa71474ace3e813e28df01736d209b043734b7d83f7bb214a8eb5a
4
+ data.tar.gz: 31c8f2258bd54bab4a5f31cebcaf510f965b812fb7aca526098bb18878a41ce3
5
5
  SHA512:
6
- metadata.gz: 9fec2040704b9d393992b7f9ba73be9ed4de414ba87d021b5aa80daff9b086594807f2ade26b55f3b7c23478d4476040862a019c5bef2538fe82cb5d7e3a20ed
7
- data.tar.gz: 882a81ff03783bfd18e9d2ac09a494ac8cd434e293db3d7a83735c0c5f8fe391a319ea596a31ed3680d1a542e9dbcb656e5fd711f3779a828cea6ff993b37e61
6
+ metadata.gz: b11967dff6d336fc3360b731ec6178b255b43e5c502580185541361c7ca3b9df41b5cf1f5c408c7cb1b2719722ff237f687d3bcf0f472b07edbb6137317655ec
7
+ data.tar.gz: 4f5238c70548687dc966d4a440a424176619f2255fb00f0a0508a21cf6642f888c7070016efba63e0c18f54c47bfe4873612f843b1058560cfc6bb951d378aff
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
- spec/dummy/log/
1
+ spec/dummy/log/*
2
2
  pkg/
3
- spec/dummy/tmp/
3
+ spec/dummy/tmp/*
4
+ spec/dummy/db/*
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rbexy (0.2.3)
4
+ rbexy (0.3.0)
5
5
  actionview (>= 5.0, < 7.0)
6
6
  activesupport (>= 5.0, < 7.0)
7
7
  railties (>= 5.0, < 7.0)
@@ -6,6 +6,7 @@ volumes:
6
6
  services:
7
7
  rbexy:
8
8
  build: .
9
+ image: rbexy
9
10
  volumes:
10
11
  - .:/app
11
12
  - bundle:/usr/local/bundle
@@ -13,3 +14,14 @@ services:
13
14
  - $HOME/.gitconfig:/root/.gitconfig:ro
14
15
  - $HOME/.gem/credentials:/root/.gem/credentials
15
16
  working_dir: /app
17
+ dummy:
18
+ image: rbexy
19
+ volumes:
20
+ - .:/app
21
+ - bundle:/usr/local/bundle
22
+ working_dir: /app/spec/dummy/
23
+ command: ./start.sh
24
+ ports:
25
+ - 3000:3000
26
+ environment:
27
+ - RAILS_LOG_STDOUT=1
@@ -1,25 +1,20 @@
1
1
  require "action_view"
2
+ require "active_support/core_ext/class/attribute"
2
3
 
3
4
  module Rbexy
4
5
  class Component < ActionView::Base
5
- class LookupContext < ActionView::LookupContext
6
- attr_accessor :component_name_stack
7
-
8
- def self.details_hash(context)
9
- context.registered_details.each_with_object({}) do |key, details_hash|
10
- value = key == :locale ? [context.locale] : context.send(key)
11
- details_hash[key] = value
12
- end
6
+ class TemplatePath < String
7
+ def to_s
8
+ self
13
9
  end
10
+ end
14
11
 
15
- # We override any calls to args_for_lookup and set partial=false so that
16
- # the lookup context doesn't automatically add a `_` prefix to the
17
- # template path, since we're using the Rails partial-rendering
18
- # functionality but don't want our templates prefixed with a `_`
19
- def args_for_lookup(name, prefixes, partial, keys, details_options)
20
- partial = false if component_name_stack.include?(name)
21
- super(name, prefixes, partial, keys, details_options)
22
- end
12
+ def self.component_name
13
+ name.underscore
14
+ end
15
+
16
+ def component_name
17
+ self.class.component_name
23
18
  end
24
19
 
25
20
  def initialize(view_context, **props)
@@ -45,10 +40,9 @@ module Rbexy
45
40
  end
46
41
 
47
42
  def call
48
- replace_lookup_context
49
- view_renderer.render(self, partial: component_name, &nil)
50
- ensure
51
- restore_lookup_context
43
+ path = TemplatePath.new(component_name)
44
+ template = view_context.lookup_context.find(path)
45
+ template.render(self, {})
52
46
  end
53
47
 
54
48
  def content
@@ -66,40 +60,13 @@ module Rbexy
66
60
  raise(ContextNotFound, "no parent context `#{name}`")
67
61
  end
68
62
 
69
- def view_renderer
70
- view_context.view_renderer
71
- end
72
-
73
- def component_name
74
- self.class.name.underscore
63
+ def compiled_method_container
64
+ Rbexy::Component
75
65
  end
76
66
 
77
67
  private
78
68
 
79
- attr_reader :view_context, :content_block, :old_lookup_context
80
-
81
- def replace_lookup_context
82
- unless view_renderer.lookup_context.is_a? Rbexy::Component::LookupContext
83
- @old_lookup_context = view_renderer.lookup_context
84
- view_renderer.lookup_context = build_lookup_context(old_lookup_context)
85
- end
86
-
87
- (view_renderer.lookup_context.component_name_stack ||= []) << component_name
88
- end
89
-
90
- def restore_lookup_context
91
- return unless old_lookup_context
92
- view_renderer.lookup_context = old_lookup_context
93
- @old_lookup_context = nil
94
- end
95
-
96
- def build_lookup_context(existing_context)
97
- paths = existing_context.view_paths.dup.unshift(
98
- *Rbexy.configuration.template_paths.map { |p| ActionView::OptimizedFileSystemResolver.new(p) }
99
- )
100
-
101
- LookupContext.new(paths, LookupContext.details_hash(existing_context))
102
- end
69
+ attr_reader :view_context, :content_block
103
70
 
104
71
  def method_missing(meth, *args, &block)
105
72
  if view_context.respond_to?(meth)
@@ -2,6 +2,7 @@ module Rbexy
2
2
  class Configuration
3
3
  attr_accessor :component_provider
4
4
  attr_accessor :template_paths
5
+ attr_accessor :enable_context
5
6
 
6
7
  def template_paths
7
8
  @template_paths ||= []
@@ -85,11 +85,14 @@ module Rbexy
85
85
  base_tag
86
86
  end
87
87
 
88
+ context_open = Rbexy.configuration.enable_context ? "rbexy_context.push({});" : nil
89
+ context_close = Rbexy.configuration.enable_context ? "rbexy_context.pop;" : nil
90
+
88
91
  [
89
92
  "Rbexy::OutputBuffer.new.tap { |output|",
90
- "rbexy_context.push({}) if defined?(Rbexy::Component) && self.is_a?(Rbexy::Component);",
91
- "output << (#{tag});",
92
- "rbexy_context.pop if defined?(Rbexy::Component) && self.is_a?(Rbexy::Component);",
93
+ context_open,
94
+ "output << (#{tag}).html_safe;",
95
+ context_close,
93
96
  "}.html_safe"
94
97
  ].join(" ")
95
98
  end
@@ -1,7 +1,9 @@
1
+ require "active_support/core_ext/string/output_safety"
2
+
1
3
  module Rbexy
2
- class OutputBuffer < String
4
+ class OutputBuffer < ActiveSupport::SafeBuffer
3
5
  def <<(content)
4
- value = content.is_a?(Array) ? content.join : content
6
+ value = content.is_a?(Array) ? content.join.html_safe : content
5
7
  super([nil, false].include?(value) ? "" : value.to_s)
6
8
  end
7
9
  end
@@ -4,5 +4,6 @@ module Rbexy
4
4
  module Rails
5
5
  autoload :Engine, "rbexy/rails/engine"
6
6
  autoload :ControllerHelper, "rbexy/rails/controller_helper"
7
+ autoload :ComponentTemplateResolver, "rbexy/rails/component_template_resolver"
7
8
  end
8
9
  end
@@ -0,0 +1,35 @@
1
+ require "action_view"
2
+
3
+ module Rbexy
4
+ module Rails
5
+ class ComponentTemplateResolver < ActionView::FileSystemResolver
6
+ # Rails 6 requires us to override `_find_all` in order to hook
7
+ def _find_all(name, prefix, partial, details, key, locals)
8
+ find_templates(name, prefix, partial, details, locals)
9
+ end
10
+
11
+ # Rails 5 only requires `find_templates` (which tbh is the proper way
12
+ # to implement subclasses of ActionView::Resolver)
13
+ def find_templates(name, prefix, partial, details, locals = [])
14
+ return [] unless name.is_a? Rbexy::Component::TemplatePath
15
+
16
+ templates_path = File.join(@path, prefix, name)
17
+ extensions = details[:handlers].join(",")
18
+
19
+ Dir["#{templates_path}.*{#{extensions}}"].map do |template_path|
20
+ source = File.binread(template_path)
21
+ handler = ActionView::Template.handler_for_extension(File.extname(template_path)[1..-1])
22
+ virtual_path = ["rbexy_component", prefix, name].join("/")
23
+
24
+ ActionView::Template.new(
25
+ source,
26
+ template_path,
27
+ handler,
28
+ locals: [],
29
+ virtual_path: virtual_path
30
+ )
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -1,3 +1,20 @@
1
+ require "active_support/concern"
2
+
3
+ module Rbexy
4
+ module Rails
1
5
  module ControllerHelper
6
+ extend ActiveSupport::Concern
7
+
2
8
  def rbexy_component_provider; end
9
+
10
+ class_methods do
11
+ def inherited(klass)
12
+ super
13
+ Rbexy.configuration.template_paths.each do |path|
14
+ prepend_view_path(Rbexy::Rails::ComponentTemplateResolver.new(path))
15
+ end
16
+ end
17
+ end
3
18
  end
19
+ end
20
+ end
@@ -24,6 +24,7 @@ module Rbexy
24
24
  require "rbexy/component_providers/rbexy_provider"
25
25
  config.component_provider = Rbexy::ComponentProviders::RbexyProvider.new
26
26
  config.template_paths << ::Rails.root.join("app", "components")
27
+ config.enable_context = true
27
28
  end
28
29
  end
29
30
  end
@@ -1,3 +1,3 @@
1
1
  module Rbexy
2
- VERSION = "0.2.3"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbexy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Giancola
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-10-24 00:00:00.000000000 Z
11
+ date: 2020-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -235,6 +235,7 @@ files:
235
235
  - lib/rbexy/output_buffer.rb
236
236
  - lib/rbexy/parser.rb
237
237
  - lib/rbexy/rails.rb
238
+ - lib/rbexy/rails/component_template_resolver.rb
238
239
  - lib/rbexy/rails/controller_helper.rb
239
240
  - lib/rbexy/rails/engine.rb
240
241
  - lib/rbexy/runtime.rb