Sha256: 3154cc986b0dd80f093d866ca80a3df7c2abe6cfd1d2334330b0fedef5e9002d
Contents?: true
Size: 2 KB
Versions: 2
Compression:
Stored size: 2 KB
Contents
require 'active_record' require 'active_support/core_ext' require 'cohort_scope/cohort' require 'cohort_scope/big_cohort' require 'cohort_scope/strict_cohort' module CohortScope def self.extended(klass) klass.class_eval do class << self attr_accessor :minimum_cohort_size end end end def self.conditions_for(constraints) case constraints when ::Array constraints.inject({}) { |memo, (k, v)| memo[k] = v; memo } when ::Hash constraints.dup end end # Find the biggest scope possible by removing constraints <b>in any order</b>. # Returns an empty scope if it can't meet the minimum scope size. def big_cohort(constraints, options = {}) BigCohort.create self, constraints, (options[:minimum_cohort_size] || minimum_cohort_size) end # Find the first acceptable scope by removing constraints <b>in strict order</b>, starting with the last constraint. # Returns an empty scope if it can't meet the minimum scope size. # # <tt>constraints</tt> must be key/value pairs (splat if it's an array) # # Note that the first constraint is implicitly required. # # Take this example, where favorite color is considered to be "more important" than birthdate: # # ordered_constraints = [ [:favorite_color, 'heliotrope'], [:birthdate, '1999-01-01'] ] # Citizen.strict_cohort(*ordered_constraints) #=> [...] # # If the original constraints don't meet the minimum scope size, then the only constraint that can be removed is birthdate. # In other words, this would never return a scope that was constrained on birthdate but not on favorite_color. def strict_cohort(*args) args = args.dup options = args.last.is_a?(::Hash) ? args.pop : {} constraints = args StrictCohort.create self, constraints, (options[:minimum_cohort_size] || minimum_cohort_size) end module ActiveRecordRelationExt def to_cohort Cohort.new self end end end ::ActiveRecord::Relation.send :include, ::CohortScope::ActiveRecordRelationExt
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
cohort_scope-0.2.3 | lib/cohort_scope.rb |
cohort_scope-0.2.2 | lib/cohort_scope.rb |