Mike.Whitehead
Mike.Whitehead

Reputation: 818

Rails - How to assign booking numbers to events

I'm building an events app using Ruby on Rails. Events can be either free or paid. In each instance I want to be able to assign a unique booking number for users when they book their places on an event. What do I need to do to ensure both paid and free events are assigned a number in the server and a booking is then confirmed? Is it as simple as adding a booking_number column in my bookings table?

This is my model and controller code -

booking.rb

      class Booking < ActiveRecord::Base

    belongs_to :event
    belongs_to :user

    #validates :quantity, presence: true, numericality: { greater_than: 0 }
    validates :total_amount, presence: true, numericality: { greater_than: 0 }
    validates :quantity, :total_amount, presence: true


    def reserve
        # Don't process this booking if it isn't valid
        self.valid?

        # We can always set this, even for free events because their price will be 0.
        #self.total_amount = booking.quantity * event.price

                # Free events don't need to do anything special
                if event.is_free?
                save!

                # Paid events should charge the customer's card
                else

                    begin
                        self.total_amount = event.price_pennies * self.quantity
                        charge = Stripe::Charge.create(
                            amount: total_amount,
                            currency: "gbp",
                            source: stripe_token, 
                            description: "Booking created for amount #{total_amount}")
                        self.stripe_charge_id = charge.id
                        save!
                    rescue Stripe::CardError => e
                    errors.add(:base, e.message)
                    false
                end
            end 
        #end
    end
end

bookings_controller.rb

class BookingsController < ApplicationController

    before_action :authenticate_user!

    def new
        # booking form
        # I need to find the event that we're making a booking on
        @event = Event.find(params[:event_id])
        # and because the event "has_many :bookings"
        @booking = @event.bookings.new(quantity: params[:quantity])
        # which person is booking the event?
        @booking.user = current_user
        #@total_amount = @event.price * @booking.quantity


    end

    def create
        puts params
        # actually process the booking
        @event = Event.find(params[:event_id])
        @booking = @event.bookings.new(booking_params)
        @booking.user = current_user

            if 
                @booking.reserve
                flash[:success] = "Your place on our event has been booked"
                redirect_to event_path(@event)
            else
                flash[:error] = "Booking unsuccessful"
                render "new"
            end
    end


    private

    def booking_params
        params.require(:booking).permit(:stripe_token, :quantity, :event_id, :stripe_charge_id, :total_amount)
    end



end

This is my views code. There's an if/else statement here depending on whether its a paid event or not. The free events 'booking_id' simply shows as blank at the moment - not sure why.

booking.new.html.erb

                <% if @event.is_free? %>
<div class="col-md-6 col-md-offset-3" id="eventshow">
  <div class="row">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h2>Your Booking Confirmation</h2>
        </div>
                <div class="panel-body">

                        <h1>Hi there</h1>

                        <p>You have placed a booking on <%= @event.title %></p>

                        <p>Your order number is <%= @booking.booking_id %></p>

                        <p>We hope you have a wonderful time. Enjoy!</p>

                        <p>Love from Mama Knows Best</p>
                </div>        
                  <div class="panel-footer">
                    <%= link_to "Home", root_path %>
                  </div>
    </div>
  </div>
</div>                      

                <% else %>

<div class="col-md-6 col-md-offset-3" id="eventshow">
  <div class="row">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h2>Confirm Your Booking</h2>
        </div>



                <%= simple_form_for [@event, @booking], id: "new_booking" do |form| %>

                 <div class="calculate-total">
                              <p>
                                  Confirm number of spaces you wish to book here:
                                    <input type="number" placeholder="1" name="booking[quantity]"  min="1" value="1" class="num-spaces">
                              </p>
                                <p>
                                    Total Amount
                                    £<span class="total" data-unit-cost="<%= @event.price %>">0</span>
                                </p>
                          </div>



                 <span class="payment-errors"></span>

                <div class="form-row">
                    <label>
                      <span>Card Number</span>
                      <input type="text" size="20" data-stripe="number"/>
                    </label>
                </div>

                <div class="form-row">
                  <label>
                  <span>CVC</span>
                  <input type="text" size="4" data-stripe="cvc"/>
                  </label>
                </div>

                <div class="form-row">
                    <label>
                        <span>Expiration (MM/YYYY)</span>
                        <input type="text" size="2" data-stripe="exp-month"/>
                    </label>
                    <span> / </span>
                    <input type="text" size="4" data-stripe="exp-year"/>
                </div>
            </div>
            <div class="panel-footer">    

               <%= form.button :submit %>


            </div> 

<% end %>
<% end %>

      </div>
  </div>
</div>  

Upvotes: 3

Views: 258

Answers (2)

Shawn Wilson
Shawn Wilson

Reputation: 1351

So you can use the SecureRandom Generator like this, and assuming your model is named Booking

  ### Add this in your model
  before_create :add_booking_number
  validates_uniqueness_of :add_booking_number

  def add_booking_number
    begin
      self.booking_number = SecureRandom.hex(2).upcase
      other_booking = booking_number.find_by(booking_number: self.booking_number)
    end while other_booking
  end

To use the Moniker "MAMA" you can do

def add_event_ident
  self.booking_number = "MAMA" + '- ' + SecureRandom.hex(2).upcase
  ...
end

the (2) at the end of the .hex controls the length of the generated string (2) will produce 4 random letters. You can find the best length that works for you by going into rails console and playing with it ie.

SecureRandom.hex(4) ...

Upvotes: 1

Jayaprakash
Jayaprakash

Reputation: 1403

You can create a new column like booking_id in your booking table and assign a unique random before the booking object is created.

Ref: Source

If you want the random to be human readable, you can try this

Upvotes: 0

Related Questions