Skip to content

Commit

Permalink
event addons
Browse files Browse the repository at this point in the history
Update to mvc for event, event_addons
Still WIP for views
  • Loading branch information
beingmattlevy committed Jul 24, 2024
1 parent ce45b8a commit e1ca747
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 112 deletions.
57 changes: 23 additions & 34 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,28 @@ def show

def new
@event = Event.new(Event::DEFAULT_ATTRIBUTES)
@event.build_default_event_addons
end

def edit
params_symbolized_hash[:event]&.each_pair do |key, value|
@event.send("#{key}=", value) if @event.respond_to?("#{key}=")
end

# build default event addons if event does not have persisted addons
@event.create_default_event_addons

render
end

def create
create_params = params_symbolized_hash[:event].dup
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])
Rails.logger.info("event_create: created event: #{@event.id} event_addons: #{@event.event_addons.inspect}")

if @event.save
redirect_to @event
Expand All @@ -51,6 +59,8 @@ def update
update_params = params_symbolized_hash[:event].dup
TimeHelper.normalize_time_attributes(update_params)

Rails.logger.info("event_update: #{update_params}")

if @event.update(update_params)
redirect_to @event, notice: 'The event has been updated.'
else
Expand Down Expand Up @@ -116,42 +126,21 @@ 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
end

