From 371b984b89e109bb69fafdef559ca1f1b9e02e58 Mon Sep 17 00:00:00 2001 From: Tom Naessens Date: Sat, 15 Oct 2022 21:47:15 +0200 Subject: [PATCH] Add ticket overview screen --- app/actions/generate_html_barcodes.rb | 17 +++++ app/assets/stylesheets/barcodes.css.scss | 4 ++ app/controllers/registrations_controller.rb | 13 +++- app/helpers/application_helper.rb | 6 +- app/models/registration.rb | 8 +++ .../access_levels/_access_level.html.erb | 2 +- app/views/access_levels/_form.html.erb | 2 +- app/views/events/_event_details.html.erb | 45 ++++++++++++++ app/views/events/show.html.erb | 46 +------------- .../confirm_registration.html.erb | 4 +- .../confirm_registration.text.erb | 4 +- .../_registration_payment_form.html.erb | 6 +- app/views/registrations/show.html.erb | 62 +++++++++++++++++++ config/routes.rb | 4 +- ...0221009173017_add_token_to_registration.rb | 17 +++++ db/schema.rb | 4 +- test/fixtures/registrations.yml | 7 +++ test/models/registration_test.rb | 2 + 18 files changed, 194 insertions(+), 59 deletions(-) create mode 100644 app/actions/generate_html_barcodes.rb create mode 100644 app/assets/stylesheets/barcodes.css.scss create mode 100644 app/views/events/_event_details.html.erb create mode 100644 app/views/registrations/show.html.erb create mode 100644 db/migrate/20221009173017_add_token_to_registration.rb diff --git a/app/actions/generate_html_barcodes.rb b/app/actions/generate_html_barcodes.rb new file mode 100644 index 00000000..4c0e5f13 --- /dev/null +++ b/app/actions/generate_html_barcodes.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +require 'barby/barcode/ean_13' +require 'barby/outputter/html_outputter' + +class GenerateHtmlBarcodes + def initialize(barcode_data) + @barcode_data = barcode_data + end + + def call + barcode = Barby::EAN13.new(@barcode_data) + + html_outputter = Barby::HtmlOutputter.new(barcode) + html_outputter.to_html.html_safe # rubocop:disable Rails/OutputSafety + end +end diff --git a/app/assets/stylesheets/barcodes.css.scss b/app/assets/stylesheets/barcodes.css.scss new file mode 100644 index 00000000..1988ddbb --- /dev/null +++ b/app/assets/stylesheets/barcodes.css.scss @@ -0,0 +1,4 @@ +table.barby-barcode { border-spacing: 0; } +tr.barby-row {} +td.barby-cell { width: 3px; height: 70px; } +td.barby-cell.on { background: #000; } diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb index 82f41e8b..a4a2f2d2 100644 --- a/app/controllers/registrations_controller.rb +++ b/app/controllers/registrations_controller.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class RegistrationsController < ApplicationController - before_action :authenticate_user!, only: [:index, :destroy, :resend, :update, :email, :upload] + before_action :authenticate_user!, except: [:new, :create, :show] require 'csv' @@ -47,12 +47,21 @@ def create @registration.deliver flash[:success] = "Registration successful. Please check your mailbox for your ticket or further payment information." - respond_with @event + + redirect_to event_registration_path(@registration.event, @registration.token) else render "events/show" end end + def show + @registration = Registration.find_by(token: params[:token]) + return head(:not_found) unless @registration + + @event = @registration.event + @barcode = GenerateHtmlBarcodes.new(@registration.barcode_data).call + end + def update @registration = Registration.find params.require(:id) authorize! :update, @registration diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index b9dd9933..61cd505c 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -10,8 +10,12 @@ def datepicker_time(field) field.try { |d| d.strftime("%Y-%m-%d %H:%M") } end + def nice_amount(float) + number_with_precision float, precision: 2 + end + def euro(float) - "€#{number_with_precision float, precision: 2}" + number_to_currency(float, unit: '€') end # Form helpers diff --git a/app/models/registration.rb b/app/models/registration.rb index d9400908..3bb650be 100644 --- a/app/models/registration.rb +++ b/app/models/registration.rb @@ -26,6 +26,8 @@ class Registration < ApplicationRecord record.payment_code = Registration.create_payment_code if record.payment_code.nil? end + before_save :ensure_token + after_save do |record| if !record.access_level.capacity.nil? && (record.access_level.registrations.count > record.access_level.capacity) record.errors.add :access_level, "type is sold out." @@ -95,6 +97,10 @@ def deliver end end + def ensure_token + self.token ||= SecureRandom.uuid + end + private def from_cents(value) @@ -122,6 +128,7 @@ def to_cents(value) # payment_code :string(255) # price :integer # student_number :string(255) +# token :string(255) not null # created_at :datetime # updated_at :datetime # access_level_id :integer not null @@ -132,6 +139,7 @@ def to_cents(value) # index_registrations_on_access_level_id (access_level_id) # index_registrations_on_event_id (event_id) # index_registrations_on_payment_code (payment_code) UNIQUE +# index_registrations_on_token (token) # # Foreign Keys # diff --git a/app/views/access_levels/_access_level.html.erb b/app/views/access_levels/_access_level.html.erb index 6644261c..3a808e95 100644 --- a/app/views/access_levels/_access_level.html.erb +++ b/app/views/access_levels/_access_level.html.erb @@ -4,7 +4,7 @@ <%= access_level.registrations.size %> (<%= access_level.registrations.paid.size %>) / <%= access_level.capacity.presence || "∞".html_safe %> <%= "#{pluralize(access_level.tickets_left, "ticket")} left" if access_level.capacity.presence %> - € <%= number_with_precision access_level.price || "0", precision: 2 %> + €<%= nice_amount(access_level.price || "0") %> <%= translate access_level.permit %> <%= access_level.has_comment ? 'Yes' : 'No' %> <%= access_level.hidden ? 'Yes' : 'No' %> diff --git a/app/views/access_levels/_form.html.erb b/app/views/access_levels/_form.html.erb index 0c3888f2..ff826e1a 100644 --- a/app/views/access_levels/_form.html.erb +++ b/app/views/access_levels/_form.html.erb @@ -2,7 +2,7 @@ <%= form_for [object.event, object], remote: true do |f| %> <%= f.text_field :name, class: 'form-control' %> <%= f.number_field :capacity, class: 'form-control', placeholder: 0 %> - <%= f.text_field :price, value: number_with_precision(f.object.price, precision: 2), class: 'form-control', placeholder: "0.00" %> + <%= f.text_field :price, value: nice_amount(f.object.price), class: 'form-control', placeholder: "0.00" %> <%= f.collection_select :permit, AccessLevel.permits.keys, :itself, lambda {|k| translate(k)}, {}, class: 'form-control' %> diff --git a/app/views/events/_event_details.html.erb b/app/views/events/_event_details.html.erb new file mode 100644 index 00000000..db88af59 --- /dev/null +++ b/app/views/events/_event_details.html.erb @@ -0,0 +1,45 @@ +

