Skip to content

Commit

Permalink
Refactor sac membership config forms
Browse files Browse the repository at this point in the history
  • Loading branch information
codez committed May 24, 2024
1 parent 6087bcb commit 926cbf5
Show file tree
Hide file tree
Showing 29 changed files with 333 additions and 681 deletions.

This file was deleted.

This file was deleted.

33 changes: 0 additions & 33 deletions app/components/sac_membership_configs/field_rows_component.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%ul.pagination.pagination-sm
- @available_configs.each do |config|
%li.page-item{ class: [active?(config) && 'active'] }
%li.page-item{ class: item_class(config) }
= link_to_edit_config(config)
%li.page-item{ class: [entry.new_record? && 'active'] }
%li.page-item{ class: new_item_class }
= link_new_config
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,34 @@ class ValidFromSelectorComponent < ApplicationComponent

delegate :entry, to: :helpers

def initialize(available_configs:)
def initialize(group, available_configs)
@group = group
@available_configs = available_configs
end

private

def item_class(config)
'active' if active?(config)
end

def new_item_class
'active' if entry.new_record?
end

def active?(config)
config.id == params[:id].to_i
end

def link_to_edit_config(config)
link_to(config.valid_from,
edit_group_sac_membership_config_path(group_id: Group.root.id, id: config.id),
polymorphic_path([@group, config], action: :edit),
class: 'page-link')
end

def link_new_config
link_to(t('sac_membership_configs.global.link.add'),
new_group_sac_membership_config_path(group_id: Group.root.id),
link_to(t('sac_section_membership_configs.global.link.add'),
polymorphic_path([@group, entry.class], action: :new),
class: 'page-link')
end

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

73 changes: 73 additions & 0 deletions app/controllers/concerns/membership_configurable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# frozen_string_literal: true

# Copyright (c) 2024, Schweizer Alpen-Club. This file is part of
# hitobito_sac_cas and licensed under the Affero General Public License version 3
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_sac_cas

module MembershipConfigurable
extend ActiveSupport::Concern

included do
helper_method :cancel_url, :available_configs, :group
before_action :assert_group_type
end

def index
redirect_to latest_config_entry_path
end

def show
redirect_to edit_path(find_entry)
end

private

def build_entry
if latest_config
attrs = latest_config.attributes
attrs.delete('id')
attrs[:valid_from] = latest_config.valid_from + 1
else
attrs = new_entry_values
end
model_class.new(attrs)
end

def latest_config_entry_path
if latest_config
edit_path(latest_config)
else
new_path
end
end

def find_entry
group_configs.find(params[:id])
end

def edit_path(config)
polymorphic_path([group, config], action: :edit)
end

def new_path
polymorphic_path([group, model_class], action: :new)
end

def cancel_url
group_path(id: group.id)
end

def available_configs
@available_configs ||= group_configs.list.to_a
end

def group_configs
model_class.all
end

def latest_config
available_configs.last
end

end

0 comments on commit 926cbf5

Please sign in to comment.