Class: Api::V5::VouchersFilter
- Inherits:
-
Object
- Object
- Api::V5::VouchersFilter
- Includes:
- PaginationConcernFilter
- Defined in:
- app/filters/api/v5/vouchers_filter.rb
Instance Attribute Summary collapse
-
#collections ⇒ Object
Returns the value of attribute collections.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
- #_active ⇒ Object
- #_by_category ⇒ Object
- #_by_currency_code ⇒ Object
- #_inactive ⇒ Object
- #_paginate ⇒ Object
- #active ⇒ Object
- #active_collections ⇒ Object
- #as_json(context) ⇒ Object
- #build_collections ⇒ Object
- #by_category(category) ⇒ Object
- #by_currency_code(currency_code) ⇒ Object
- #inactive ⇒ Object
- #inactive_collections ⇒ Object
-
#initialize(user = nil) ⇒ VouchersFilter
constructor
A new instance of VouchersFilter.
- #page_number(number) ⇒ Object
- #per_page(number) ⇒ Object
Constructor Details
#initialize(user = nil) ⇒ VouchersFilter
Returns a new instance of VouchersFilter.
10 11 12 13 14 15 16 |
# File 'app/filters/api/v5/vouchers_filter.rb', line 10 def initialize(user = nil) @user = user @config = {} user_vouchers = user&.vouchers.active @collections = user_vouchers || Voucher.where('1 = 0') end |
Instance Attribute Details
#collections ⇒ Object
Returns the value of attribute collections.
7 8 9 |
# File 'app/filters/api/v5/vouchers_filter.rb', line 7 def collections @collections end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
8 9 10 |
# File 'app/filters/api/v5/vouchers_filter.rb', line 8 def config @config end |
#user ⇒ Object
Returns the value of attribute user.
7 8 9 |
# File 'app/filters/api/v5/vouchers_filter.rb', line 7 def user @user end |
Instance Method Details
#_active ⇒ Object
77 78 79 |
# File 'app/filters/api/v5/vouchers_filter.rb', line 77 def _active @collections = active_collections if config[:active] end |
#_by_category ⇒ Object
85 86 87 |
# File 'app/filters/api/v5/vouchers_filter.rb', line 85 def _by_category @collections = active_collections.where(voucher_category: config[:category].split(',')) if config[:category] end |
#_by_currency_code ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'app/filters/api/v5/vouchers_filter.rb', line 89 def _by_currency_code currency_code_params = config[:currency_code] currency_code = if currency_code_params.present? currency_code_params.upcase == 'THB' ? ['THB', nil] : currency_code_params.upcase end @collections = active_collections.where(currency_code: currency_code) if currency_code end |
#_inactive ⇒ Object
81 82 83 |
# File 'app/filters/api/v5/vouchers_filter.rb', line 81 def _inactive @collections = inactive_collections if config[:inactive] end |
#_paginate ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'app/filters/api/v5/vouchers_filter.rb', line 56 def _paginate page_number = config.fetch(:page_number, 1) per_page = config.fetch(:per_page, 10) sorted_collections = collections.sort_by { |c| c.expiry_date || c.end_date } @total = collections.length @collections = Kaminari.paginate_array(sorted_collections).page(page_number).per(per_page) end |
#active ⇒ Object
18 19 20 |
# File 'app/filters/api/v5/vouchers_filter.rb', line 18 def active @config[:active] = true end |
#active_collections ⇒ Object
102 103 104 105 |
# File 'app/filters/api/v5/vouchers_filter.rb', line 102 def active_collections invalid_vouchers_ids = (run_out_voucher_ids + used_and_expired_ids).uniq.compact collections.where.not(id: invalid_vouchers_ids) end |
#as_json(context) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/filters/api/v5/vouchers_filter.rb', line 65 def as_json(context) data = build_collections = { total: (@total.presence || 0) } ActiveModelSerializers::SerializableResource.new(data, { serialization_context: context, serializer: ActiveModel::Serializer::CollectionSerializer, each_serializer: Api::V5::VoucherSerializer, meta: , user_id: user.id, adapter: :json_api }).as_json end |
#build_collections ⇒ Object
48 49 50 51 52 53 54 |
# File 'app/filters/api/v5/vouchers_filter.rb', line 48 def build_collections _active _by_category _by_currency_code _inactive _paginate end |
#by_category(category) ⇒ Object
38 39 40 41 |
# File 'app/filters/api/v5/vouchers_filter.rb', line 38 def by_category(category) config[:category] = category if category.present? self end |
#by_currency_code(currency_code) ⇒ Object
43 44 45 46 |
# File 'app/filters/api/v5/vouchers_filter.rb', line 43 def by_currency_code(currency_code) config[:currency_code] = currency_code if currency_code.present? self end |
#inactive ⇒ Object
22 23 24 |
# File 'app/filters/api/v5/vouchers_filter.rb', line 22 def inactive @config[:inactive] = true end |
#inactive_collections ⇒ Object
98 99 100 |
# File 'app/filters/api/v5/vouchers_filter.rb', line 98 def inactive_collections collections.where.not(id: active_collections.ids) end |
#page_number(number) ⇒ Object
26 27 28 29 30 |
# File 'app/filters/api/v5/vouchers_filter.rb', line 26 def page_number(number) n = number.to_i config[:page_number] = n if n.positive? self end |
#per_page(number) ⇒ Object
32 33 34 35 36 |
# File 'app/filters/api/v5/vouchers_filter.rb', line 32 def per_page(number) n = number.to_i config[:per_page] = n if n.positive? self end |