Added price currency to conference, select currency in basic info and created data rake task for setting old conferences' price currency

This commit is contained in:
Edward Phillips 2019-07-13 21:39:47 +02:00
parent 9b12830c8f
commit 17488e11c7
8 changed files with 398 additions and 355 deletions

View file

@ -4,6 +4,13 @@ Not release yet...
## Update from 1.0 ## Update from 1.0
### Set Conference Price Currency
Price currency is now set when creating a new conference, for all conferences created before this change the price currency must be set.
```
bundle exec rake data:set_conference_price_currency RAILS_ENV=production
```
### Multiple Schedules ### Multiple Schedules
A conference can have multiple schedules now so it's easier for organizers to A conference can have multiple schedules now so it's easier for organizers to
test schedules and collaborate on different versions. test schedules and collaborate on different versions.

View file

@ -8,14 +8,10 @@ class Ticket < ApplicationRecord
has_paper_trail meta: { conference_id: :conference_id }, has_paper_trail meta: { conference_id: :conference_id },
ignore: %i[updated_at] ignore: %i[updated_at]
monetize :price_cents, with_model_currency: :price_currency monetize :price_cents, with_currency: ->(ticket) { ticket.conference.price_currency }
scope :for_registration, -> { where(registration_ticket: true) } scope :for_registration, -> { where(registration_ticket: true) }
# This validation is for the sake of simplicity.
# If we would allow different currencies per conference we also have to handle convertions between currencies!
validate :tickets_of_conference_have_same_currency
validates :price_cents, :price_currency, :title, presence: true validates :price_cents, :price_currency, :title, presence: true
validates :price_cents, numericality: { greater_than_or_equal_to: 0 } validates :price_cents, numericality: { greater_than_or_equal_to: 0 }
@ -79,6 +75,10 @@ class Ticket < ApplicationRecord
private private
def get_price_currency
'GBP'
end
def tickets_of_conference_have_same_currency def tickets_of_conference_have_same_currency
tickets = Ticket.where(conference_id: conference_id) tickets = Ticket.where(conference_id: conference_id)
return if tickets.count.zero? || (tickets.count == 1 && self == tickets.first) return if tickets.count.zero? || (tickets.count == 1 && self == tickets.first)

View file

@ -7,6 +7,7 @@
input_html: { required: 'required' } input_html: { required: 'required' }
= f.input :short_title, hint: "A short and unique handle for your conference, using only letters, numbers, underscores, and dashes. This will be used to identify your conference in URLs etc. Example: 'froscon2011'", = f.input :short_title, hint: "A short and unique handle for your conference, using only letters, numbers, underscores, and dashes. This will be used to identify your conference in URLs etc. Example: 'froscon2011'",
input_html: { required: 'required', pattern: '[a-zA-Z0-9_-]+', title: 'Only letters, numbers, underscores, and dashes.' }, prepend: conferences_url + '/' input_html: { required: 'required', pattern: '[a-zA-Z0-9_-]+', title: 'Only letters, numbers, underscores, and dashes.' }, prepend: conferences_url + '/'
= f.input :price_currency, as: :select, class: 'form-control', collection: ['USD', 'EUR', 'GBP', 'INR', 'CNY', 'CHF'], include_blank: false, hint: "Please select the currency in which the tickets for this conference will be sold"
= f.inputs 'Scheduling' do = f.inputs 'Scheduling' do
= f.input :timezone, as: :time_zone, default: Time.zone.name, hint: 'Please select in what time zone your conference will take place.' = f.input :timezone, as: :time_zone, default: Time.zone.name, hint: 'Please select in what time zone your conference will take place.'
= f.input :start_date, as: :string, input_html: { id: 'conference-start-datepicker', required: 'required' } = f.input :start_date, as: :string, input_html: { id: 'conference-start-datepicker', required: 'required' }

View file

@ -12,7 +12,10 @@
= f.input :title, input_html: { autofocus: true } = f.input :title, input_html: { autofocus: true }
= f.input :description, input_html: { rows: 5, data: { provide: 'markdown-editable' } } = f.input :description, input_html: { rows: 5, data: { provide: 'markdown-editable' } }
= f.input :price = f.input :price
= f.input :price_currency, as: :select, class: 'form-control', collection: ['USD', 'EUR', 'GBP', 'INR', 'CNY', 'CHF'], include_blank: false %label
Currency
%p
= @conference.price_currency
= f.input :registration_ticket, hint: 'A registration ticket is with which user register for the conference.' = f.input :registration_ticket, hint: 'A registration ticket is with which user register for the conference.'
%p.text-right %p.text-right
= f.action :submit, as: :button, button_html: { class: 'btn btn-primary' } = f.action :submit, as: :button, button_html: { class: 'btn btn-primary' }

View file

@ -5,7 +5,6 @@ Devise.setup do |config|
# ==> openIDs configuration # ==> openIDs configuration
# Define the available openID providers that can be used to log in # Define the available openID providers that can be used to log in
# Pass each provider to User model in :omniauth_providers (for open_id providers use their name) # Pass each provider to User model in :omniauth_providers (for open_id providers use their name)
config.omniauth :open_id, name: 'suse', identifier: 'http://www.opensuse.org/openid/user' config.omniauth :open_id, name: 'suse', identifier: 'http://www.opensuse.org/openid/user'
config.omniauth :google_oauth2, (ENV['OSEM_GOOGLE_KEY'] || Rails.application.secrets.google_key), (ENV['OSEM_GOOGLE_SECRET'] || Rails.application.secrets.google_secret), config.omniauth :google_oauth2, (ENV['OSEM_GOOGLE_KEY'] || Rails.application.secrets.google_key), (ENV['OSEM_GOOGLE_SECRET'] || Rails.application.secrets.google_secret),
name: 'google', name: 'google',

View file

@ -0,0 +1,5 @@
class AddPriceCurrencyToConferences < ActiveRecord::Migration[5.1]
def change
add_column :conferences, :price_currency, :string, default: "USD"
end
end

View file

@ -10,7 +10,10 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20181113195810) do ActiveRecord::Schema.define(version: 20190713163405) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "answers", force: :cascade do |t| create_table "answers", force: :cascade do |t|
t.string "title" t.string "title"
@ -104,6 +107,7 @@ ActiveRecord::Schema.define(version: 20181113195810) do
t.integer "ticket_layout", default: 0 t.integer "ticket_layout", default: 0
t.string "custom_domain" t.string "custom_domain"
t.integer "booth_limit", default: 0 t.integer "booth_limit", default: 0
t.string "price_currency", default: "USD"
t.index ["organization_id"], name: "index_conferences_on_organization_id" t.index ["organization_id"], name: "index_conferences_on_organization_id"
end end

View file

@ -0,0 +1,24 @@
# frozen_string_literal: true
namespace :data do
desc 'Sets price_currency in all pre-existing Conference records'
task set_conference_price_currency: :environment do
Conference.all.each do |conference|
# if ticket has price_currency it is old and needs to be updated
if conference.tickets.first.price_currency
# check all tickets have same price currency
ticket_pc = conference.tickets.first.price_currency
make_change = true
conference.tickets.each do |ticket|
make_change = false unless ticket.price_currency == ticket_pc
end
end
if make_change
conference.update(price_currency: ticket_pc)
puts "price currency of #{ticket_pc} set for conference: #{conference.title}."
else
puts "price currency discrepancy between tickets for conference: #{conference.title}, please check and try again."
end
end
end
end