rbexy 0.1.4 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1775f06e1058875736a53072d7d76c11f7320cea33176e5392afcdc8d1c95f76
4
- data.tar.gz: ccc233edf9234293845e67ebd8e8a27cc640c9f778e9bbb9c8063efd79cde318
3
+ metadata.gz: 65318a8a1a6c9bc0dc13f0b653dd18df24af2eee0ba7b316c92987becfd10a5b
4
+ data.tar.gz: c5bfae261147560bd41b85a3d18b1f672d99294eec70038687315dadeaddcb4a
5
5
  SHA512:
6
- metadata.gz: 877b65e6375f895a730420811d59b7e82937fb075709a3b42d8778d7b6e7a3e16119adffacf657184ca2cd21f30d5aecb0601a0250eb4a8095295d2cebb4a676
7
- data.tar.gz: c77c30e0c6420d1154022daa40dec11ed5a9fd416d83df8d002f3b28cde6e7a64d39d8154734ffec80a56ef56e60b1bbdbd842da7f7e9bc0a77863d31dd255e1
6
+ metadata.gz: 9fec2040704b9d393992b7f9ba73be9ed4de414ba87d021b5aa80daff9b086594807f2ade26b55f3b7c23478d4476040862a019c5bef2538fe82cb5d7e3a20ed
7
+ data.tar.gz: 882a81ff03783bfd18e9d2ac09a494ac8cd434e293db3d7a83735c0c5f8fe391a319ea596a31ed3680d1a542e9dbcb656e5fd711f3779a828cea6ff993b37e61
data/.gitignore CHANGED
@@ -1,2 +1,3 @@
1
1
  spec/dummy/log/
2
2
  pkg/
3
+ spec/dummy/tmp/
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rbexy (0.1.4)
4
+ rbexy (0.2.3)
5
5
  actionview (>= 5.0, < 7.0)
6
6
  activesupport (>= 5.0, < 7.0)
7
7
  railties (>= 5.0, < 7.0)
data/README.md CHANGED
@@ -375,6 +375,16 @@ Rbexy.configure do |config|
375
375
  end