Description

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Organisation<%= event.club.name %>
Location<%= event.location %>
Start date<%= nice_time event.start_date %>
End date<%= nice_time event.end_date %>
+ <%= event.description.html_safe %> +
Website<%= link_to event.website, event.website %>
Contact e-mail<%= mail_to event.contact_email, event.contact_email %>
+ +<% unless event.access_levels.blank? %> +

Tickets

+
+
+
    + <% event.access_levels.public?.each do |al| %> + <%= render partial: "events/ticket", locals: {al: al} %> + <% end %> +
+
+
+<% end %> diff --git a/app/views/events/show.html.erb b/app/views/events/show.html.erb index e2420c67..9f74d137 100644 --- a/app/views/events/show.html.erb +++ b/app/views/events/show.html.erb @@ -2,51 +2,7 @@
-

Description

- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Organisation<%= @event.club.name %>
Location<%= @event.location %>
Start date<%= nice_time @event.start_date %>
End date<%= nice_time @event.end_date %>
- <%= @event.description.html_safe %> -
Website<%= link_to @event.website, @event.website %>
Contact e-mail<%= mail_to @event.contact_email, @event.contact_email %>
- - <% unless @event.access_levels.blank? %> -

Tickets

-
-
-
    - <% @event.access_levels.public?.each do |al| %> - <%= render partial: "events/ticket", locals: {al: al} %> - <% end %> -
-
-
- <% end %> + <%= render partial: 'events/event_details', locals: { event: @event } %>
diff --git a/app/views/registration_mailer/confirm_registration.html.erb b/app/views/registration_mailer/confirm_registration.html.erb index e9741ae3..5276a583 100644 --- a/app/views/registration_mailer/confirm_registration.html.erb +++ b/app/views/registration_mailer/confirm_registration.html.erb @@ -9,7 +9,7 @@

