From baefc249f02512510b2f0d2a0ddf049560bc20fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Tue, 9 Jun 2020 00:18:15 +0200 Subject: [PATCH] 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. --- Gemfile | 1 + Gemfile.lock | 3 +++ app/components/application_component.rb | 2 ++ config/application.rb | 1 + config/initializers/wicked_pdf.rb | 24 ++++++++++++++++++++++++ spec/rails_helper.rb | 5 +++++ 6 files changed, 36 insertions(+) create mode 100644 app/components/application_component.rb diff --git a/Gemfile b/Gemfile index 32e1f6f0a..9f26113fe 100644 --- a/Gemfile +++ b/Gemfile @@ -59,6 +59,7 @@ gem "translator-text", "~> 0.1.0" gem "turbolinks", "~> 5.2.1" gem "turnout", "~> 2.5.0" gem "uglifier", "~> 4.2.0" +gem "view_component", "~> 2.19.1", require: "view_component/engine" gem "whenever", "~> 1.0.0", require: false gem "wicked_pdf", "~> 2.1.0" gem "wkhtmltopdf-binary", "~> 0.12.4" diff --git a/Gemfile.lock b/Gemfile.lock index 89922ea49..e44bb04ed 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -608,6 +608,8 @@ GEM uniform_notifier (1.13.0) user_agent_parser (2.6.0) uuidtools (2.1.5) + view_component (2.19.1) + activesupport (>= 5.0.0, < 7.0) warden (1.2.9) rack (>= 2.0.9) wasabi (3.5.0) @@ -728,6 +730,7 @@ DEPENDENCIES turbolinks (~> 5.2.1) turnout (~> 2.5.0) uglifier (~> 4.2.0) + view_component (~> 2.19.1) web-console (~> 3.7.0) webdrivers (~> 4.4.1) whenever (~> 1.0.0) diff --git a/app/components/application_component.rb b/app/components/application_component.rb new file mode 100644 index 000000000..3db4d0451 --- /dev/null +++ b/app/components/application_component.rb @@ -0,0 +1,2 @@ +class ApplicationComponent < ViewComponent::Base +end diff --git a/config/application.rb b/config/application.rb index 819918788..935dffa93 100644 --- a/config/application.rb +++ b/config/application.rb @@ -112,6 +112,7 @@ module Consul # * English: https://github.com/consul/consul/blob/master/CUSTOMIZE_EN.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/models/custom" config.paths["app/views"].unshift(Rails.root.join("app", "views", "custom")) diff --git a/config/initializers/wicked_pdf.rb b/config/initializers/wicked_pdf.rb index 824095dc3..37820d285 100644 --- a/config/initializers/wicked_pdf.rb +++ b/config/initializers/wicked_pdf.rb @@ -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 # # Use this to set up shared configuration options for your entire application. diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index c4756dd02..717007659 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -11,6 +11,11 @@ require "spec_helper" require "capybara/rails" require "capybara/rspec" 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?