Module: TypeBalancer::Rails::CollectionMethods
- Defined in:
- lib/type_balancer/rails/collection_methods.rb
Overview
Provides collection methods for balancing by type These methods are extended onto ActiveRecord::Relation
Instance Method Summary collapse
Instance Method Details
#balance_by_type(options = {}) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/type_balancer/rails/collection_methods.rb', line 11 def balance_by_type( = {}) type_field, offset, per_page = pagination_params() cache_key = build_cache_key(type_field) expires_in = [:expires_in] || ::TypeBalancer::Rails.cache_expiry_seconds cache_reset = [:cache_reset] if cache_reset ids = compute_ids(type_field) TypeBalancer::Rails.cache_adapter.write(cache_key, ids, expires_in: expires_in) else ids = TypeBalancer::Rails.cache_adapter.fetch(cache_key, expires_in: expires_in) do compute_ids(type_field) end end page_ids = ids[offset, per_page] || [] return empty_relation if page_ids.empty? order_by_ids(page_ids) end |