cohort_scope 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,7 +7,7 @@ module CohortScope
7
7
  # Recursively look for a scope that meets the constraints and is at least <tt>minimum_cohort_size</tt>.
8
8
  def create(active_record, constraints, minimum_cohort_size)
9
9
  if constraints.none? # failing base case
10
- empty_scope = active_record.scoped.where '1 = 2'
10
+ empty_scope = active_record.scoped.where Arel::Nodes::Equality.new(1,2)
11
11
  return new(empty_scope)
12
12
  end
13
13
 
@@ -49,6 +49,30 @@ module CohortScope
49
49
  super
50
50
  end
51
51
  end
52
+
53
+ def where_value_nodes
54
+ __getobj__.instance_variable_get(:@where_values)
55
+ end
56
+
57
+ def active_record
58
+ __getobj__.klass
59
+ end
60
+
61
+ def +(other)
62
+ case other
63
+ when Cohort
64
+ combined_conditions = (where_value_nodes + other.where_value_nodes).inject(nil) do |memo, node|
65
+ if memo.nil?
66
+ node
67
+ else
68
+ memo.or(node)
69
+ end
70
+ end
71
+ Cohort.new active_record.where(combined_conditions)
72
+ else
73
+ super
74
+ end
75
+ end
52
76
 
53
77
  def inspect
54
78
  "<Cohort scope with #{count} members>"
@@ -1,3 +1,3 @@
1
1
  module CohortScope
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
data/lib/cohort_scope.rb CHANGED
@@ -55,4 +55,12 @@ module CohortScope
55
55
  constraints = args
56
56
  StrictCohort.create self, constraints, (options[:minimum_cohort_size] || minimum_cohort_size)
57
57
  end
58
+
59
+ module ActiveRecordRelationExt
60
+ def to_cohort
61
+ Cohort.new self
62
+ end
63
+ end
58
64
  end
65
+
66
+ ::ActiveRecord::Relation.send :include, ::CohortScope::ActiveRecordRelationExt
@@ -82,7 +82,26 @@ class TestCohortScope < Test::Unit::TestCase
82
82
  assert_kind_of Citizen, cohort.last
83
83
  assert_kind_of Citizen, cohort.where(:teeth => 31).first
84
84
  end
85
-
85
+
86
+ def test_011_combine_scopes_with_or
87
+ nobody = Citizen.big_cohort({:favorite_color => 'oaisdjaoisjd'}, :minimum_cohort_size => 1)
88
+ assert_equal 0, nobody.count
89
+ people_who_love_heliotrope_are_from_the_fifties = Citizen.big_cohort({:favorite_color => 'heliotrope'}, :minimum_cohort_size => 1)
90
+ assert_equal 1, people_who_love_heliotrope_are_from_the_fifties.count
91
+ assert people_who_love_heliotrope_are_from_the_fifties.none? { |c| @date_range.include? c.birthdate }
92
+ their_children_are_born_in_the_eighties = Citizen.big_cohort({:birthdate => @date_range}, :minimum_cohort_size => 1)
93
+ assert_equal 9, their_children_are_born_in_the_eighties.count
94
+ everybody = (people_who_love_heliotrope_are_from_the_fifties + their_children_are_born_in_the_eighties + nobody)
95
+ assert_kind_of CohortScope::Cohort, everybody
96
+ assert_equal 10, everybody.count
97
+ end
98
+
99
+ def test_012_to_cohort
100
+ relation = Citizen.big_cohort({:favorite_color => 'heliotrope'}, :minimum_cohort_size => 1).where(:birthdate => @date_range)
101
+ assert_equal [], relation.as_json
102
+ assert_equal({ :members => 0 }, relation.to_cohort.as_json)
103
+ end
104
+
86
105
  private
87
106
 
88
107
  def style
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: cohort_scope
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.0
5
+ version: 0.2.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Seamus Abshere
@@ -12,7 +12,7 @@ autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
14
 
15
- date: 2011-05-17 00:00:00 -05:00
15
+ date: 2011-06-03 00:00:00 -05:00
16
16
  default_executable:
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency