mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-06 01:08:21 +01:00
improved coupon filtering to allow pagination while filtering
This commit is contained in:
parent
44479b5597
commit
3b3e1af822
@ -10,13 +10,8 @@ class API::CouponsController < API::ApiController
|
|||||||
COUPONS_PER_PAGE = 10
|
COUPONS_PER_PAGE = 10
|
||||||
|
|
||||||
def index
|
def index
|
||||||
if params[:filter] == 'all'
|
@coupons = Coupon.method(params[:filter]).call.page(params[:page]).per(COUPONS_PER_PAGE).order('created_at DESC')
|
||||||
@coupons = Coupon.page(params[:page]).per(COUPONS_PER_PAGE).order('created_at DESC')
|
@total = Coupon.method(params[:filter]).call.length
|
||||||
@total = Coupon.count
|
|
||||||
else
|
|
||||||
@coupons = Coupon.order('created_at DESC').all.select { |c| c.status == params[:filter] }
|
|
||||||
@total = @coupons.count
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def show; end
|
def show; end
|
||||||
|
@ -16,6 +16,20 @@ class Coupon < ActiveRecord::Base
|
|||||||
validates_with CouponDiscountValidator
|
validates_with CouponDiscountValidator
|
||||||
validates_with CouponExpirationValidator
|
validates_with CouponExpirationValidator
|
||||||
|
|
||||||
|
scope :disabled, -> { where(active: false) }
|
||||||
|
scope :expired, -> { where('valid_until IS NOT NULL AND valid_until < ?', DateTime.now) }
|
||||||
|
scope :sold_out, lambda {
|
||||||
|
joins(:invoices).select('coupons.*, COUNT(invoices.id) as invoices_count').group('coupons.id')
|
||||||
|
.where.not(max_usages: nil).having('COUNT(invoices.id) >= coupons.max_usages')
|
||||||
|
}
|
||||||
|
scope :active, lambda {
|
||||||
|
joins('LEFT OUTER JOIN invoices ON invoices.coupon_id = coupons.id')
|
||||||
|
.select('coupons.*, COUNT(invoices.id) as invoices_count')
|
||||||
|
.group('coupons.id')
|
||||||
|
.where('active = true AND (valid_until IS NULL OR valid_until >= ?)', DateTime.now)
|
||||||
|
.having('COUNT(invoices.id) < coupons.max_usages OR coupons.max_usages IS NULL')
|
||||||
|
}
|
||||||
|
|
||||||
def safe_destroy
|
def safe_destroy
|
||||||
if invoices.size.zero?
|
if invoices.size.zero?
|
||||||
destroy
|
destroy
|
||||||
|
Loading…
x
Reference in New Issue
Block a user