Bedankt voor het bestellen van een ticket voor <%= @registration.event.name %> van <%= @registration.event.club.name %>. Als u zich niet opgegeven hebt voor dit ticket, dan heeft iemand anders uw e-mailadres gebruikt om dit te registreren. Mocht dit het geval zijn, mag u deze e-mail negeren.

-

Om uw ticket te ontvangen via e-mail, schrijft u <%= number_with_precision @registration.to_pay, precision: 2 %> euro over op het rekeningnummer <%= @registration.event.bank_number || "our bank account" %>. Plaats de code "<%= @registration.payment_code %>" in de mededeling van de overschrijving (zonder aanhalingstekens). Als u de code vergeet mee te geven of niet correct vermeldt in de beschrijving, dan kunnen wij uw betaling niet verwerken en zal u uw ticket niet ontvangen. U mag maximaal één code ingeven per overschrijving.

+

Om uw ticket te ontvangen via e-mail, schrijft u <%= nice_amount @registration.to_pay %> euro over op het rekeningnummer <%= @registration.event.bank_number || "our bank account" %>. Plaats de code "<%= @registration.payment_code %>" in de mededeling van de overschrijving (zonder aanhalingstekens). Als u de code vergeet mee te geven of niet correct vermeldt in de beschrijving, dan kunnen wij uw betaling niet verwerken en zal u uw ticket niet ontvangen. U mag maximaal één code ingeven per overschrijving.

Gelieve ten laatste 3 dagen voor het evenement te betalen. Indien dit niet meer lukt, gelieve ons te contacteren via <%= mail_to @registration.event.contact_email, @registration.event.contact_email %> om een andere betalingswijze af te spreken. Eens we uw betaling verwerkt hebben, zal u uw ticket via e-mail ontvangen.

@@ -27,7 +27,7 @@

Thank you for buying a ticket for <%= @registration.event.name %> of <%= @registration.event.club.name %>. If you did not register for this ticket, someone else used your email address to register. If this is the case, please ignore this email.

-

To receive your ticket by mail, please transfer <%= number_with_precision @registration.to_pay, precision: 2 %> euro to <%= @registration.event.bank_number || "our bank account" %>. Place "<%= @registration.payment_code %>" in the description of your transfer (without the quotation marks). If you forget this code or you do not copy it correctly, we cannot process your payment and you will not receive your ticket. You are only allowed to enter one code per transfer.

+

To receive your ticket by mail, please transfer <%= nice_amount @registration.to_pay %> euro to <%= @registration.event.bank_number || "our bank account" %>. Place "<%= @registration.payment_code %>" in the description of your transfer (without the quotation marks). If you forget this code or you do not copy it correctly, we cannot process your payment and you will not receive your ticket. You are only allowed to enter one code per transfer.

Please pay at least 3 days before the event. If this is no longer possible, please contact us via <%= mail_to @registration.event.contact_email, @registration.event.contact_email %> to agree upon a different payment method. You will receive your ticket by mail once we processed your payment.