def permitted_params
params.permit(
:id,
:user_email,
:user_id,
:_method,
:authenticity_token,
:commit,
event: %i[
start_time
end_time
ticket_sales_start_time
ticket_sales_end_time
ticket_requests_end_time
adult_ticket_price
allow_donations
allow_financial_assistance
cabin_price
early_arrival_price
end_time
kid_ticket_price
late_departure_price
max_adult_tickets_per_request
max_cabin_requests
max_cabins_per_request
max_kid_tickets_per_request
name
require_mailing_address
require_role
start_time
tickets_require_approval
]
)
.to_hash
.with_indifferent_access
params.permit(:id, :user_email, :user_id, :_method, :authenticity_token, :commit,
event: [:name, :start_time, :end_time,
:ticket_sales_start_time, :ticket_sales_end_time, :ticket_requests_end_time,
:adult_ticket_price, :kid_ticket_price,
: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
end
end
8 changes: 8 additions & 0 deletions app/models/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
# updated_at :datetime not null
#
class Addon < ApplicationRecord
has_many :event_addons, autosave: true
has_many :ticket_request_event_addon, autosave: true

CATEGORIES = [CATEGORY_PASS = 'pass',
CATEGORY_CAMP = 'camp'].freeze

Expand All @@ -23,6 +26,10 @@ def self.categories
CATEGORIES
end

def self.order_by_category
order(:category, :id)
end

def self.find_all_by_category(category)
addons = []

Expand All @@ -32,4 +39,5 @@ def self.find_all_by_category(category)

addons
end

end
38 changes: 35 additions & 3 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ class Event < ApplicationRecord
:photo, :photo_cache, :tickets_require_approval, :require_mailing_address,
:require_role, :allow_financial_assistance, :allow_donations,
:ticket_sales_start_time, :ticket_sales_end_time,
:ticket_requests_end_time
:ticket_requests_end_time, :event_addons_attributes

accepts_nested_attributes_for :event_addons

mount_uploader :photo, PhotoUploader

Expand Down Expand Up @@ -206,8 +208,38 @@ def to_param
[id, slug].join('-') # 1-summer-campout-xii
end

def find_all_addons
Addon.all
def build_event_addons_from_params(build_params)
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"]})
end
end

Rails.logger.debug { "build_event_addons_from_params: save: #{event_addons.inspect}" }
event_addons
end

# create default event addons from Addons
def create_default_event_addons
return if event_addons.present?

build_default_event_addons
save
Rails.logger.debug { "create_default_event_addons: save: #{event_addons.inspect}" }
event_addons
end

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
end

Rails.logger.debug { "build_default_event_addons: #{event_addons.inspect}" }
event_addons
end

private
Expand Down
11 changes: 11 additions & 0 deletions app/models/event_addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,21 @@
class EventAddon < ApplicationRecord
belongs_to :addon
belongs_to :event
attr_accessible :id, :event_id, :addon_id, :price, :event, :addon

before_validation :set_default_values

validates :price, presence: true, numericality: { greater_than_or_equal_to: 0 }

def set_default_values
self.price ||= addon.default_price
end

def name
addon.name
end

def category
addon.category
end
end
42 changes: 12 additions & 30 deletions app/views/events/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

.vertical-20
%fieldset
%legend Ticket Details
%legend Ticket Sale Details
= f.label :ticket_sales_start_time do
Ticket Sales Opening Time
%p.muted
Expand Down Expand Up @@ -75,7 +75,7 @@


.col-sm-12.col-md-6.col-lg-6
%legend Event Pricing
%legend Ticket Pricing
= f.label :adult_ticket_price, 'Adult Ticket Price ($)'
= f.number_field :adult_ticket_price, class: 'input-small', required: true,
min: 0
Expand All @@ -85,14 +85,6 @@
%p.muted Leave blank for no limit
= f.number_field :max_adult_tickets_per_request, class: 'input-mini', min: 1

= f.label :early_arrival_price, 'Early Arrival Price($)'
= f.number_field :early_arrival_price, class: 'input-small', required: true,
min: 0

= f.label :late_departure_price, 'Late Departure Price($)'
= f.number_field :late_departure_price, class: 'input-small', required: true,
min: 0

= f.label :kid_ticket_price do
Kid Ticket Price ($)
%p.muted Keep blank if you want to sell only adult tickets
Expand All @@ -103,25 +95,15 @@
%p.muted Leave blank for no limit
= f.number_field :max_kid_tickets_per_request, class: 'input-mini', min: 1

= f.label :max_cabin_requests do
Maximum number of cabins that can be requested in total
%p.muted
Once more than this number of cabins is requested, subsequent ticket
requesters will not have the option to request cabins. You'll probably
want to set this value to be a little bit higher than the actual number
of cabins at your site, and then manually email the few individuals who
requested a cabin but won't get one. Leave blank for no limit (probably
not a good idea).
= f.number_field :max_cabin_requests, class: 'input-mini', min: 1

= f.label :cabin_price do
Cabin Price ($)
%p.muted Keep blank if there are no cabins or you aren't charging separately for them
= f.number_field :cabin_price, class: 'input-small', min: 0

= f.label :max_cabins_per_request do
Maximum number of cabins allowed per ticket request?
%p.muted Leave blank for no limit
= f.number_field :max_cabins_per_request, class: 'input-mini', min: 1
%legend Event Addons
%p.muted
Set price > 0 to include the addon in the event.
Setting the price to 0 removes the item from being sold.

- @event.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
= event_addon_form.number_field :price, class: 'input-small', min: 0

= f.submit " ▶︎ Update Event", class: 'btn btn-primary btn-large'
19 changes: 11 additions & 8 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,17 @@ def create_events
end
end

def create_addons
return if Addon.count > 0

Addon.create category: Addon::CATEGORY_PASS, name: 'Early Arrival', default_price: 20
Addon.create category: Addon::CATEGORY_PASS, name: 'Late Departure', default_price: 30
Addon.create category: Addon::CATEGORY_CAMP, name: 'Car Camping', default_price: 50
Addon.create category: Addon::CATEGORY_CAMP, name: 'RV under 20ft', default_price: 100
Addon.create category: Addon::CATEGORY_CAMP, name: 'RV under 25ft', default_price: 125
Addon.create category: Addon::CATEGORY_CAMP, name: 'RV over 25ft', default_price: 150
end

def print_event(event)
puts
puts " Name: #{event.name.to_s.colorize(color: :magenta, mode: :bold)}"
Expand All @@ -184,14 +195,6 @@ def header(string)
puts "\n#{format(" %-#{HEADER_WIDTH}.#{HEADER_WIDTH}s".colorize(background: :light_blue), string)}"
end

def create_addons
Addon.create category: Addon::CATEGORY_PASS, name: 'Early Arrival', default_price: 20
Addon.create category: Addon::CATEGORY_PASS, name: 'Late Departure', default_price: 30
Addon.create category: Addon::CATEGORY_CAMP, name: 'Car Camping', default_price: 50
Addon.create category: Addon::CATEGORY_CAMP, name: 'RV under 20ft', default_price: 100
Addon.create category: Addon::CATEGORY_CAMP, name: 'RV under 25ft', default_price: 125
Addon.create category: Addon::CATEGORY_CAMP, name: 'RV over 25ft', default_price: 150
end
end
end

Expand Down
35 changes: 0 additions & 35 deletions spec/controllers/events_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,41 +84,6 @@
expect(new_event.require_role).to be_truthy
end

# rubocop: enable RSpec/AnyInstance

describe '#permitted_params' do
subject(:permitted_keys) { permitted_event_keys }

let(:permitted_event_params) { described_class.new.send(:permitted_params)[:event].to_h.symbolize_keys }
let(:expected_keys) do
%i[
adult_ticket_price
allow_donations
allow_financial_assistance
cabin_price
early_arrival_price
end_time
kid_ticket_price
late_departure_price
max_adult_tickets_per_request
max_cabin_requests
max_cabins_per_request
max_kid_tickets_per_request
name
require_mailing_address
require_role
start_time
ticket_requests_end_time
ticket_sales_end_time
ticket_sales_start_time
tickets_require_approval
]
end
let(:permitted_event_keys) { permitted_event_params.keys.sort }

it { expect(permitted_keys).to eql(expected_keys) }
end

context 'when the user is not signed in' do
before { make_request[] }

Expand Down
5 changes: 3 additions & 2 deletions spec/models/event_addon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@
end

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

it 'has price set not default price' do
expect(event_addon.price).to eq(314_159)
expect(event_addon.price).to eq(price)
end
end
end
Expand Down
Loading

0 comments on commit e1ca747

Please sign in to comment.