From 427669466e3a175069ca1b34e0b1c469433756db Mon Sep 17 00:00:00 2001 From: Matt Levy Date: Wed, 24 Jul 2024 17:40:16 -0700 Subject: [PATCH] cleanup rubocop is now not murderbot --- app/models/addon.rb | 4 ++-- app/models/event.rb | 4 ++++ app/models/event_addon.rb | 7 +++---- app/views/events/_form.html.haml | 7 +++---- spec/controllers/events_controller_spec.rb | 1 + spec/models/addon_spec.rb | 3 ++- spec/models/event_addon_spec.rb | 4 ++-- spec/models/event_spec.rb | 14 ++++++++------ spec/models/ticket_request_event_addon_spec.rb | 4 ++-- 9 files changed, 27 insertions(+), 21 deletions(-) diff --git a/app/models/addon.rb b/app/models/addon.rb index de3fb5cd..8f28c824 100644 --- a/app/models/addon.rb +++ b/app/models/addon.rb @@ -12,8 +12,8 @@ # updated_at :datetime not null # class Addon < ApplicationRecord - has_many :event_addons, autosave: true - has_many :ticket_request_event_addon, autosave: true + has_many :event_addons, autosave: true, dependent: :destroy + has_many :ticket_request_event_addon, autosave: true, dependent: :destroy CATEGORIES = [CATEGORY_PASS = 'pass', CATEGORY_CAMP = 'camp'].freeze diff --git a/app/models/event.rb b/app/models/event.rb index fc277ec7..ee46e6ee 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -244,6 +244,10 @@ def build_default_event_addons event_addons end + def sorted_event_addons + event_addons.sort_by { |e| [e.category, e.name] } + end + private def generate_slug! diff --git a/app/models/event_addon.rb b/app/models/event_addon.rb index c3370b7c..333f8e4e 100644 --- a/app/models/event_addon.rb +++ b/app/models/event_addon.rb @@ -30,11 +30,10 @@ class EventAddon < ApplicationRecord validates :price, presence: true, numericality: { greater_than_or_equal_to: 0 } + delegate :name, to: :addon + delegate :category, to: :addon + def set_default_values self.price ||= addon.default_price end - - delegate :name, to: :addon - - delegate :category, to: :addon end diff --git a/app/views/events/_form.html.haml b/app/views/events/_form.html.haml index 3f8261cb..7241ef57 100644 --- a/app/views/events/_form.html.haml +++ b/app/views/events/_form.html.haml @@ -95,12 +95,11 @@ %p.muted Leave blank for no limit = f.number_field :max_kid_tickets_per_request, class: 'input-mini', min: 1 - %legend Event Addons + %legend Event Addons and Pricing %p.muted - Set price > 0 to include the addon in the event. - Setting the price to 0 removes the item from being sold. + To remove an addon from being sold at an event, set the price to 0. - - @event.event_addons.each do |event_addon| + - @event.sorted_event_addons.each do |event_addon| = f.fields_for :event_addons, event_addon do |event_addon_form| = event_addon_form.hidden_field :addon_id, value: event_addon.addon_id = event_addon_form.label :name, event_addon.name diff --git a/spec/controllers/events_controller_spec.rb b/spec/controllers/events_controller_spec.rb index 2a948517..5f50d93b 100644 --- a/spec/controllers/events_controller_spec.rb +++ b/spec/controllers/events_controller_spec.rb @@ -71,6 +71,7 @@ before do allow_any_instance_of(described_class).to receive(:params).and_return(ActionController::Parameters.new(event: new_event_params)) end + # rubocop: enable RSpec/AnyInstance it 'does not have a nil start_time' do expect(new_event.start_time).not_to be_nil diff --git a/spec/models/addon_spec.rb b/spec/models/addon_spec.rb index 051aaa04..78d9f788 100644 --- a/spec/models/addon_spec.rb +++ b/spec/models/addon_spec.rb @@ -58,7 +58,8 @@ subject(:addons) { described_class.find_all_by_category(category) } let(:category) { Addon::CATEGORY_PASS } - let!(:addon) { create(:addon, category:) } + + before { create(:addon, category:) } context 'when category is known' do it { is_expected.to be_a(Array) } diff --git a/spec/models/event_addon_spec.rb b/spec/models/event_addon_spec.rb index 5075e25d..7ed23466 100644 --- a/spec/models/event_addon_spec.rb +++ b/spec/models/event_addon_spec.rb @@ -39,11 +39,11 @@ end end - context 'sets price' do + context 'when price is set' do let(:price) { 314 } let(:event_addon) { build(:event_addon, price:) } - it 'has price set not default price' do + it 'has price set' do expect(event_addon.price).to eq(price) end end diff --git a/spec/models/event_spec.rb b/spec/models/event_spec.rb index 05d235c8..e7916f43 100644 --- a/spec/models/event_spec.rb +++ b/spec/models/event_spec.rb @@ -374,9 +374,10 @@ describe '#build_event_addons_from_params' do let(:price) { 314 } - let!(:addon) { create(:addon) } context 'event addon is built for event' do + let!(:addon) { create(:addon) } + before do event_addons_attributes = { '0'=>{ 'addon_id' => addon.id.to_s, 'price' => price.to_s } } event.build_event_addons_from_params(event_addons_attributes) @@ -396,7 +397,10 @@ end context 'event addon is not built for event' do - let(:event_addons_attributes) { { '0'=>{ 'addon_id' => 999, 'price' => price } } } + before do + event_addons_attributes = { '0'=>{ 'addon_id' => 999, 'price' => price.to_s } } + event.build_event_addons_from_params(event_addons_attributes) + end it 'has an unknown addon id' do expect(event.event_addons.size).to eq(0) @@ -412,9 +416,8 @@ end context 'event has event addons' do - let!(:addon) { create(:addon) } - before do + create(:addon) event.create_default_event_addons end @@ -445,9 +448,8 @@ end context 'event has event addons' do - let!(:addon) { create(:addon) } - before do + create(:addon) event.build_default_event_addons end diff --git a/spec/models/ticket_request_event_addon_spec.rb b/spec/models/ticket_request_event_addon_spec.rb index c5b32449..e2837ef2 100644 --- a/spec/models/ticket_request_event_addon_spec.rb +++ b/spec/models/ticket_request_event_addon_spec.rb @@ -30,13 +30,13 @@ let(:ticket_request_event_addon) { build(:ticket_request_event_addon) } describe '#quantity' do - context 'valid quantity' do + context 'when valid quantity' do let(:ticket_request_event_addon) { build(:ticket_request_event_addon, quantity: 2) } it { is_expected.to be_valid } end - context 'invalid quantity' do + context 'when invalid quantity' do let(:ticket_request_event_addon) { build(:ticket_request_event_addon, quantity: -1) } it { is_expected.not_to be_valid }