376
376
  ```
377
377
 
378
+ Or in Rails you can customize the component provider just for a controller:
379
+
380
+ ```ruby
381
+ class ThingsController < ApplicationController
382
+ def rbexy_component_provider
383
+ MyComponentProvider.new
384
+ end
385
+ end
386
+ ```
387
+
378
388
  See `lib/rbexy/component_providers/` for example implementations.
379
389
 
380
390
  ## Usage outside of Rails
@@ -3,6 +3,8 @@ require "action_view"
3
3
  module Rbexy
4
4
  class Component < ActionView::Base
5
5
  class LookupContext < ActionView::LookupContext
6
+ attr_accessor :component_name_stack
7
+
6
8
  def self.details_hash(context)
7
9
  context.registered_details.each_with_object({}) do |key, details_hash|
8
10
  value = key == :locale ? [context.locale] : context.send(key)
@@ -15,7 +17,8 @@ module Rbexy
15
17
  # template path, since we're using the Rails partial-rendering
16
18
  # functionality but don't want our templates prefixed with a `_`
17
19
  def args_for_lookup(name, prefixes, partial, keys, details_options)
18
- super(name, prefixes, false, keys, details_options)
20
+ partial = false if component_name_stack.include?(name)
21
+ super(name, prefixes, partial, keys, details_options)
19
22
  end
20
23
  end
21
24
 
@@ -37,7 +40,6 @@ module Rbexy
37
40
  def setup(**props); end
38
41
 
39
42
  def render(&block)
40
- @content = nil
41
43
  @content_block = block_given? ? block : nil
42
44
  call
43
45
  end
@@ -50,7 +52,7 @@ module Rbexy
50
52
  end
51
53
 
52
54
  def content
53
- @content ||= content_block ? view_context.capture(self, &content_block) : ""
55
+ content_block ? view_context.capture(self, &content_block) : ""
54
56
  end
55
57
 
56
58
  def create_context(name, value)
@@ -77,9 +79,12 @@ module Rbexy
77
79
  attr_reader :view_context, :content_block, :old_lookup_context
78
80
 
79
81
  def replace_lookup_context
80
- return if view_renderer.lookup_context.is_a? Rbexy::Component::LookupContext
81
- @old_lookup_context = view_renderer.lookup_context
82
- view_renderer.lookup_context = build_lookup_context(old_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
83
88
  end
84
89
 
85
90
  def restore_lookup_context
@@ -0,0 +1,20 @@
1
+ module Rbexy
2
+ module ComponentProviders
3
+ class NamespacedRbexyProvider < Rbexy::ComponentProviders::RbexyProvider
4
+ attr_reader :namespaces
5
+
6
+ def initialize(*namespaces)
7
+ @namespaces = namespaces
8
+ end
9
+
10
+ def find(name)
11
+ namespaces.each do |namespace|
12
+ result = super("#{namespace}::#{name}")
13
+ return result if result != nil
14
+ end
15
+
16
+ super
17
+ end
18
+ end
19
+ end
20
+ end
@@ -10,8 +10,6 @@ module Rbexy
10
10
  find(name).new(context, **props).render(&block)
11
11
  end
12
12
 
13
- private
14
-
15
13
  def find(name)
16
14
  ActiveSupport::Inflector.constantize("#{name}Component")
17
15
  rescue NameError => e
@@ -10,8 +10,6 @@ module Rbexy
10
10
  find(name).new(**props).render_in(context, &block)
11
11
  end
12
12
 
13
- private
14
-
15
13
  def find(name)
16
14
  ActiveSupport::Inflector.constantize("#{name}Component")
17
15
  rescue NameError => e
@@ -2,7 +2,7 @@ module Rbexy
2
2
  autoload :Component, "rbexy/component"
3
3
 
4
4
  module Rails
5
- autoload :TemplateHandler, "rbexy/rails/template_handler"
6
5
  autoload :Engine, "rbexy/rails/engine"
6
+ autoload :ControllerHelper, "rbexy/rails/controller_helper"
7
7
  end
8
8
  end
@@ -0,0 +1,3 @@
1
+ module ControllerHelper
2
+ def rbexy_component_provider; end
3
+ end
@@ -4,10 +4,16 @@ module Rbexy
4
4
  module Rails
5
5
  class Engine < ::Rails::Engine
6
6
  initializer "rbexy" do |app|
7
- ActionView::Template.register_template_handler(:rbx, Rbexy::Rails::TemplateHandler)
7
+ template_handler = ::Rails.version.to_f >= 6.0 ?
8
+ proc { |template, source| Rbexy.compile(source) } :
9
+ proc { |template| Rbexy.compile(template.source) }
10
+
11
+ ActionView::Template.register_template_handler(:rbx, template_handler)
8
12
 
9
13
  ActiveSupport.on_load :action_controller_base do
10
14
  helper Rbexy::ViewContextHelper
15
+ helper_method :rbexy_component_provider
16
+ include ControllerHelper
11
17
  end
12
18
 
13
19
  if defined?(ViewComponent)
@@ -11,7 +11,11 @@ module Rbexy
11
11
 
12
12
  DefaultTagBuilder = ActionView::Helpers::TagHelper::TagBuilder
13
13
 
14
- def self.create_tag_builder(context, provider = Rbexy.configuration.component_provider)
14
+ def self.create_tag_builder(context, provider = nil)
15
+ provider = provider ||
16
+ provider_from_context(context) ||
17
+ Rbexy.configuration.component_provider
18
+
15
19
  if provider
16
20
  ComponentTagBuilder.new(context, provider)
17
21
  else
@@ -19,6 +23,12 @@ module Rbexy
19
23
  end
20
24
  end
21
25
 
26
+ def self.provider_from_context(context)
27
+ if context.respond_to?(:rbexy_component_provider)
28
+ context.rbexy_component_provider
29
+ end
30
+ end
31
+
22
32
  def initialize(component_provider = nil)
23
33
  @rbexy_tag = self.class.create_tag_builder(self, component_provider)
24
34
  end
@@ -1,3 +1,3 @@
1
1
  module Rbexy
2
- VERSION = "0.1.4"
2
+ VERSION = "0.2.3"
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.1.4
4
+ version: 0.2.3
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-13 00:00:00.000000000 Z
11
+ date: 2020-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -224,6 +224,7 @@ files:
224
224
  - example.rb
225
225
  - lib/rbexy.rb
226
226
  - lib/rbexy/component.rb
227
+ - lib/rbexy/component_providers/namespaced_rbexy_provider.rb
227
228
  - lib/rbexy/component_providers/rbexy_provider.rb
228
229
  - lib/rbexy/component_providers/view_component_provider.rb
229
230
  - lib/rbexy/component_tag_builder.rb
@@ -234,8 +235,8 @@ files:
234
235
  - lib/rbexy/output_buffer.rb
235
236
  - lib/rbexy/parser.rb
236
237
  - lib/rbexy/rails.rb
238
+ - lib/rbexy/rails/controller_helper.rb
237
239
  - lib/rbexy/rails/engine.rb
238
- - lib/rbexy/rails/template_handler.rb
239
240
  - lib/rbexy/runtime.rb
240
241
  - lib/rbexy/version.rb
241
242
  - lib/rbexy/view_context_helper.rb
@@ -1,9 +0,0 @@
1
- module Rbexy
2
- module Rails
3
- class TemplateHandler
4
- def self.call(template, source)
5
- Rbexy.compile(source)
6
- end
7
- end
8
- end
9
- end