Skip to content

Commit

Permalink
add course compensation category kind validation
Browse files Browse the repository at this point in the history
  • Loading branch information
openscript committed Jul 24, 2024
1 parent 9449614 commit 0b21bd9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/controllers/sac_cas/event/kinds_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ def load_assocations
@cost_centers = CostCenter.list
@cost_units = CostUnit.list
@kind_categories = Event::KindCategory.list
@course_compensation_categories = CourseCompensationCategory.where(kind: [:day, :flat])
@course_compensation_categories = CourseCompensationCategory.where(kind: Event::Kind::ALLOWED_KINDS)
end
end
8 changes: 8 additions & 0 deletions app/models/sac_cas/event/kind.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ module SacCas::Event::Kind
validates :short_name, presence: true

validate :maximum_age_greater_than_minimum_age, if: -> { maximum_age.present? }
validate :kinds_must_be_allowed

SEASONS = %w[winter summer].freeze
ACCOMMODATIONS = %w[no_overnight hut pension pension_or_hut bivouac].freeze
ALLOWED_KINDS = %w[day flat].freeze

i18n_enum :season, SEASONS
i18n_enum :accommodation, ACCOMMODATIONS
Expand Down Expand Up @@ -96,4 +98,10 @@ def maximum_age_greater_than_minimum_age
errors.add(:maximum_age, :greater_than_or_equal_to, count: minimum_age || 0)
end
end

def kinds_must_be_allowed
if course_compensation_categories.any? { |ccc| !ALLOWED_KINDS.include?(ccc.kind) }
errors.add(:course_compensation_category_ids, :invalid)
end
end
end
8 changes: 8 additions & 0 deletions spec/models/event/kind_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
describe "::validations" do
subject(:kind) { Fabricate.build(:sac_event_kind) }

let!(:course_compensation_category_budget) { Fabricate(:course_compensation_category, kind: "budget") }

it "is valid as builded by fabricator" do
expect(kind).to be_valid
expect(kind.level).to eq event_levels(:ek)
Expand Down Expand Up @@ -78,6 +80,12 @@
expect(kind).not_to be_valid
expect(kind.errors[:maximum_age]).to eq ["muss grösser oder gleich 2 sein"]
end

it "validates that course compensation categories kinds are either flat or day" do
kind.course_compensation_category_ids = [course_compensation_category_budget.id]
expect(kind).not_to be_valid
expect(kind.errors[:course_compensation_category_ids]).to eq ["ist nicht gültig"]
end
end

describe "#push_down_inherited_attributes!" do
Expand Down

0 comments on commit 0b21bd9

Please sign in to comment.