Class: VoucherForm::AppFirst
- Inherits:
-
Object
- Object
- VoucherForm::AppFirst
- Includes:
- DefaultErrorContainer
- Defined in:
- app/forms/voucher_form/app_first.rb
Overview
generate voucher with valid app first code
Instance Attribute Summary collapse
-
#client_type ⇒ Object
readonly
Returns the value of attribute client_type.
-
#code ⇒ Object
Returns the value of attribute code.
-
#country ⇒ Object
readonly
Returns the value of attribute country.
-
#currency_code ⇒ Object
readonly
Returns the value of attribute currency_code.
-
#temporary ⇒ Object
readonly
Returns the value of attribute temporary.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
- #build_voucher ⇒ Object
-
#initialize(user_id:, client_type:, temporary: false, country: nil) ⇒ AppFirst
constructor
A new instance of AppFirst.
- #valid? ⇒ Boolean
Methods included from DefaultErrorContainer
#error, #error_message_simple, #merge_errors
Constructor Details
#initialize(user_id:, client_type:, temporary: false, country: nil) ⇒ AppFirst
Returns a new instance of AppFirst.
13 14 15 16 17 18 19 20 21 22 23 |
# File 'app/forms/voucher_form/app_first.rb', line 13 def initialize(user_id:, client_type:, temporary: false, country: nil) @user = User.find_by(id: user_id) @temporary = temporary @client_type = client_type if country.blank? @country = Country.find_thailand end @country ||= country @currency_code = @country&.currency_code&.upcase || Country::THAI_CURRENCY_CODE end |
Instance Attribute Details
#client_type ⇒ Object (readonly)
Returns the value of attribute client_type.
8 9 10 |
# File 'app/forms/voucher_form/app_first.rb', line 8 def client_type @client_type end |
#code ⇒ Object
Returns the value of attribute code.
7 8 9 |
# File 'app/forms/voucher_form/app_first.rb', line 7 def code @code end |
#country ⇒ Object (readonly)
Returns the value of attribute country.
8 9 10 |
# File 'app/forms/voucher_form/app_first.rb', line 8 def country @country end |
#currency_code ⇒ Object (readonly)
Returns the value of attribute currency_code.
8 9 10 |
# File 'app/forms/voucher_form/app_first.rb', line 8 def currency_code @currency_code end |
#temporary ⇒ Object (readonly)
Returns the value of attribute temporary.
8 9 10 |
# File 'app/forms/voucher_form/app_first.rb', line 8 def temporary @temporary end |
#user ⇒ Object
Returns the value of attribute user.
7 8 9 |
# File 'app/forms/voucher_form/app_first.rb', line 7 def user @user end |
Instance Method Details
#build_voucher ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'app/forms/voucher_form/app_first.rb', line 25 def build_voucher voucher = Voucher.new voucher.attributes = { id: temporary? ? rand(9999) : nil, user_id: user.id, voucher_type: 'specific_customer', name: 'First App Booking', voucher_code: Voucher::FIRST_APP_CODE, amount: 100, amount_cap_currency: currency_code, amount_currency: currency_code, currency_code: currency_code, min_total_price_currency: currency_code, max_usage: 1, expiry_range_by: 'created_at', expiry_type: 'single', quota: 1, non_refundable: true, expiry_date: Time.thai_time.to_date + 30.days, active: true, payment_type_ids: PaymentType.ids, voucher_category: 'first_app_voucher', require_full_prepayment: true, first_booking_only: false, country_id: country.id, } ServiceResult.new data: voucher end |
#valid? ⇒ Boolean
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'app/forms/voucher_form/app_first.rb', line 55 def valid? errors.clear if user.blank? errors.add(:base, 'Please login to use this voucher') return false end if user.vouchers.where(voucher_category: :first_app_voucher).present? errors.add(:base, 'Sorry you have used this voucher, this voucher is only valid for one time booking') return false end if user.reservations.where(medium: %w[iOS Android]).present? errors.add(:base, I18n.t('views.voucher.invalid_first_app')) return false end unless %w[android ios].include?(client_type.downcase) errors.add(:base, I18n.t('views.voucher.invalid_first_app')) return false end true end |