Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
rubocop is now not murderbot
  • Loading branch information
beingmattlevy committed Jul 25, 2024
1 parent f64a66e commit 4276694
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 21 deletions.
4 changes: 2 additions & 2 deletions app/models/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
7 changes: 3 additions & 4 deletions app/models/event_addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 3 additions & 4 deletions app/views/events/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/controllers/events_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion spec/models/addon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
4 changes: 2 additions & 2 deletions spec/models/event_addon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 8 additions & 6 deletions spec/models/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions spec/models/ticket_request_event_addon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down

0 comments on commit 4276694

Please sign in to comment.