Allow using components with view_component
While Rails provides a lot of functionality by default, there's one missing piece which is present in frameworks like Django or Phoenix: the so-called "view models", or "components". It isn't easy to extract methods in a standard Rails view/partial, since extracting them to a helper will make them available to all views, and so two helper methods can't have the same name. It's also hard to organize the code in modules, and due to that it's hard to figure out where a certain helper method is supposed to be called from. Furthermore, object-oriented techniques like inheritance can't be applied, and so in CONSUL customizing views is harder that customizing models. Components fix all these issues, and work the way Ruby objects usually do. Components are also a pattern whose popularity has increased a lot in the last few years, with JavaScript frameworks like React using them heavily. While React's components aren't exactly the same as the components we're going to use, the concept is really similar. I've always liked the idea of components. However, there wasn't a stable gem we could safely use. The most popular gem (cells) hasn't been maintained for years, and we have to be very careful choosing which gems CONSUL should depend on. The view_component gem is maintained by GitHub, which is as a guarantee of future maintenance as it can be (not counting the Rails core team), and its usage started growing after RailsConf 2019. While that's certainly not a huge amount of time, it's not that we're using an experimental gem either. There's currently a conflict between view_component and wicked_pdf. We're adding a monkey-patch with the fix until it's merged in wicked_pdf.
This commit is contained in:
1
Gemfile
1
Gemfile
@@ -59,6 +59,7 @@ gem "translator-text", "~> 0.1.0"
|
|||||||
gem "turbolinks", "~> 5.2.1"
|
gem "turbolinks", "~> 5.2.1"
|
||||||
gem "turnout", "~> 2.5.0"
|
gem "turnout", "~> 2.5.0"
|
||||||
gem "uglifier", "~> 4.2.0"
|
gem "uglifier", "~> 4.2.0"
|
||||||
|
gem "view_component", "~> 2.19.1", require: "view_component/engine"
|
||||||
gem "whenever", "~> 1.0.0", require: false
|
gem "whenever", "~> 1.0.0", require: false
|
||||||
gem "wicked_pdf", "~> 2.1.0"
|
gem "wicked_pdf", "~> 2.1.0"
|
||||||
gem "wkhtmltopdf-binary", "~> 0.12.4"
|
gem "wkhtmltopdf-binary", "~> 0.12.4"
|
||||||
|
|||||||
@@ -608,6 +608,8 @@ GEM
|
|||||||
uniform_notifier (1.13.0)
|
uniform_notifier (1.13.0)
|
||||||
user_agent_parser (2.6.0)
|
user_agent_parser (2.6.0)
|
||||||
uuidtools (2.1.5)
|
uuidtools (2.1.5)
|
||||||
|
view_component (2.19.1)
|
||||||
|
activesupport (>= 5.0.0, < 7.0)
|
||||||
warden (1.2.9)
|
warden (1.2.9)
|
||||||
rack (>= 2.0.9)
|
rack (>= 2.0.9)
|
||||||
wasabi (3.5.0)
|
wasabi (3.5.0)
|
||||||
@@ -728,6 +730,7 @@ DEPENDENCIES
|
|||||||
turbolinks (~> 5.2.1)
|
turbolinks (~> 5.2.1)
|
||||||
turnout (~> 2.5.0)
|
turnout (~> 2.5.0)
|
||||||
uglifier (~> 4.2.0)
|
uglifier (~> 4.2.0)
|
||||||
|
view_component (~> 2.19.1)
|
||||||
web-console (~> 3.7.0)
|
web-console (~> 3.7.0)
|
||||||
webdrivers (~> 4.4.1)
|
webdrivers (~> 4.4.1)
|
||||||
whenever (~> 1.0.0)
|
whenever (~> 1.0.0)
|
||||||
|
|||||||
2
app/components/application_component.rb
Normal file
2
app/components/application_component.rb
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
class ApplicationComponent < ViewComponent::Base
|
||||||
|
end
|
||||||
@@ -112,6 +112,7 @@ module Consul
|
|||||||
# * English: https://github.com/consul/consul/blob/master/CUSTOMIZE_EN.md
|
# * English: https://github.com/consul/consul/blob/master/CUSTOMIZE_EN.md
|
||||||
# * Spanish: https://github.com/consul/consul/blob/master/CUSTOMIZE_ES.md
|
# * Spanish: https://github.com/consul/consul/blob/master/CUSTOMIZE_ES.md
|
||||||
#
|
#
|
||||||
|
config.autoload_paths << "#{Rails.root}/app/components/custom"
|
||||||
config.autoload_paths << "#{Rails.root}/app/controllers/custom"
|
config.autoload_paths << "#{Rails.root}/app/controllers/custom"
|
||||||
config.autoload_paths << "#{Rails.root}/app/models/custom"
|
config.autoload_paths << "#{Rails.root}/app/models/custom"
|
||||||
config.paths["app/views"].unshift(Rails.root.join("app", "views", "custom"))
|
config.paths["app/views"].unshift(Rails.root.join("app", "views", "custom"))
|
||||||
|
|||||||
@@ -1,3 +1,27 @@
|
|||||||
|
class WickedPdf
|
||||||
|
# Wicked Pdf magic breaks ViewComponent
|
||||||
|
# https://github.com/mileszs/wicked_pdf/pull/925
|
||||||
|
module PdfHelper
|
||||||
|
def render(*args)
|
||||||
|
options = args.first
|
||||||
|
if options.is_a?(Hash) && options.key?(:pdf)
|
||||||
|
render_with_wicked_pdf(options)
|
||||||
|
else
|
||||||
|
super
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_to_string(*args)
|
||||||
|
options = args.first
|
||||||
|
if options.is_a?(Hash) && options.key?(:pdf)
|
||||||
|
render_to_string_with_wicked_pdf(options)
|
||||||
|
else
|
||||||
|
super
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# WickedPDF Global Configuration
|
# WickedPDF Global Configuration
|
||||||
#
|
#
|
||||||
# Use this to set up shared configuration options for your entire application.
|
# Use this to set up shared configuration options for your entire application.
|
||||||
|
|||||||
@@ -11,6 +11,11 @@ require "spec_helper"
|
|||||||
require "capybara/rails"
|
require "capybara/rails"
|
||||||
require "capybara/rspec"
|
require "capybara/rspec"
|
||||||
require "selenium/webdriver"
|
require "selenium/webdriver"
|
||||||
|
require "view_component/test_helpers"
|
||||||
|
|
||||||
|
RSpec.configure do |config|
|
||||||
|
config.include ViewComponent::TestHelpers, type: :component
|
||||||
|
end
|
||||||
|
|
||||||
Rails.application.load_tasks if Rake::Task.tasks.empty?
|
Rails.application.load_tasks if Rake::Task.tasks.empty?
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user