diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index 4e447f01..5fa8f726 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -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 @@ -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 @@ -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 diff --git a/app/models/addon.rb b/app/models/addon.rb index 0b5663ab..de3fb5cd 100644 --- a/app/models/addon.rb +++ b/app/models/addon.rb @@ -39,5 +39,4 @@ def self.find_all_by_category(category) addons end - end diff --git a/app/models/event.rb b/app/models/event.rb index 3346e8fb..fc277ec7 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -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 @@ -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}" } diff --git a/app/models/event_addon.rb b/app/models/event_addon.rb index df4ae42d..c3370b7c 100644 --- a/app/models/event_addon.rb +++ b/app/models/event_addon.rb @@ -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 diff --git a/db/seeds.rb b/db/seeds.rb index b3108c49..67b88340 100755 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -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 @@ -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 diff --git a/spec/models/event_addon_spec.rb b/spec/models/event_addon_spec.rb index 37a2618e..5075e25d 100644 --- a/spec/models/event_addon_spec.rb +++ b/spec/models/event_addon_spec.rb @@ -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) diff --git a/spec/models/event_spec.rb b/spec/models/event_spec.rb index 0c02d31a..05d235c8 100644 --- a/spec/models/event_spec.rb +++ b/spec/models/event_spec.rb @@ -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 @@ -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) @@ -434,7 +434,6 @@ event_addon = event.event_addons.first expect(event_addon&.price).to eq(event_addon&.addon&.default_price) end - end end @@ -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)