diff --git a/app/assets/stylesheets/admin/budget_phases/phases.scss b/app/assets/stylesheets/admin/budget_phases/phases.scss index d627cf055..7d84f5231 100644 --- a/app/assets/stylesheets/admin/budget_phases/phases.scss +++ b/app/assets/stylesheets/admin/budget_phases/phases.scss @@ -3,9 +3,4 @@ caption { @include element-invisible; } - - [aria-pressed] { - @include switch; - margin-bottom: 0; - } } diff --git a/app/assets/stylesheets/admin/toggle_switch.scss b/app/assets/stylesheets/admin/toggle_switch.scss new file mode 100644 index 000000000..501e85eaa --- /dev/null +++ b/app/assets/stylesheets/admin/toggle_switch.scss @@ -0,0 +1,6 @@ +.admin .toggle-switch { + [aria-pressed] { + @include switch; + margin-bottom: 0; + } +} diff --git a/app/components/admin/budget_phases/toggle_enabled_component.html.erb b/app/components/admin/budget_phases/toggle_enabled_component.html.erb index d84202d46..832049577 100644 --- a/app/components/admin/budget_phases/toggle_enabled_component.html.erb +++ b/app/components/admin/budget_phases/toggle_enabled_component.html.erb @@ -1 +1 @@ -<%= render Admin::ActionComponent.new(action, phase, options) %> +<%= render Admin::ToggleSwitchComponent.new(action, phase, pressed: enabled?, **options) %> diff --git a/app/components/admin/budget_phases/toggle_enabled_component.rb b/app/components/admin/budget_phases/toggle_enabled_component.rb index 5c270050c..daf2693b1 100644 --- a/app/components/admin/budget_phases/toggle_enabled_component.rb +++ b/app/components/admin/budget_phases/toggle_enabled_component.rb @@ -9,14 +9,7 @@ class Admin::BudgetPhases::ToggleEnabledComponent < ApplicationComponent private def options - { - text: text, - method: :patch, - remote: true, - "aria-label": t("admin.budgets.edit.enable_phase", phase: phase.name), - "aria-pressed": enabled?, - form_class: "toggle-switch" - } + { "aria-label": t("admin.budgets.edit.enable_phase", phase: phase.name) } end def action @@ -26,12 +19,4 @@ class Admin::BudgetPhases::ToggleEnabledComponent < ApplicationComponent :enable end end - - def text - if enabled? - t("shared.yes") - else - t("shared.no") - end - end end diff --git a/app/components/admin/toggle_switch_component.html.erb b/app/components/admin/toggle_switch_component.html.erb new file mode 100644 index 000000000..35b566c48 --- /dev/null +++ b/app/components/admin/toggle_switch_component.html.erb @@ -0,0 +1 @@ +<%= render Admin::ActionComponent.new(action, record, **default_options.merge(options)) %> diff --git a/app/components/admin/toggle_switch_component.rb b/app/components/admin/toggle_switch_component.rb new file mode 100644 index 000000000..ceee74d5b --- /dev/null +++ b/app/components/admin/toggle_switch_component.rb @@ -0,0 +1,31 @@ +class Admin::ToggleSwitchComponent < ApplicationComponent + attr_reader :action, :record, :pressed, :options + alias_method :pressed?, :pressed + + def initialize(action, record, pressed:, **options) + @action = action + @record = record + @pressed = pressed + @options = options + end + + private + + def text + if pressed? + t("shared.yes") + else + t("shared.no") + end + end + + def default_options + { + text: text, + method: :patch, + remote: true, + "aria-pressed": pressed?, + form_class: "toggle-switch" + } + end +end