diff --git a/app/views/registration_mailer/confirm_registration.text.erb b/app/views/registration_mailer/confirm_registration.text.erb index 5d736475..70880f26 100644 --- a/app/views/registration_mailer/confirm_registration.text.erb +++ b/app/views/registration_mailer/confirm_registration.text.erb @@ -4,7 +4,7 @@ Beste <%= @registration.name %>, Bedankt voor het bestellen van een ticket voor <%= @registration.event.name %> van <%= @registration.event.club.name %>. Als u zich niet opgegeven hebt voor dit ticket, dan heeft iemand anders uw e-mailadres gebruikt om dit te registreren. Mocht dit het geval zijn, mag u deze e-mail negeren. -Om uw ticket te ontvangen via e-mail, schrijft u <%= number_with_precision @registration.to_pay, precision: 2 %> euro over op het rekeningnummer <%= @registration.event.bank_number || "our bank account" %>. Plaats de code "<%= @registration.payment_code %>" in de mededeling van de overschrijving (zonder aanhalingstekens). Als u de code vergeet mee te geven of niet correct vermeldt in de beschrijving, dan kunnen wij uw betaling niet verwerken en zal u uw ticket niet ontvangen. U mag maximaal 1 code ingeven per overschrijving. +Om uw ticket te ontvangen via e-mail, schrijft u <%= nice_amount @registration.to_pay %> euro over op het rekeningnummer <%= @registration.event.bank_number || "our bank account" %>. Plaats de code "<%= @registration.payment_code %>" in de mededeling van de overschrijving (zonder aanhalingstekens). Als u de code vergeet mee te geven of niet correct vermeldt in de beschrijving, dan kunnen wij uw betaling niet verwerken en zal u uw ticket niet ontvangen. U mag maximaal 1 code ingeven per overschrijving. Gelieve ten laatste 3 dagen voor het evenement te betalen. Indien dit niet meer lukt, gelieve ons te contacteren via <%= @registration.event.contact_email %> om een andere betalingswijze af te spreken. Eens we uw betaling verwerkt hebben, zal u uw ticket via e-mail ontvangen. @@ -23,7 +23,7 @@ Dear <%= @registration.name %>, Thank you for buying a ticket for <%= @registration.event.name %> of <%= @registration.event.club.name %>. If you did not register for this ticket, someone else used your email address to register. If this is the case, please ignore this email. -To receive your ticket by mail, please transfer <%= number_with_precision @registration.to_pay, precision: 2 %> euro to <%= @registration.event.bank_number || "our bank account" %>. Place "<%= @registration.payment_code %>" in the description of your transfer (without the quotation marks). If you forget this code or you do not copy it correctly, we cannot process your payment and you will not receive your ticket. You are only allowed to enter one code per transfer. +To receive your ticket by mail, please transfer <%= nice_amount @registration.to_pay %> euro to <%= @registration.event.bank_number || "our bank account" %>. Place "<%= @registration.payment_code %>" in the description of your transfer (without the quotation marks). If you forget this code or you do not copy it correctly, we cannot process your payment and you will not receive your ticket. You are only allowed to enter one code per transfer. Please pay at least 3 days before the event. If this is no longer possible, please contact us via <%= @registration.event.contact_email %> to agree upon a different payment method. You will receive your ticket by mail once we processed your payment. diff --git a/app/views/registrations/_registration_payment_form.html.erb b/app/views/registrations/_registration_payment_form.html.erb index 22d0b84b..ab61ab7b 100644 --- a/app/views/registrations/_registration_payment_form.html.erb +++ b/app/views/registrations/_registration_payment_form.html.erb @@ -1,8 +1,8 @@ <%= form_for [registration.event, registration], method: :patch, remote: true, html: { class: 'form-inline' } do |f| %>
-
- <%= f.text_field :to_pay, value: number_with_precision(f.object.to_pay, precision: 2), disabled: true, class: 'disabling registration-paid form-control' %> - <%= f.hidden_field :price, value: number_with_precision(f.object.price, precision: 2), disabled: true, class: 'registration-price' %> +
+ <%= f.text_field :to_pay, value: nice_amount(f.object.to_pay), disabled: true, class: 'disabling registration-paid form-control' %> + <%= f.hidden_field :price, value: nice_amount(f.object.price), disabled: true, class: 'registration-price' %>
<%= check_box_tag 'is_paid', '1', f.object.paid?, class: 'disabling registration-box input-append', disabled: true %>
diff --git a/app/views/registrations/show.html.erb b/app/views/registrations/show.html.erb new file mode 100644 index 00000000..f6ed03f2 --- /dev/null +++ b/app/views/registrations/show.html.erb @@ -0,0 +1,62 @@ +
+
+ <%= render partial: 'events/event_details', locals: { event: @event } %> +

+ <%= link_to "Register another ticket", event_path(@registration.event) %> +

+
+ +
+

Your ticket

+ + + + + + + + + + + + + +
Name<%= @registration.name %>
Email<%= @registration.email %>
Ticket<%= @registration.access_level.name %>
+ + <% if @registration.paid? %> +
+ <%= @barcode %> +
+
+
+ <%= @barcode %> +
+
+ <% else %> +

Payment

+ + + + + + + + + + + + + +
Price<%= euro @registration.access_level.price %>
Already paid- <%= euro @registration.paid %>
Remaining<%= euro @registration.to_pay %>
+ +

+ Om uw ticket te ontvangen via e-mail, schrijft u <%= nice_amount @registration.to_pay %> euro + over op het rekeningnummer <%= @registration.event.bank_number || "our bank account" %>. Plaats de code + "<%= @registration.payment_code %>" in de mededeling van de overschrijving (zonder aanhalingstekens).
+ Als u de + code vergeet mee te geven of niet correct vermeldt in de beschrijving, dan kunnen wij uw betaling niet verwerken + en zal u uw ticket niet ontvangen. U mag maximaal één code ingeven per overschrijving. +

