diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index d661a9b3e..5d29ef60d 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -2,6 +2,7 @@ class UsersController < ApplicationController before_action :authenticate_user!, only: :search + before_action :set_currency_options, only: [:edit, :new, :create, :update] load_and_authorize_resource # GET /users/1 @@ -40,7 +41,7 @@ def search def user_params params[:user][:timezone] = params[:user][:timezone].presence || nil - params.require(:user).permit(:name, :biography, :nickname, :affiliation, + params.require(:user).permit(:name, :biography, :nickname, :affiliation, :default_currency, :picture, :picture_cache, :timezone) end @@ -49,5 +50,9 @@ def user_params def load_user @user ||= (params[:id] && params[:id] != 'current' && User.find(params[:id])) || current_user end + # rubocop:enable Naming/MemoizedInstanceVariableName + def set_currency_options + @currency_options = CurrencyConversion::VALID_CURRENCIES.map { |currency| [currency, currency] } + end end diff --git a/app/models/currency_conversion.rb b/app/models/currency_conversion.rb index ffc2ca197..9d25094c6 100644 --- a/app/models/currency_conversion.rb +++ b/app/models/currency_conversion.rb @@ -24,6 +24,8 @@ class CurrencyConversion < ApplicationRecord VALID_CURRENCIES = %w[AUD CAD CHF CNY EUR GBP JPY USD].freeze belongs_to :conference validates :rate, numericality: { greater_than: 0 } + # Ensure from_currency and to_currency are among the VALID_CURRENCIES + validates :from_currency, :to_currency, inclusion: { in: VALID_CURRENCIES } validates :from_currency, uniqueness: { scope: :to_currency }, on: :create def self.convert_currency(conference, amount, from_currency, to_currency) diff --git a/app/models/user.rb b/app/models/user.rb index bcf38b4db..bfa75bb97 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -16,6 +16,7 @@ # confirmed_at :datetime # current_sign_in_at :datetime # current_sign_in_ip :string +# default_currency :string # email :string default(""), not null # email_public :boolean default(FALSE) # encrypted_password :string default(""), not null diff --git a/app/views/users/edit.html.haml b/app/views/users/edit.html.haml index d1b967620..aff35bd69 100644 --- a/app/views/users/edit.html.haml +++ b/app/views/users/edit.html.haml @@ -23,6 +23,9 @@ The timezone setting will update the event schedules to show using the time you selected. Your browser is current set to %span.js-localTimezone + .form-group + = f.label :default_currency, 'Default Currency' + = f.select :default_currency, options_for_select(@currency_options, selected: @user.default_currency), {}, { class: 'form-control' } .form-group = f.label :avatar %br diff --git a/db/schema.rb b/db/schema.rb index 06f91c428..240119034 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -627,6 +627,7 @@ t.boolean "is_disabled", default: false t.string "picture" t.string "timezone" + t.string "default_currency" t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 78784fa4f..f9135376c 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -16,6 +16,7 @@ # confirmed_at :datetime # current_sign_in_at :datetime # current_sign_in_ip :string +# default_currency :string # email :string default(""), not null # email_public :boolean default(FALSE) # encrypted_password :string default(""), not null diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index a999ef03a..151acca47 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -16,6 +16,7 @@ # confirmed_at :datetime # current_sign_in_at :datetime # current_sign_in_ip :string +# default_currency :string # email :string default(""), not null # email_public :boolean default(FALSE) # encrypted_password :string default(""), not null