Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare for ViewComponents #1749

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
8 changes: 8 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ gem("sassc-rails")
# https://github.com/hotwired/stimulus-rails/issues/108
gem("sprockets", "~>4.2.1")

# ViewComponents for separation of concerns and much faster template rendering
gem("view_component")
# Improved defaults for ViewComponents
# https://evilmartians.com/chronicles/viewcomponent-in-the-wild-supercharging-your-components
gem("view_component-contrib")

# Security fix updates via Dependabot
# CVE-2021-41817 regex denial of service vulnerability
gem("date", ">= 3.2.1")
Expand Down Expand Up @@ -231,3 +237,5 @@ group :production do
# https://newrelic.com/
gem("newrelic_rpm")
end

gem "dry-initializer", "~> 3.1"
12 changes: 12 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ GEM
irb (~> 1.10)
reline (>= 0.3.8)
docile (1.4.0)
dry-initializer (3.1.1)
erubi (1.12.0)
execjs (2.9.1)
fastimage (2.3.0)
Expand Down Expand Up @@ -234,6 +235,7 @@ GEM
rack (>= 1.1)
rubocop (>= 1.33.0, < 2.0)
rubocop-ast (>= 1.30.0, < 2.0)
ruby-next-core (1.0.0)
ruby-progressbar (1.13.0)
rubyzip (2.3.2)
sassc (2.4.0)
Expand Down Expand Up @@ -281,6 +283,13 @@ GEM
kgio (~> 2.6)
raindrops (~> 0.7)
uniform_notifier (1.16.0)
view_component (3.8.0)
activesupport (>= 5.2.0, < 8.0)
concurrent-ruby (~> 1.0)
method_source (~> 1.0)
view_component-contrib (0.2.2)
ruby-next-core (>= 0.15.0)
view_component
web-console (4.2.1)
actionview (>= 6.0.0)
activemodel (>= 6.0.0)
Expand Down Expand Up @@ -333,6 +342,7 @@ DEPENDENCIES
database_cleaner-active_record
date (>= 3.2.1)
debug (>= 1.0.0)
dry-initializer (~> 3.1)
fastimage
i18n
importmap-rails
Expand Down Expand Up @@ -370,6 +380,8 @@ DEPENDENCIES
terser
turbo-rails
unicorn
view_component
view_component-contrib
web-console
webmock
xmlrpc
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ class ApplicationController < ActionController::Base
require "csv"
include LoginSystem

# Allow folder organization in the app/views folder
append_view_path Rails.root.join("app/views/controllers")

# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
Expand Down
6 changes: 6 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,10 @@ def get_next_id(object)

query.result_ids[idx + 1] || query.result_ids[idx - 1]
end

# make View components easier to call
def component(name, *args, **kwargs, &block)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depending on how you and others feel about all-arguments forwarding,
the following should work here. Your choice:

Suggested change
def component(name, *args, **kwargs, &block)
def component(name, ...)

component = name.to_s.camelize.constantize::Component
render(component.new(*args, **kwargs), &block)
end
end
3 changes: 3 additions & 0 deletions app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

# Base class for mailers for each type of email
class ApplicationMailer < ActionMailer::Base
# Allow folder organization in the app/views folder
append_view_path Rails.root.join("app/views/mailers")

# Use native Ruby URI::MailTo class
def self.valid_email_address?(address)
address.to_s.match?(URI::MailTo::EMAIL_REGEXP)
Expand Down
5 changes: 5 additions & 0 deletions app/views/components/application_view_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class ApplicationViewComponent < ViewComponentContrib::Base
extend Dry::Initializer
end
5 changes: 5 additions & 0 deletions app/views/components/application_view_component_preview.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class ApplicationViewComponentPreview < ViewComponentContrib::Preview::Base
self.abstract_class = true
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading