Skip to content

Commit

Permalink
Use dropdown for course states, remove stepper term, add state change
Browse files Browse the repository at this point in the history
validations for course model
  • Loading branch information
mtnstar committed May 23, 2024
1 parent f236dfd commit c9217a9
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,20 @@
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_sac_cas

class Events::Courses::StateSteppingController < ApplicationController

@@helper = Object.new
.extend(ActionView::Helpers::TranslationHelper)
.extend(ActionView::Helpers::OutputSafetyHelper)
class Events::Courses::StateController < ApplicationController

def update
authorize!(:update, entry)

save_next_step if step_possible?
save_next_state if state_possible?

redirect_to group_event_path
end

def save_next_step
entry.state = next_step
private

def save_next_state
entry.state = next_state

if entry.save
set_success_notice
Expand All @@ -30,7 +28,7 @@ def save_next_step
end

def set_success_notice
flash.now[:notice] = t('events/courses/state_stepping.flash.success',
flash.now[:notice] = t('events/courses/state.flash.success',
state: entry.decorate.state_translated)
end

Expand All @@ -39,19 +37,15 @@ def set_failure_notice
end

def error_messages
@@helper.safe_join(entry.errors.full_messages, '<br/>'.html_safe)
helpers.safe_join(entry.errors.full_messages, '<br/>'.html_safe)
end

def next_step
def next_state
params[:state]
end

def step_possible?
stepper.step_possible?(next_step)
end

def stepper
@stepper ||= Events::Courses::StateStepper.new(entry)
def state_possible?
entry.state_possible?(next_state)
end

def entry
Expand Down
7 changes: 0 additions & 7 deletions app/controllers/sac_cas/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module SacCas::EventsController

prepended do
before_render_form :preload_translated_associations
before_render_show :load_state_stepper
end

private
Expand All @@ -21,10 +20,4 @@ def preload_translated_associations
@cost_centers = CostCenter.includes(:translations).list
@cost_units = CostUnit.includes(:translations).list
end

def load_state_stepper
return unless entry.type == 'Event::Course'

@state_stepper = Events::Courses::StateStepper.new(entry)
end
end
38 changes: 0 additions & 38 deletions app/domain/events/courses/state_stepper.rb

This file was deleted.

35 changes: 35 additions & 0 deletions app/helpers/dropdown/events/courses/state.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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 Dropdown::Events::Courses
class State < ::Dropdown::Base

attr_reader :course, :template

delegate :t, to: :template

def initialize(template, course)
@course = course
@template = template
super(template, current_state_label, :'exchange-alt')
init_items
end

private

def current_state_label
t("activerecord.attributes.event/course.states.#{course.state}")
end

def init_items
course.available_states.each do |step|
link = template.state_group_event_path(template.params[:group_id], course, { state: step })
add_item(template.t(".state_buttons.#{step}"), link, method: :put)
end
end
end
end
50 changes: 50 additions & 0 deletions app/models/concerns/events/courses/state.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# 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 Events::Courses::State
extend ActiveSupport::Concern

included do
# key: current state
# value: possible next state
SAC_COURSE_STATES =
{ created: [:application_open],
application_open: [:application_paused, :created, :canceled],
application_paused: [:application_open],
application_closed: [:assignment_closed, :canceled],
assignment_closed: [:ready, :canceled],
ready: [:closed, :canceled],
canceled: [:application_open],
closed: [:ready]
}.freeze

self.possible_states = SAC_COURSE_STATES.keys.collect(&:to_s)

validate :assert_valid_state_change, if: :state_changed?
before_create :set_default_state
end

def available_states(state = self.state)
SAC_COURSE_STATES[state.to_sym]
end

def state_possible?(new_state)
available_states.any?(new_state.to_sym)
end

private

def assert_valid_state_change
unless available_states(state_was).include?(state.to_sym)
errors.add(:state, "State cannot be changed from #{state_was} to #{state}")
end
end

def set_default_state
self.state = :created
end
end
4 changes: 1 addition & 3 deletions app/models/sac_cas/event/course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ module SacCas::Event::Course

prepended do # rubocop:disable Metrics/BlockLength
include I18nEnums
include Events::Courses::State

translates :brief_description, :specialities, :similar_tours, :program

Expand Down Expand Up @@ -132,9 +133,6 @@ module SacCas::Event::Course
:hidden_contact_attrs
]

self.possible_states = %w(created application_open application_paused application_closed
assignment_closed ready closed canceled)

self.possible_participation_states = %w(unconfirmed applied rejected assigned summoned
attended absent canceled annulled)

Expand Down
7 changes: 1 addition & 6 deletions app/views/events/_actions_show_sac_cas.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,4 @@
- # https://github.com/hitobito/hitobito_sac_cas

- if can?(:update, entry)
- @state_stepper&.available_steps&.each do |state|
= action_button(t(".state_buttons.#{state}"),
step_state_group_event_path(@group, @event, { state: state }),
nil,
method: :put)

= Dropdown::Events::Courses::State.new(self, entry)
2 changes: 1 addition & 1 deletion config/locales/wagon.de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ de:
success_multiple:
"%{model} wurde erfolgreich für %{names} erstellt."

events/courses/state_stepping:
events/courses/state:
flash:
success: Status wurde auf %{state} gesetzt.

Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

resources :events, only: [] do
member do
put 'step_state' => 'events/courses/state_stepping#update'
put 'state' => 'events/courses/state#update'
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

require 'spec_helper'

describe Events::Courses::StateSteppingController do
describe Events::Courses::StateController do

let(:admin) { people(:admin) }
let(:mitglied) { people(:mitglied) }
Expand All @@ -28,7 +28,7 @@
context 'as admin' do
before { sign_in(admin) }

it 'updates state if step is possible' do
it 'updates state if state change is possible' do
put :update, params: { group_id: group.id, id: course.id, state: 'application_open' }

course.reload
Expand All @@ -39,7 +39,7 @@
expect(response).to redirect_to(group_event_path(group, course))
end

it 'does not update state if step is impossible' do
it 'does not update state if new state is not available' do
expect(course).to_not receive(:state=)

put :update, params: { group_id: group.id, id: course.id, state: 'ready' }
Expand Down
15 changes: 0 additions & 15 deletions spec/domain/events/courses/state_stepper_spec.rb

This file was deleted.

6 changes: 5 additions & 1 deletion spec/fabricators/event_fabricator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
end

Fabricator(:sac_open_course, from: :sac_course) do
state { :application_open }
contact_id { ActiveRecord::FixtureSet.identify(:admin) }
cost_center_id { ActiveRecord::FixtureSet.identify(:tour) }
cost_unit_id { ActiveRecord::FixtureSet.identify(:ski) }
Expand All @@ -26,6 +25,11 @@
before_create do |event|
event.dates.build(start_at: 1.week.from_now) if event.dates.empty?
end
after_create do |event|
# default state for new courses must be created and
# can be changed from created to application_open later
event.update!(state: :application_open)
end
end

Fabricator(:sac_event_kind, from: :event_kind) do
Expand Down
Loading

0 comments on commit c9217a9

Please sign in to comment.