Simplify code to find a content block
We're only calling the `block_for` method from one place: the
`content_block` helper, and we're never passing the locale parameter to
that helper, which means we're always calling `block_for` using
`I18n.locale`.
So I'm not sure why we were doing the `locale ||=` assignment, since
`I18n.locale` doesn't return nil.
In any case, since this was added in commit c1de2dced, Ruby has added
support for arguments forwarding, so we can use it here to simplify the
code a little bit.
This commit is contained in:
@@ -46,8 +46,8 @@ module ApplicationHelper
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def content_block(name, locale = I18n.locale)
|
def content_block(...)
|
||||||
SiteCustomization::ContentBlock.block_for(name, locale)
|
SiteCustomization::ContentBlock.block_for(...)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.asset_data_base64(path)
|
def self.asset_data_base64(path)
|
||||||
|
|||||||
@@ -4,8 +4,7 @@ class SiteCustomization::ContentBlock < ApplicationRecord
|
|||||||
validates :locale, presence: true, inclusion: { in: ->(*) { Setting.enabled_locales.map(&:to_s) }}
|
validates :locale, presence: true, inclusion: { in: ->(*) { Setting.enabled_locales.map(&:to_s) }}
|
||||||
validates :name, presence: true, uniqueness: { scope: :locale }, inclusion: { in: ->(*) { VALID_BLOCKS }}
|
validates :name, presence: true, uniqueness: { scope: :locale }, inclusion: { in: ->(*) { VALID_BLOCKS }}
|
||||||
|
|
||||||
def self.block_for(name, locale)
|
def self.block_for(name, locale = I18n.locale)
|
||||||
locale ||= I18n.default_locale
|
|
||||||
find_by(name: name, locale: locale)&.body
|
find_by(name: name, locale: locale)&.body
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user