Added customer composite key
This commit is contained in:
@@ -16,6 +16,7 @@ class ReservationsController < ApplicationController
|
||||
def new
|
||||
@reservation = Reservation.new
|
||||
@reservation.team = @company.teams.first
|
||||
@customers = @company.customers
|
||||
end
|
||||
|
||||
# GET /reservations/1/edit
|
||||
@@ -23,18 +24,24 @@ class ReservationsController < ApplicationController
|
||||
|
||||
# POST /reservations or /reservations.json
|
||||
def create
|
||||
@reservation = Reservation.new(reservation_params)
|
||||
@reservation.company = @company
|
||||
@reservation = @company.reservations.new(
|
||||
reservation_params.except(:customer_id, :customer_birth_year)
|
||||
)
|
||||
|
||||
respond_to do |format|
|
||||
if @reservation.save
|
||||
format.html { redirect_to reservation_url(@reservation), notice: t('.reservation_created') }
|
||||
format.json { render :show, status: :created, location: @reservation }
|
||||
else
|
||||
format.html { render :new, status: :unprocessable_entity }
|
||||
format.json { render json: @reservation.errors, status: :unprocessable_entity }
|
||||
end
|
||||
# Find or create customer
|
||||
find_or_create_customer
|
||||
assign_customer_to_reservation
|
||||
|
||||
if @reservation.save
|
||||
redirect_to @reservation, notice: t('.reservation_created')
|
||||
else
|
||||
@customers = @company.customers
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
rescue ActiveRecord::RecordInvalid => e
|
||||
@reservation.errors.add(:base, "Failed to save customer: #{e.message}")
|
||||
@customers = @company.customers
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
|
||||
# PATCH/PUT /reservations/1 or /reservations/1.json
|
||||
@@ -69,11 +76,63 @@ class ReservationsController < ApplicationController
|
||||
|
||||
# Only allow a list of trusted parameters through.
|
||||
def reservation_params
|
||||
params.require(:reservation).permit(:company_id, :customer_id, :team_id, :title, :description, :start_time,
|
||||
:end_time)
|
||||
params.require(:reservation).permit(
|
||||
:customer_id,
|
||||
:team_id,
|
||||
:start_time,
|
||||
:end_time,
|
||||
:title,
|
||||
:description,
|
||||
:customer_first_name,
|
||||
:customer_surname,
|
||||
:customer_original_phone,
|
||||
:customer_birth_year # Keep this in permitted params
|
||||
)
|
||||
end
|
||||
|
||||
def determine_layout
|
||||
action_name == 'index' ? 'calendar' : 'application'
|
||||
end
|
||||
|
||||
def find_or_create_customer
|
||||
customer_params = build_customer_attributes
|
||||
|
||||
@customer = if new_customer?
|
||||
Customer.create!(customer_params)
|
||||
else
|
||||
find_or_initialize_customer(customer_params)
|
||||
end
|
||||
end
|
||||
|
||||
def build_customer_attributes
|
||||
{
|
||||
first_name: params[:reservation][:customer_first_name],
|
||||
surname: params[:reservation][:customer_surname],
|
||||
original_phone: params[:reservation][:customer_original_phone],
|
||||
phone: params[:reservation][:customer_original_phone],
|
||||
birthyear: params[:reservation][:customer_birth_year],
|
||||
company_id: @company.id
|
||||
}
|
||||
end
|
||||
|
||||
def new_customer?
|
||||
params[:reservation][:customer_id].present? &&
|
||||
params[:reservation][:customer_id].end_with?('__new')
|
||||
end
|
||||
|
||||
def find_or_initialize_customer(attributes)
|
||||
Customer.find_or_create_by!(
|
||||
first_name: attributes[:first_name],
|
||||
surname: attributes[:surname],
|
||||
original_phone: attributes[:original_phone]
|
||||
) do |customer|
|
||||
customer.assign_attributes(attributes)
|
||||
end
|
||||
end
|
||||
|
||||
def assign_customer_to_reservation
|
||||
@reservation.customer_first_name = @customer.first_name
|
||||
@reservation.customer_surname = @customer.surname
|
||||
@reservation.customer_original_phone = @customer.original_phone
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user