Simplify using helper methods in components

The `use_helpers` method was added in ViewComponent 3.8.0, and it's
included by default in all components since version 3.11.0.

Note we sometimes delegated the `can?` method to the controller instead
of the helpers, for no particularly reason. We're unifying that code as
well.
This commit is contained in:
Javi Martín
2024-02-23 22:34:01 +01:00
parent 52ec5094f0
commit 1d955b7a20
70 changed files with 70 additions and 70 deletions

View File

@@ -1,6 +1,6 @@
class Admin::AllowedTableActionsComponent < ApplicationComponent
attr_reader :record, :options
delegate :can?, to: :helpers
use_helpers :can?
delegate :action, to: :table_actions_component
def initialize(record, **options)

View File

@@ -1,6 +1,6 @@
class Admin::Budgets::CalculateWinnersButtonComponent < ApplicationComponent
attr_reader :budget, :from_investments
delegate :can?, to: :helpers
use_helpers :can?
def initialize(budget, from_investments: false)
@budget = budget

View File

@@ -1,5 +1,5 @@
class Admin::Budgets::DraftingComponent < ApplicationComponent
delegate :can?, to: :controller
use_helpers :can?
attr_reader :budget
def initialize(budget)

View File

@@ -1,6 +1,6 @@
class Admin::Budgets::LinksComponent < ApplicationComponent
attr_reader :budget
delegate :can?, to: :helpers
use_helpers :can?
def initialize(budget)
@budget = budget

View File

@@ -1,5 +1,5 @@
class Admin::BudgetsWizard::BaseComponent < ApplicationComponent
delegate :single_heading?, :url_params, to: :helpers
use_helpers :single_heading?, :url_params
def budget_mode
helpers.budget_mode || "multiple"

View File

@@ -1,7 +1,7 @@
class Admin::BudgetsWizard::Budgets::NewComponent < ApplicationComponent
include Header
attr_reader :budget
delegate :single_heading?, to: :helpers
use_helpers :single_heading?
def initialize(budget)
@budget = budget

View File

