From 941cebf47512e519918027b4a8a830d5a9a7bc43 Mon Sep 17 00:00:00 2001 From: Nicolas Brousse Date: Thu, 17 Mar 2022 21:42:35 +0100 Subject: [PATCH] fix: initialize empty WeekdaySelectComponent if Rails < 7 (#115) --- CHANGELOG.md | 1 + .../form/weekday_select_component.rb | 34 ++++++++++--------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d4a9a3..9212218 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Fixed - Fix broken gem initialization due to missing ViewComponent::Form constant (#114) +- Initialize empty WeekdaySelectComponent if Rails < 7 to fix Zeitwerk error (#115) ## [0.2.0] - 2022-03-02 ### Added diff --git a/app/components/view_component/form/weekday_select_component.rb b/app/components/view_component/form/weekday_select_component.rb index 605bb26..052377e 100644 --- a/app/components/view_component/form/weekday_select_component.rb +++ b/app/components/view_component/form/weekday_select_component.rb @@ -1,20 +1,20 @@ # frozen_string_literal: true -if Rails::VERSION::MAJOR >= 7 - module ViewComponent - module Form - class WeekdaySelectComponent < FieldComponent - attr_reader :html_options +module ViewComponent + module Form + class WeekdaySelectComponent < FieldComponent + attr_reader :html_options - def initialize(form, object_name, method_name, options = {}, html_options = {}) - @html_options = html_options + def initialize(form, object_name, method_name, options = {}, html_options = {}) + @html_options = html_options - super(form, object_name, method_name, options) + super(form, object_name, method_name, options) - set_html_options! - end + set_html_options! + end - def call + def call # rubocop:disable Metrics/MethodLength + if Rails::VERSION::MAJOR >= 7 # rubocop:disable Style/GuardClause ActionView::Helpers::Tags::WeekdaySelect.new( object_name, method_name, @@ -22,14 +22,16 @@ def call options, html_options ).render + else + raise NotImplementedError, "#{self.class} is only available in Rails >= 7" end + end - protected + protected - def set_html_options! - @html_options[:class] = class_names(html_options[:class], html_class) - @html_options.delete(:class) if @html_options[:class].blank? - end + def set_html_options! + @html_options[:class] = class_names(html_options[:class], html_class) + @html_options.delete(:class) if @html_options[:class].blank? end end end