+ <% end %> +
+
diff --git a/config/routes.rb b/config/routes.rb index 59487927..cf950167 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -38,7 +38,8 @@ end end resources :role_names - resources :registrations do + + resources :registrations, except: [:show] do member do get 'resend' get 'info' @@ -49,6 +50,7 @@ post 'email' end end + resources :registrations, only: [:show], param: :token member do get 'statistics' diff --git a/db/migrate/20221009173017_add_token_to_registration.rb b/db/migrate/20221009173017_add_token_to_registration.rb new file mode 100644 index 00000000..d8b0bdfb --- /dev/null +++ b/db/migrate/20221009173017_add_token_to_registration.rb @@ -0,0 +1,17 @@ +class AddTokenToRegistration < ActiveRecord::Migration[6.1] + def up + # To be converted to UUID type when there's support for it in Rails+MySQL + add_column :registrations, :token, :string, null: true + Registration.find_each do |r| + r.update_column(:token, SecureRandom.uuid) + end + change_column_null :registrations, :token, false + + add_index :registrations, :token + end + + def down + remove_index :registrations, :token + remove_column :registrations, :token + end +end diff --git a/db/schema.rb b/db/schema.rb index 5c65dc5b..5b3def57 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_10_09_134241) do +ActiveRecord::Schema.define(version: 2022_10_09_173017) do create_table "access_levels", id: :integer, charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t| t.string "name" @@ -142,9 +142,11 @@ t.string "barcode_data" t.string "payment_code" t.integer "access_level_id", null: false + t.string "token", null: false t.index ["access_level_id"], name: "index_registrations_on_access_level_id" t.index ["event_id"], name: "index_registrations_on_event_id" t.index ["payment_code"], name: "index_registrations_on_payment_code", unique: true + t.index ["token"], name: "index_registrations_on_token" end create_table "users", id: :integer, charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t| diff --git a/test/fixtures/registrations.yml b/test/fixtures/registrations.yml index e3bcb2c5..06195e0f 100644 --- a/test/fixtures/registrations.yml +++ b/test/fixtures/registrations.yml @@ -12,6 +12,7 @@ one: price: 0 payment_code: <%= Registration.create_payment_code %> access_level_id: 1 + token: <%= SecureRandom.uuid %> two: id: 2 @@ -25,6 +26,7 @@ two: price: 10 payment_code: <%= Registration.create_payment_code %> access_level_id: 2 + token: <%= SecureRandom.uuid %> three: id: 3 @@ -38,6 +40,7 @@ three: price: 20 payment_code: GAN7539840256920891 access_level_id: 1 + token: <%= SecureRandom.uuid %> four: id: 4 @@ -51,6 +54,7 @@ four: price: 10 payment_code: <%= Registration.create_payment_code %> access_level_id: 1 + token: <%= SecureRandom.uuid %> <% 9.times do |n| %> capacity_registration_<%= n %>: @@ -64,6 +68,7 @@ capacity_registration_<%= n %>: student_number: <%= 100 + n %> payment_code: <%= Registration.create_payment_code %> access_level_id: <%= 100 + (n % 3) %> + token: <%= SecureRandom.uuid %> <% end %> # == Schema Information @@ -81,6 +86,7 @@ capacity_registration_<%= n %>: # payment_code :string(255) # price :integer # student_number :string(255) +# token :string(255) not null # created_at :datetime # updated_at :datetime # access_level_id :integer not null @@ -91,6 +97,7 @@ capacity_registration_<%= n %>: # index_registrations_on_access_level_id (access_level_id) # index_registrations_on_event_id (event_id) # index_registrations_on_payment_code (payment_code) UNIQUE +# index_registrations_on_token (token) # # Foreign Keys # diff --git a/test/models/registration_test.rb b/test/models/registration_test.rb index 8f123bc7..0adbf851 100644 --- a/test/models/registration_test.rb +++ b/test/models/registration_test.rb @@ -79,6 +79,7 @@ def teardown # payment_code :string(255) # price :integer # student_number :string(255) +# token :string(255) not null # created_at :datetime # updated_at :datetime # access_level_id :integer not null @@ -89,6 +90,7 @@ def teardown # index_registrations_on_access_level_id (access_level_id) # index_registrations_on_event_id (event_id) # index_registrations_on_payment_code (payment_code) UNIQUE +# index_registrations_on_token (token) # # Foreign Keys #