rux-rails 1.4.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: 80c3381d7ae7854540067cd7267c5636961a7b30a538fad7c5fd063c5cb12a64
4
- data.tar.gz: c4234d93e5b93109b612a5249f06b065cc53157999246cc04f12aea1bdc98cad
3
+ metadata.gz: 7b7a679a3ee90ae6b529db0784f4b94012e90bbb25830520cdfd9e78d7ceb799
4
+ data.tar.gz: 43babb6951b66022a80188c441f3a8ef052255d15c7d95e38814c2af896e4a04
5
5
  SHA512:
6
- metadata.gz: 8b61ed880646e3a058c4bd525f6f7fe9e308fe066f40d76c53ea312a5c604874eb321405f9d41f3a026221df3a83b3049356c1146f897ca7c9c4cf96b5274443
7
- data.tar.gz: 6aa7c072623ab565de33479b0428c096412377f9cf020c0c91751cf9b80e2f21fad1c2f8500a95b2b67dd5450a985ef8a5d0162ac72978d6cd1de04d706b64dc
6
+ metadata.gz: bebca880ba79a25eed37a0b1bb5fd8052a03d7a580301a5adc1ee8cab9e79b317cae245696521f98b3fd363ab916eae585796244330c3096711516fdf304a6f4
7
+ data.tar.gz: 9bdc46fd60266214a5eae8c4a83181625bf1ba87cdd33b512ad766169344618c83801203535c5490e9060ac8ea9cfa7b2db82f813039939497695de5ef5b1c0c
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
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
+
1
6
  ## 1.4.0
2
7
  * Make rux-rails safe to load in production.
3
8
  - All the monkeypatches to `Kernel`, etc are now only loaded if `config.rux.transpile` is `true`.
data/Gemfile CHANGED
@@ -2,17 +2,14 @@ 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'
14
12
  gem 'benchmark-ips'
15
- gem 'rails', '~> 7.0'
16
13
  gem 'slim', '~> 5.0'
17
14
  end
18
15
 
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,38 +1,22 @@
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|
8
+ initializer 'rux-rails.initialize', before: 'onload.initialize' do |app|
9
+ ViewComponent::Base.send(:include, RuxRails::Components)
10
+
11
11
  if config.rux.transpile.nil? && (Rails.env.development? || Rails.env.test?)
12
12
  config.rux.transpile = true
13
+ Onload.enabled = true
14
+ else
15
+ Onload.enabled = config.rux.transpile
13
16
  end
14
17
 
15
- RuxRails.transpile_on_load = -> () { config.rux.transpile }
16
-
17
18
  if config.rux.transpile
18
- if Rails.respond_to?(:autoloaders) && Rails.autoloaders.zeitwerk_enabled?
19
- require 'rux-rails/core_ext/kernel_zeitwerk'
20
- require 'rux-rails/ext/zeitwerk/loader'
21
-
22
- RuxRails.zeitwerk_mode = true
23
- else
24
- require 'rux-rails/core_ext/kernel'
25
- require 'rux-rails/ext/activesupport/dependencies'
26
-
27
- RuxRails.zeitwerk_mode = false
28
- end
29
-
30
- begin
31
- require 'bootsnap'
32
- rescue LoadError
33
- else
34
- require 'rux-rails/ext/bootsnap/autoload'
35
- end
19
+ Onload.register('.rux', RuxRails::RuxLoader)
36
20
  end
37
21
 
38
22
  ActionView::Template.register_template_handler(
@@ -43,18 +27,6 @@ module RuxRails
43
27
  app.config.eager_load_paths << library_path
44
28
  app.config.autoload_paths << library_path
45
29
  end
46
-
47
- if config.rux.transpile && app.config.file_watcher
48
- paths = Set.new(app.config.eager_load_paths + app.config.autoload_paths)
49
-
50
- dirs = paths.each_with_object({}) do |path, ret|
51
- ret[path] = %w(rux)
52
- end
53
-
54
- app.reloaders << app.config.file_watcher.new([], dirs) do
55
- # empty block, watcher seems to need it?
56
- end
57
- end
58
30
  end
59
31
 
60
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.4.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)