From 9e154a1dcd5d28102d852eb6a26eefba2cf054db Mon Sep 17 00:00:00 2001 From: siddhantbajaj Date: Mon, 10 Jul 2017 20:43:27 +0530 Subject: [PATCH] Added Token field for physical_ticket Added token field in physical_ticket model. This token will also be stored in the qr code and will uniqely identify the ticket. --- app/controllers/physical_ticket_controller.rb | 2 +- app/models/physical_ticket.rb | 15 +++++++++++++++ app/views/admin/physical_ticket/index.html.haml | 6 +++--- app/views/physical_ticket/index.html.haml | 4 ++-- app/views/physical_ticket/show.html.haml | 2 +- ...0170721001700_add_index_to_physical_tickets.rb | 6 ++++++ db/schema.rb | 5 ++++- .../physical_ticket_controller_spec.rb | 2 +- 8 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 db/migrate/20170721001700_add_index_to_physical_tickets.rb diff --git a/app/controllers/physical_ticket_controller.rb b/app/controllers/physical_ticket_controller.rb index 6bb7f50b..fb25ae63 100644 --- a/app/controllers/physical_ticket_controller.rb +++ b/app/controllers/physical_ticket_controller.rb @@ -1,7 +1,7 @@ class PhysicalTicketController < ApplicationController before_action :authenticate_user! load_resource :conference, find_by: :short_title - load_and_authorize_resource + load_and_authorize_resource find_by: :token authorize_resource :conference_registrations, class: Registration def index diff --git a/app/models/physical_ticket.rb b/app/models/physical_ticket.rb index 6142c875..689d118a 100644 --- a/app/models/physical_ticket.rb +++ b/app/models/physical_ticket.rb @@ -4,4 +4,19 @@ class PhysicalTicket < ActiveRecord::Base has_one :conference, through: :ticket_purchase has_one :user, through: :ticket_purchase has_many :ticket_scannings + + before_create :set_token + + private + + def set_token + self.token = generate_token + end + + def generate_token + loop do + token = SecureRandom.hex(10) + break token unless PhysicalTicket.exists?(token: token) + end + end end diff --git a/app/views/admin/physical_ticket/index.html.haml b/app/views/admin/physical_ticket/index.html.haml index fe128932..96c5911f 100644 --- a/app/views/admin/physical_ticket/index.html.haml +++ b/app/views/admin/physical_ticket/index.html.haml @@ -32,12 +32,12 @@ .btn-group = link_to 'Show', conference_physical_ticket_path(@conference.short_title, - physical_ticket.id), + physical_ticket.token), class: 'btn btn-primary' = link_to 'Generate PDF', conference_physical_ticket_path(@conference.short_title, - physical_ticket.id, - format: :pdf), + physical_ticket.token, + format: :pdf), class: 'button btn btn-default btn-info' - else %h5 No Tickets sold! diff --git a/app/views/physical_ticket/index.html.haml b/app/views/physical_ticket/index.html.haml index b077400b..cb0710ad 100644 --- a/app/views/physical_ticket/index.html.haml +++ b/app/views/physical_ticket/index.html.haml @@ -24,11 +24,11 @@ .btn-group = link_to 'Show', conference_physical_ticket_path(@conference.short_title, - physical_ticket.id), + physical_ticket.token), class: 'btn btn-primary' = link_to 'Generate PDF', conference_physical_ticket_path(@conference.short_title, - physical_ticket.id, + physical_ticket.token, format: :pdf), class: 'button btn btn-default btn-info' - else diff --git a/app/views/physical_ticket/show.html.haml b/app/views/physical_ticket/show.html.haml index 26fa7853..067f0a9b 100644 --- a/app/views/physical_ticket/show.html.haml +++ b/app/views/physical_ticket/show.html.haml @@ -72,6 +72,6 @@ %p.text-left = link_to 'Generate PDF', conference_physical_ticket_path(@conference.short_title, - @physical_ticket.id, + @physical_ticket.token, format: :pdf), class: 'button btn btn-default btn-info' diff --git a/db/migrate/20170721001700_add_index_to_physical_tickets.rb b/db/migrate/20170721001700_add_index_to_physical_tickets.rb new file mode 100644 index 00000000..46a0b8c0 --- /dev/null +++ b/db/migrate/20170721001700_add_index_to_physical_tickets.rb @@ -0,0 +1,6 @@ +class AddIndexToPhysicalTickets < ActiveRecord::Migration + def change + add_column :physical_tickets, :token, :string + add_index :physical_tickets, :token, unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 64ac0393..d49dfa03 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170711102511) do +ActiveRecord::Schema.define(version: 20170721001700) do create_table "ahoy_events", force: :cascade do |t| t.integer "visit_id" @@ -317,8 +317,11 @@ ActiveRecord::Schema.define(version: 20170711102511) do t.integer "ticket_purchase_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "token" end + add_index "physical_tickets", ["token"], name: "index_physical_tickets_on_token", unique: true + create_table "programs", force: :cascade do |t| t.integer "conference_id" t.integer "rating", default: 0 diff --git a/spec/controllers/physical_ticket_controller_spec.rb b/spec/controllers/physical_ticket_controller_spec.rb index 7a430479..863e27af 100644 --- a/spec/controllers/physical_ticket_controller_spec.rb +++ b/spec/controllers/physical_ticket_controller_spec.rb @@ -9,7 +9,7 @@ describe PhysicalTicketController do describe 'GET #show' do before :each do sign_in user - get :show, id: physical_ticket.id, conference_id: conference.short_title + get :show, id: physical_ticket.token, conference_id: conference.short_title end it 'assigns ticket_layout' do