@@ -1,6 +1,6 @@
class Admin::MenuComponent < ApplicationComponent
include LinkListHelper
delegate :can?, to: :helpers
use_helpers :can?
def links
[

View File

@@ -1,5 +1,5 @@
class Admin::Organizations::TableActionsComponent < ApplicationComponent
delegate :can?, to: :controller
use_helpers :can?
attr_reader :organization
def initialize(organization)

View File

@@ -1,6 +1,6 @@
class Admin::Poll::Questions::Answers::Documents::IndexComponent < ApplicationComponent
attr_reader :answer
delegate :can?, to: :helpers
use_helpers :can?
def initialize(answer)
@answer = answer

View File

@@ -1,6 +1,6 @@
class Admin::Poll::Questions::FilterComponent < ApplicationComponent
attr_reader :polls
delegate :current_path_with_query_params, to: :helpers
use_helpers :current_path_with_query_params
def initialize(polls)
@polls = polls

View File

@@ -2,7 +2,7 @@ class Admin::Poll::Questions::FormComponent < ApplicationComponent
include TranslatableFormHelper
include GlobalizeHelper
attr_reader :question, :url
delegate :can?, to: :helpers
use_helpers :can?
def initialize(question, url:)
@question = question

View File

@@ -1,6 +1,6 @@
class Admin::Settings::ContentTypesFormComponent < ApplicationComponent
attr_reader :setting, :tab
delegate :dom_id, to: :helpers
use_helpers :dom_id
def initialize(setting, tab: nil)
@setting = setting

View File

@@ -1,6 +1,6 @@
class Admin::Settings::RowComponent < ApplicationComponent
attr_reader :key, :tab, :type
delegate :dom_id, to: :helpers
use_helpers :dom_id
def initialize(key, type: :text, tab: nil)
@key = key

View File

@@ -1,6 +1,6 @@
class Admin::Settings::TextFormComponent < ApplicationComponent
attr_reader :setting, :tab
delegate :dom_id, to: :helpers
use_helpers :dom_id
def initialize(setting, tab: nil)
@setting = setting

View File

@@ -1,6 +1,6 @@
class Admin::SiteCustomization::InformationTexts::FormFieldComponent < ApplicationComponent
attr_reader :i18n_content, :locale
delegate :globalize, :site_customization_display_translation_style, to: :helpers
use_helpers :globalize, :site_customization_display_translation_style
def initialize(i18n_content, locale:)
@i18n_content = i18n_content

View File

@@ -1,7 +1,7 @@
class Admin::Tenants::NewComponent < ApplicationComponent
include Header
attr_reader :tenant
delegate :current_user, to: :helpers
use_helpers :current_user
def initialize(tenant)
@tenant = tenant

View File

@@ -1,5 +1,5 @@
class ApplicationComponent < ViewComponent::Base
include SettingsHelper
delegate :back_link_to, :t, to: :helpers
use_helpers :back_link_to, :t
delegate :default_form_builder, to: :controller
end

View File

@@ -1,6 +1,6 @@
class Attachable::FieldsComponent < ApplicationComponent
attr_reader :f, :resource_type, :resource_id, :relation_name
delegate :current_user, :render_image, to: :helpers
use_helpers :current_user, :render_image
def initialize(f, resource_type:, resource_id:, relation_name:)
@f = f

View File

@@ -1,6 +1,6 @@
class Budgets::BudgetComponent < ApplicationComponent
attr_reader :budget
delegate :attached_background_css, to: :helpers
use_helpers :attached_background_css
def initialize(budget)
@budget = budget

View File

@@ -1,6 +1,6 @@
class Budgets::Executions::ImageComponent < ApplicationComponent
attr_reader :investment
delegate :image_path_for, to: :helpers
use_helpers :image_path_for
def initialize(investment)
@investment = investment

View File

@@ -1,5 +1,5 @@
class Budgets::InvestmentComponent < ApplicationComponent
delegate :locale_and_user_status, :namespaced_budget_investment_path, :image_path_for, to: :helpers
use_helpers :locale_and_user_status, :namespaced_budget_investment_path, :image_path_for
attr_reader :investment
def initialize(investment)

View File

@@ -1,6 +1,6 @@
class Budgets::Investments::BallotComponent < ApplicationComponent
attr_reader :investment, :investment_ids, :ballot
delegate :current_user, :heading_link, :link_to_verify_account, to: :helpers
use_helpers :current_user, :heading_link, :link_to_verify_account
def initialize(investment:, investment_ids:, ballot:)
@investment = investment

View File

@@ -1,5 +1,5 @@
class Budgets::Investments::FiltersComponent < ApplicationComponent
delegate :valid_filters, :current_filter, :link_list, :current_path_with_query_params, to: :helpers
use_helpers :valid_filters, :current_filter, :link_list, :current_path_with_query_params
def render?
valid_filters&.any?

View File

@@ -2,7 +2,7 @@ class Budgets::Investments::FormComponent < ApplicationComponent
include TranslatableFormHelper
include GlobalizeHelper
attr_reader :investment, :url
delegate :current_user, :budget_heading_select_options, :suggest_data, to: :helpers
use_helpers :current_user, :budget_heading_select_options, :suggest_data
def initialize(investment, url:)
@investment = investment

View File

@@ -1,6 +1,6 @@
class Budgets::Investments::MapComponent < ApplicationComponent
attr_reader :heading, :investments
delegate :render_map, to: :helpers
use_helpers :render_map
def initialize(investments, heading:)
@investments = investments

View File

@@ -1,6 +1,6 @@
class Budgets::Investments::MyBallotComponent < ApplicationComponent
attr_reader :ballot, :heading, :investment_ids, :assigned_heading
delegate :can?, :heading_link, to: :helpers
use_helpers :can?, :heading_link
def initialize(ballot:, heading:, investment_ids:, assigned_heading: nil)
@ballot = ballot

View File

@@ -1,6 +1,6 @@
class Budgets::Investments::VotesComponent < ApplicationComponent
attr_reader :investment
delegate :namespace, :current_user, :image_absolute_url, :link_to_verify_account, to: :helpers
use_helpers :namespace, :current_user, :image_absolute_url, :link_to_verify_account
def initialize(investment)
@investment = investment

View File

@@ -1,5 +1,5 @@
class Budgets::MapComponent < ApplicationComponent
delegate :render_map, to: :helpers
use_helpers :render_map
attr_reader :budget
def initialize(budget)

View File

@@ -1,5 +1,5 @@
class Budgets::PhasesComponent < ApplicationComponent
delegate :wysiwyg, :auto_link_already_sanitized_html, to: :helpers
use_helpers :wysiwyg, :auto_link_already_sanitized_html
attr_reader :budget
def initialize(budget)

View File

@@ -1,5 +1,5 @@
class Budgets::SubheaderComponent < ApplicationComponent
delegate :current_user, :link_to_signin, :link_to_signup, :link_to_verify_account, :can?, to: :helpers
use_helpers :current_user, :link_to_signin, :link_to_signup, :link_to_verify_account, :can?
attr_reader :budget
def initialize(budget)

View File

@@ -1,5 +1,5 @@
class Budgets::SupportsInfoComponent < ApplicationComponent
delegate :current_user, to: :helpers
use_helpers :current_user
attr_reader :budget
def initialize(budget)

View File

@@ -2,7 +2,7 @@ class Debates::FormComponent < ApplicationComponent
include TranslatableFormHelper
include GlobalizeHelper
attr_reader :debate
delegate :suggest_data, to: :helpers
use_helpers :suggest_data
def initialize(debate)
@debate = debate

View File

@@ -1,7 +1,7 @@
class Debates::NewComponent < ApplicationComponent
include Header
attr_reader :debate
delegate :new_window_link_to, to: :helpers
use_helpers :new_window_link_to
def initialize(debate)
@debate = debate

View File

@@ -1,6 +1,6 @@
class Debates::VotesComponent < ApplicationComponent
attr_reader :debate
delegate :current_user, :link_to_verify_account, to: :helpers
use_helpers :current_user, :link_to_verify_account
def initialize(debate)
@debate = debate

View File

@@ -1,7 +1,7 @@
class Documents::DocumentComponent < ApplicationComponent
attr_reader :document, :show_destroy_link
alias_method :show_destroy_link?, :show_destroy_link
delegate :can?, to: :helpers
use_helpers :can?
def initialize(document, show_destroy_link: false)
@document = document

View File

@@ -1,6 +1,6 @@
class Layout::AdminHeaderComponent < ApplicationComponent
attr_reader :user
delegate :namespace, :namespaced_root_path, :show_admin_menu?, to: :helpers
use_helpers :namespace, :namespaced_root_path, :show_admin_menu?
def initialize(user)
@user = user

View File

@@ -1,6 +1,6 @@
class Layout::AdminLoginItemsComponent < ApplicationComponent
attr_reader :user
delegate :link_list, :show_admin_menu?, to: :helpers
use_helpers :link_list, :show_admin_menu?
def initialize(user)
@user = user

View File

@@ -1,5 +1,5 @@
class Layout::CommonHTMLAttributesComponent < ApplicationComponent
delegate :rtl?, to: :helpers
use_helpers :rtl?
private

View File

@@ -1,5 +1,5 @@
class Layout::FooterComponent < ApplicationComponent
delegate :content_block, to: :helpers
use_helpers :content_block
def footer_legal_content_block
content_block("footer_legal")

View File

@@ -1,5 +1,5 @@
class Layout::LocaleSwitcherComponent < ApplicationComponent
delegate :name_for_locale, :link_list, :current_path_with_query_params, :rtl?, to: :helpers
use_helpers :name_for_locale, :link_list, :current_path_with_query_params, :rtl?
def render?
locales.size > 1

View File

@@ -1,6 +1,6 @@
class Layout::LoginItemsComponent < ApplicationComponent
attr_reader :user
delegate :layout_menu_link_to, to: :helpers
use_helpers :layout_menu_link_to
def initialize(user)
@user = user

View File

@@ -1,5 +1,5 @@
class Layout::SocialComponent < ApplicationComponent
delegate :content_block, to: :helpers
use_helpers :content_block
def render?
sites.any? || footer_content_block.present?

View File

@@ -1,3 +1,3 @@
class Layout::SubnavigationComponent < ApplicationComponent
delegate :content_block, :layout_menu_link_to, to: :helpers
use_helpers :content_block, :layout_menu_link_to
end

View File

@@ -1,5 +1,5 @@
class Layout::TopLinksComponent < ApplicationComponent
delegate :content_block, to: :helpers
use_helpers :content_block
def render?
top_links_content_block.present?

View File

@@ -1,6 +1,6 @@
class Legislation::Proposals::VotesComponent < ApplicationComponent
attr_reader :proposal
delegate :current_user, :link_to_verify_account, :can?, to: :helpers
use_helpers :current_user, :link_to_verify_account, :can?
def initialize(proposal)
@proposal = proposal

View File

@@ -1,3 +1,3 @@
class MachineLearning::InfoComponent < ApplicationComponent
delegate :current_user, to: :helpers
use_helpers :current_user
end

View File

@@ -1,6 +1,6 @@
class Management::Budgets::PrintInvestments::TableComponent < ApplicationComponent
attr_reader :budgets
delegate :paginate, to: :helpers
use_helpers :paginate
def initialize(budgets)
@budgets = budgets

View File

@@ -1,6 +1,6 @@
class Polls::Questions::AnswersComponent < ApplicationComponent
attr_reader :question
delegate :can?, :current_user, :user_signed_in?, to: :helpers
use_helpers :can?, :current_user, :user_signed_in?
def initialize(question)
@question = question

View File

@@ -1,7 +1,7 @@
class Polls::Questions::ReadMoreComponent < ApplicationComponent
with_collection_parameter :question
attr_reader :question
delegate :wysiwyg, to: :helpers
use_helpers :wysiwyg
def initialize(question:)
@question = question

View File

@@ -2,7 +2,7 @@ class Proposals::FormComponent < ApplicationComponent
include TranslatableFormHelper
include GlobalizeHelper
attr_reader :proposal, :url
delegate :current_user, :suggest_data, :geozone_select_options, to: :helpers
use_helpers :current_user, :suggest_data, :geozone_select_options
def initialize(proposal, url:)
@proposal = proposal

View File

@@ -1,5 +1,5 @@
class Proposals::GeozonesComponent < ApplicationComponent
delegate :image_path_for, to: :helpers
use_helpers :image_path_for
def render?
Geozone.any?

View File

@@ -1,7 +1,7 @@
class Proposals::NewComponent < ApplicationComponent
include Header
attr_reader :proposal
delegate :new_window_link_to, to: :helpers
use_helpers :new_window_link_to
def initialize(proposal)
@proposal = proposal

View File

@@ -1,6 +1,6 @@
class Proposals::VotesComponent < ApplicationComponent
attr_reader :proposal
delegate :current_user, :link_to_verify_account, to: :helpers
use_helpers :current_user, :link_to_verify_account
def initialize(proposal, vote_url: nil)
@proposal = proposal

View File

@@ -1,6 +1,6 @@
class Relationable::RelatedListComponent < ApplicationComponent
attr_reader :relationable
delegate :current_user, to: :helpers
use_helpers :current_user
def initialize(relationable)
@relationable = relationable

View File

@@ -1,6 +1,6 @@
class SDG::FilterLinksComponent < ApplicationComponent
attr_reader :records, :related_model, :see_more_link
delegate :link_list, to: :helpers
use_helpers :link_list
def initialize(records, related_model, see_more_link: nil)
@records = records

View File

@@ -1,6 +1,6 @@
class SDG::Goals::IndexComponent < ApplicationComponent
attr_reader :goals, :header, :phases
delegate :link_list, to: :helpers
use_helpers :link_list
def initialize(goals, header:, phases:)
@goals = goals

View File

@@ -1,6 +1,6 @@
class SDG::RelatedListSelectorComponent < ApplicationComponent
attr_reader :f
delegate :new_window_link_to, to: :helpers
use_helpers :new_window_link_to
def initialize(form)
@f = form

View File

@@ -1,6 +1,6 @@
class Shared::AgreeWithTermsOfServiceFieldComponent < ApplicationComponent
attr_reader :form
delegate :new_window_link_to, to: :helpers
use_helpers :new_window_link_to
def initialize(form)
@form = form

View File

@@ -1,6 +1,6 @@
class Shared::AvatarComponent < ApplicationComponent
attr_reader :record, :given_options
delegate :avatar_image, to: :helpers
use_helpers :avatar_image
def initialize(record, **given_options)
@record = record

View File

@@ -1,6 +1,6 @@
class Shared::CommentsComponent < ApplicationComponent
attr_reader :record, :comment_tree
delegate :current_user, :current_order, :locale_and_user_status, :commentable_cache_key, to: :helpers
use_helpers :current_user, :current_order, :locale_and_user_status, :commentable_cache_key
def initialize(record, comment_tree)
@record = record

View File

@@ -1,6 +1,6 @@
class Shared::GeozoneLinkComponent < ApplicationComponent
attr_reader :geozonable, :link
delegate :geozone_name, to: :helpers
use_helpers :geozone_name
def initialize(geozonable, link)
@geozonable = geozonable

View File

@@ -1,6 +1,6 @@
class Shared::InFavorAgainstComponent < ApplicationComponent
attr_reader :votable
delegate :votes_percentage, to: :helpers
use_helpers :votes_percentage
def initialize(votable)
@votable = votable

View File

@@ -1,6 +1,6 @@
class Shared::ModerationActionsComponent < ApplicationComponent
attr_reader :record
delegate :can?, to: :helpers
use_helpers :can?
def initialize(record)
@record = record

View File

@@ -1,6 +1,6 @@
class Shared::OrderLinksComponent < ApplicationComponent
attr_reader :i18n_namespace, :anchor
delegate :current_path_with_query_params, :current_order, :valid_orders, to: :helpers
use_helpers :current_path_with_query_params, :current_order, :valid_orders
def initialize(i18n_namespace, anchor: nil)
@i18n_namespace = i18n_namespace

View File

@@ -1,6 +1,6 @@
class Shared::ParticipationNotAllowedComponent < ApplicationComponent
attr_reader :votable, :cannot_vote_text
delegate :current_user, :link_to_signin, :link_to_signup, to: :helpers
use_helpers :current_user, :link_to_signin, :link_to_signup
def initialize(votable, cannot_vote_text:)
@votable = votable

View File

@@ -1,6 +1,6 @@
class Shared::TagListComponent < ApplicationComponent
attr_reader :taggable, :limit
delegate :link_list, to: :helpers
use_helpers :link_list
def initialize(taggable, limit:)
@taggable = taggable

View File

@@ -1,6 +1,6 @@
class Shared::VoteButtonComponent < ApplicationComponent
attr_reader :votable, :value, :options
delegate :current_user, :can?, to: :helpers
use_helpers :current_user, :can?
def initialize(votable, value:, **options)
@votable = votable

View File

@@ -1,6 +1,6 @@
class Users::PublicActivityComponent < ApplicationComponent
attr_reader :user
delegate :current_user, :valid_interests_access?, :current_path_with_query_params, to: :helpers
use_helpers :current_user, :valid_interests_access?, :current_path_with_query_params
def initialize(user)
@user = user

View File

@@ -2,7 +2,7 @@ class Valuation::Budgets::RowComponent < ApplicationComponent
attr_reader :budget
with_collection_parameter :budget
delegate :current_user, to: :helpers
use_helpers :current_user
def initialize(budget:)
@budget = budget

View File

@@ -1,5 +1,5 @@
class Widget::Feeds::ProcessComponent < ApplicationComponent
delegate :image_path_for, to: :helpers
use_helpers :image_path_for
attr_reader :process
def initialize(process)