Skip to content

Commit

Permalink
spec and rubocop fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
beingmattlevy committed Jul 24, 2024
1 parent e1ca747 commit f64a66e
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 32 deletions.
18 changes: 10 additions & 8 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@ def edit
end

def create
create_params = params_symbolized_hash[:event].dup
create_params = params_symbolized_hash[:event].dup.with_indifferent_access
Rails.logger.info("event_create: create_params: #{create_params}")
TimeHelper.normalize_time_attributes(create_params)

@event = Event.new(create_params)
@event.build_event_addons_from_params(create_params[:event_addons_attributes])
if create_params[:event_addons_attributes].present?
@event.build_event_addons_from_params(create_params[:event_addons_attributes])
end

Rails.logger.info("event_create: created event: #{@event.id} event_addons: #{@event.event_addons.inspect}")

if @event.save
Expand Down Expand Up @@ -126,10 +129,10 @@ def params_symbolized_hash

def set_event
@event = Event.where(id: permitted_params[:id].to_i).first
if @event.event_addons.blank?
@event.create_default_event_addons
Rails.logger.info("event_set: event: #{@event.id} addons.size: #{@event.event_addons.size} event_addons: #{@event.event_addons.inspect}")
end
return if @event.event_addons.present?

@event.create_default_event_addons
Rails.logger.info("event_set: event: #{@event.id} addons.size: #{@event.event_addons.size} event_addons: #{@event.event_addons.inspect}")
end

def permitted_params
Expand All @@ -140,7 +143,6 @@ def permitted_params
:max_adult_tickets_per_request, :max_kid_tickets_per_request,
:allow_donations, :allow_financial_assistance,
:require_mailing_address, :require_role, :tickets_require_approval,
event_addons_attributes: [:id, :addon_id, :price]]
).to_hash.with_indifferent_access
{ event_addons_attributes: %i[id addon_id price] }]).to_hash.with_indifferent_access
end
end
1 change: 0 additions & 1 deletion app/models/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,4 @@ def self.find_all_by_category(category)

addons
end

end
10 changes: 6 additions & 4 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,13 @@ def to_param
end

def build_event_addons_from_params(build_params)
return if build_params.blank?

Rails.logger.debug { "build_event_addons_from_params: #{build_params}" }

build_params.each do |key, value|
if Addon.exists?(id: value)
self.event_addons.build({event_id: id, addon_id: value["addon_id"], price: value["price"]})
build_params.each_value do |value|
if Addon.exists?(id: value['addon_id'])
event_addons.build({ event_id: id, addon_id: value['addon_id'], price: value['price'] })
end
end

Expand All @@ -235,7 +237,7 @@ def build_default_event_addons
return if event_addons.present?

Addon.order_by_category.each do |addon|
self.event_addons.build({event: self, addon: addon}).set_default_values
event_addons.build({ event: self, addon: }).set_default_values
end

Rails.logger.debug { "build_default_event_addons: #{event_addons.inspect}" }
Expand Down
8 changes: 2 additions & 6 deletions app/models/event_addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ def set_default_values
self.price ||= addon.default_price
end

def name
addon.name
end
delegate :name, to: :addon

def category
addon.category
end
delegate :category, to: :addon
end
3 changes: 1 addition & 2 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def create_events
end

def create_addons
return if Addon.count > 0
return if Addon.count.positive?

Addon.create category: Addon::CATEGORY_PASS, name: 'Early Arrival', default_price: 20
Addon.create category: Addon::CATEGORY_PASS, name: 'Late Departure', default_price: 30
Expand Down Expand Up @@ -194,7 +194,6 @@ def print_event(event)
def header(string)
puts "\n#{format(" %-#{HEADER_WIDTH}.#{HEADER_WIDTH}s".colorize(background: :light_blue), string)}"
end

end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/models/event_addon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

context 'sets price' do
let(:price) { 314 }
let(:event_addon) { build(:event_addon, price: price) }
let(:event_addon) { build(:event_addon, price:) }

it 'has price set not default price' do
expect(event_addon.price).to eq(price)
Expand Down
19 changes: 9 additions & 10 deletions spec/models/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -375,13 +375,13 @@
describe '#build_event_addons_from_params' do
let(:price) { 314 }
let!(:addon) { create(:addon) }
let(:event_addons_attributes) { {"0"=>{"addon_id"=>addon.id, "price"=>price} }}

before do
event.build_event_addons_from_params(event_addons_attributes)
end
context 'event addon is built for event' do
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)
end

context "event addon is built for event" do
it 'has 1 event addon' do
expect(event.event_addons.size).to eq(1)
end
Expand All @@ -395,8 +395,8 @@
end
end

context "event addon is not built for event" do
let(:event_addons_attributes) { {"0"=>{"addon_id"=>999, "price"=>price} }}
context 'event addon is not built for event' do
let(:event_addons_attributes) { { '0'=>{ 'addon_id' => 999, 'price' => price } } }

it 'has an unknown addon id' do
expect(event.event_addons.size).to eq(0)
Expand Down Expand Up @@ -434,7 +434,6 @@
event_addon = event.event_addons.first
expect(event_addon&.price).to eq(event_addon&.addon&.default_price)
end

end
end

Expand All @@ -448,9 +447,9 @@
context 'event has event addons' do
let!(:addon) { create(:addon) }

before {
before do
event.build_default_event_addons
}
end

it 'event has event addons for each Addon' do
expect(event.event_addons.size).to eq(Addon.count)
Expand Down

0 comments on commit f64a66e

Please sign in to comment.