rux-rails 1.3.0 → 1.5.0

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: 69a808afcb08d05209e5e4c8a759bf96524ec8f2580647ac216332a585271fac
4
- data.tar.gz: bce97762fcd5076e2a572661aedc2a02424fdabf804f1d869d8a7156acc73c0a
3
+ metadata.gz: 7b7a679a3ee90ae6b529db0784f4b94012e90bbb25830520cdfd9e78d7ceb799
4
+ data.tar.gz: 43babb6951b66022a80188c441f3a8ef052255d15c7d95e38814c2af896e4a04
5
5
  SHA512:
6
- metadata.gz: 73031ea1107540cf63e5834a8b7cdb46e7470d49832cc45b4b86f51edbd5a29611bb120b8f6cefa35505a6c2e2173c08cab1b6996fc2e3d7b435f58d37c66daa
7
- data.tar.gz: b3a6049fbbfd8972a9c09dd445193da594f23080d0b0b121bf2b795c0683dac64eb248eaa22de6278543479d08d5d7b42eac84984a72e77c6de628ae5cfc386a
6
+ metadata.gz: bebca880ba79a25eed37a0b1bb5fd8052a03d7a580301a5adc1ee8cab9e79b317cae245696521f98b3fd363ab916eae585796244330c3096711516fdf304a6f4
7
+ data.tar.gz: 9bdc46fd60266214a5eae8c4a83181625bf1ba87cdd33b512ad766169344618c83801203535c5490e9060ac8ea9cfa7b2db82f813039939497695de5ef5b1c0c
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## 1.5.0
2
+ * Extract monkeypatches into the [onload gem](https://github.com/camertron/onload).
3
+ * Upgrade to rux v1.2.
4
+ * Drop official support for Rails < 6 and Ruby < 3.
5
+
6
+ ## 1.4.0
7
+ * Make rux-rails safe to load in production.
8
+ - All the monkeypatches to `Kernel`, etc are now only loaded if `config.rux.transpile` is `true`.
9
+
1
10
  ## 1.3.0
2
11
  * Add support for view_component v3.0.
3
12
 
data/Gemfile CHANGED
@@ -2,15 +2,15 @@ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
5
- gem 'rux', github: 'camertron/rux' # path: '/Users/cdutro/workspace/rux'
6
-
7
5
  group :development, :test do
8
- gem 'pry-byebug'
9
6
  gem 'rake'
7
+ gem 'debug'
10
8
  end
11
9
 
12
10
  group :development do
13
11
  gem 'appraisal'
12
+ gem 'benchmark-ips'
13
+ gem 'slim', '~> 5.0'
14
14
  end
15
15
 
16
16
  group :test do
data/README.md CHANGED
@@ -81,11 +81,9 @@ In my humble opinion, the rux version:
81
81
 
82
82
  Integrating rux into your Rails app is pretty straightforward.
83
83
 
84
- 1. Add rux-rails to the development group in your Gemfile, eg:
84
+ 1. Add rux-rails to your Gemfile, eg:
85
85
  ```ruby
86
- group :development do
87
- gem 'rux-rails', '~> 1.0'
88
- end
86
+ gem 'rux-rails', '~> 1.0'
89
87
  ```
90
88
  1. Run `bundle install`
91
89
  1. ... that's it.
@@ -104,7 +102,7 @@ config.rux.transpile = false
104
102
 
105
103
  ### Production Environment
106
104
 
107
- By including rux-rails in the `:development` group in your Gemfile, rux-rails and its monkeypatches to `Kernel` aren't loaded in production (which is a good thing). You could choose to include rux-rails in the default group, but automatic transpilation of .rux files is disabled in the production environment for the same reasons the asset pipeline is disabled. Your view components (and static assets) don't change once deployed, so it's more efficient to precompile (or pre-transpile) them before deploying. Rux-rails comes with a handy rake task that can pre-transpile all your .rux templates:
105
+ By default, `config.rux.transpile` is set to `false` in the production environment, which means rux-rails' monkeypatches to `Kernel`, etc aren't loaded in production (which is a good thing). You could choose to turn it on, but automatic transpilation of .rux files is disabled automatically in the production environment for the same reasons the asset pipeline is disabled. Your view components (and static assets) don't change once deployed, so it's more efficient to precompile (or pre-transpile) them before deploying. Rux-rails comes with a handy rake task that can pre-transpile all your .rux templates:
108
106
 
109
107
  ```bash
110
108
  bundle exec rake rux:transpile
@@ -0,0 +1,13 @@
1
+ require 'action_view'
2
+
3
+ module RuxRails
4
+ class OutputBuffer < ActionView::OutputBuffer
5
+ def safe_append(obj)
6
+ Array(obj).each { |o| self.safe_append=(o) }
7
+ end
8
+
9
+ def append(obj)
10
+ Array(obj).each { |o| self.append=(o) }
11
+ end
12
+ end
13
+ end
@@ -1,58 +1,32 @@
1
1
  require 'rails/railtie'
2
-
3
- require 'active_support'
4
- require 'active_support/isolated_execution_state' if ActiveSupport::VERSION::MAJOR > 6
2
+ require 'onload'
5
3
 
6
4
  module RuxRails
7
5
  class Railtie < Rails::Railtie
8
6
  config.rux = ActiveSupport::OrderedOptions.new
9
7
 
10
- initializer 'rux-rails.initialize', before: :set_autoload_paths do |app|
11
- if Rails.respond_to?(:autoloaders) && Rails.autoloaders.zeitwerk_enabled?
12
- require 'rux-rails/core_ext/kernel_zeitwerk'
13
- require 'rux-rails/ext/zeitwerk/loader'
8
+ initializer 'rux-rails.initialize', before: 'onload.initialize' do |app|
9
+ ViewComponent::Base.send(:include, RuxRails::Components)
14
10
 
15
- RuxRails.zeitwerk_mode = true
11
+ if config.rux.transpile.nil? && (Rails.env.development? || Rails.env.test?)
12
+ config.rux.transpile = true
13
+ Onload.enabled = true
16
14
  else
17
- require 'rux-rails/core_ext/kernel'
18
- require 'rux-rails/ext/activesupport/dependencies'
19
-
20
- RuxRails.zeitwerk_mode = false
15
+ Onload.enabled = config.rux.transpile
21
16
  end
22
17
 
23
- begin
24
- require 'bootsnap'
25
- rescue LoadError
26
- else
27
- require 'rux-rails/ext/bootsnap/autoload'
18
+ if config.rux.transpile
19
+ Onload.register('.rux', RuxRails::RuxLoader)
28
20
  end
29
21
 
30
22
  ActionView::Template.register_template_handler(
31
23
  :ruxt, RuxRails::TemplateHandler
32
24
  )
33
25
 
34
- if config.rux.transpile.nil? && (Rails.env.development? || Rails.env.test?)
35
- config.rux.transpile = true
36
- end
37
-
38
- RuxRails.transpile_on_load = -> () { config.rux.transpile }
39
-
40
26
  Rux.library_paths.each do |library_path|
41
27
  app.config.eager_load_paths << library_path
42
28
  app.config.autoload_paths << library_path
43
29
  end
44
-
45
- if config.rux.transpile && app.config.file_watcher
46
- paths = Set.new(app.config.eager_load_paths + app.config.autoload_paths)
47
-
48
- dirs = paths.each_with_object({}) do |path, ret|
49
- ret[path] = %w(rux)
50
- end
51
-
52
- app.reloaders << app.config.file_watcher.new([], dirs) do
53
- # empty block, watcher seems to need it?
54
- end
55
- end
56
30
  end
57
31
 
58
32
  rake_tasks do
@@ -0,0 +1,7 @@
1
+ module RuxRails
2
+ class RuxLoader
3
+ def self.call(source)
4
+ Rux.to_ruby(source)
5
+ end
6
+ end
7
+ end
@@ -2,8 +2,8 @@ require 'rux'
2
2
 
3
3
  module RuxRails
4
4
  class TagBuilder < ::Rux::DefaultTagBuilder
5
- def call(*args)
6
- super.html_safe
5
+ def call(*args, &block)
6
+ build(*args, &block).html_safe
7
7
  end
8
8
  end
9
9
  end
@@ -1,3 +1,3 @@
1
1
  module RuxRails
2
- VERSION = '1.3.0'
2
+ VERSION = '1.5.0'
3
3
  end
data/lib/rux-rails.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  module RuxRails
2
2
  autoload :Components, 'rux-rails/components'
3
- autoload :SafeBuffer, 'rux-rails/safe_buffer'
3
+ autoload :OutputBuffer, 'rux-rails/output_buffer'
4
+ autoload :RuxLoader, 'rux-rails/rux_loader'
4
5
  autoload :TagBuilder, 'rux-rails/tag_builder'
5
6
  autoload :TemplateHandler, 'rux-rails/template_handler'
6
7
  autoload :Visitor, 'rux-rails/visitor'
@@ -16,23 +17,14 @@ module RuxRails
16
17
  def tag_builder
17
18
  @tag_builder ||= TagBuilder.new
18
19
  end
19
-
20
- def transpile_on_load?
21
- transpile_on_load.call
22
- end
23
20
  end
24
21
 
25
- self.transpile_on_load = -> () { true }
22
+ self.transpile_on_load = true
26
23
  end
27
24
 
28
25
 
29
26
  require 'rux'
30
27
  require 'rux-rails/railtie'
31
28
 
32
- # require both of these to support multiple versions of view_component
33
- require 'view_component'
34
- require 'view_component/engine'
35
-
36
- ViewComponent::Base.send(:include, RuxRails::Components)
37
29
  Rux.tag_builder = RuxRails.tag_builder
38
- Rux.buffer = RuxRails::SafeBuffer
30
+ Rux.buffer = RuxRails::OutputBuffer
data/rux-rails.gemspec CHANGED
@@ -10,9 +10,10 @@ Gem::Specification.new do |s|
10
10
  s.description = s.summary = 'Rux view components on Rails.'
11
11
  s.platform = Gem::Platform::RUBY
12
12
 
13
- s.add_dependency 'rux', '~> 1.0'
13
+ s.add_dependency 'rux', '~> 1.2'
14
14
  s.add_dependency 'railties', '>= 5.0'
15
15
  s.add_dependency 'view_component', '>= 2', '< 4'
16
+ s.add_dependency 'onload', '~> 1.0'
16
17
 
17
18
  s.require_path = 'lib'
18
19
 
@@ -2,7 +2,12 @@ require 'spec_helper'
2
2
 
3
3
  describe HomeController, type: :request do
4
4
  describe '#index' do
5
- it 'works' do
5
+ before(:each) do
6
+ # Force a recompile to avoid test pollution
7
+ HomeComponent.compile
8
+ end
9
+
10
+ it 'transpiles the file' do
6
11
  get '/'
7
12
 
8
13
  expect(response).to have_http_status(:ok)
@@ -10,5 +15,36 @@ describe HomeController, type: :request do
10
15
  have_selector("div.container img[src='/images/cat.png'] + button", text: "Click Me!")
11
16
  )
12
17
  end
18
+
19
+ it "allows hot reloading" do
20
+ get "/"
21
+
22
+ expect(response).to have_http_status(:ok)
23
+ expect(response.body).to(
24
+ have_selector(".container", text: "Click Me!")
25
+ )
26
+
27
+ new_contents = <<~RUX
28
+ class HomeComponent < ViewComponent::Base
29
+ def call
30
+ <div class="container">
31
+ <Image src="cat.png" size="40" />
32
+ <Button outline={true} disabled size={:large}>
33
+ Click if you dare
34
+ </Button>
35
+ </div>
36
+ end
37
+ end
38
+ RUX
39
+
40
+ with_file_contents(Rails.root.join(*%w[app components home_component.rux]), new_contents) do
41
+ get "/"
42
+
43
+ expect(response).to have_http_status(:ok)
44
+ expect(response.body).to(
45
+ have_selector(".container", text: "Click if you dare")
46
+ )
47
+ end
48
+ end
13
49
  end
14
50
  end
@@ -0,0 +1,13 @@
1
+ class HtmlSafetyComponent < ViewComponent::Base
2
+ def initialize(value:)
3
+ @value = value
4
+ end
5
+
6
+ def call
7
+ Rux.tag("div") {
8
+ Rux.create_buffer.tap { |_rux_buf_|
9
+ _rux_buf_.append(@value)
10
+ }.to_s
11
+ }
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ class HtmlSafetyComponent < ViewComponent::Base
2
+ def initialize(value:)
3
+ @value = value
4
+ end
5
+
6
+ def call
7
+ <div>{@value}</div>
8
+ end
9
+ end
@@ -1,3 +1,7 @@
1
+ # require both of these to support multiple versions of view_component
2
+ require 'view_component'
3
+ require 'view_component/engine'
4
+
1
5
  module RuxRails
2
6
  class DummyApplication < ::Rails::Application
3
7
  if config.respond_to?(:load_defaults)