cohort_scope 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
data/cohort_scope.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cohort_scope}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Seamus Abshere", "Andy Rossmeissl", "Derek Kastner"]
12
- s.date = %q{2010-10-15}
12
+ s.date = %q{2010-10-29}
13
13
  s.description = %q{Provides big_cohort, which widens by finding the constraint that eliminates the most records and removing it. Also provides strict_cohort, which widens by eliminating constraints in order.}
14
14
  s.email = %q{seamus@abshere.net}
15
15
  s.extra_rdoc_files = [
@@ -38,9 +38,9 @@ Gem::Specification.new do |s|
38
38
  s.rubygems_version = %q{1.3.7}
39
39
  s.summary = %q{Provides cohorts (in the form of ActiveRecord scopes) that dynamically widen until they contain a certain number of records.}
40
40
  s.test_files = [
41
- "test/helper.rb",
42
- "test/test_cohort.rb",
43
- "test/test_cohort_scope.rb"
41
+ "test/test_cohort.rb",
42
+ "test/test_cohort_scope.rb",
43
+ "test/helper.rb"
44
44
  ]
45
45
 
46
46
  if s.respond_to? :specification_version then
@@ -32,9 +32,9 @@ module CohortScope
32
32
  conditions = constraints.inject(new_hash) do |memo, tuple|
33
33
  k, v = tuple
34
34
  if v.kind_of?(ActiveRecord::Base)
35
- primary_key = association_primary_key(model, k)
36
- param = v.respond_to?(primary_key) ? v.send(primary_key) : v.to_param
37
- condition = { primary_key => param }
35
+ foreign_key = association_foreign_key model, k
36
+ lookup_value = association_lookup_value model, k, v
37
+ condition = { foreign_key => lookup_value }
38
38
  elsif !v.nil?
39
39
  condition = { k => v }
40
40
  end
@@ -44,21 +44,37 @@ module CohortScope
44
44
  conditions
45
45
  end
46
46
 
47
- # Convert constraints that are provided as ActiveRecord::Base objects into their corresponding integer primary keys.
47
+ # Convert constraints that are provided as ActiveRecord::Base objects into their corresponding primary keys.
48
48
  #
49
49
  # Only works for <tt>belongs_to</tt> relationships.
50
50
  #
51
- # For example, :car => <#Car> might get translated into :car_id => 44.
52
- def association_primary_key(model, name)
53
- @_cohort_association_primary_keys ||= {}
54
- return @_cohort_association_primary_keys[name] if @_cohort_association_primary_keys.has_key? name
55
- a = model.reflect_on_association name
56
- raise "there is no association #{name.inspect} on #{model}" if a.nil?
57
- raise "can't use cohort scope on :through associations (#{self.name} #{name})" if a.options.has_key? :through
58
- if !a.primary_key_name.blank?
59
- @_cohort_association_primary_keys[name] = a.primary_key_name
51
+ # For example, :car => <#Car> might get translated into :car_id => 44 or :car_type => 44 if :foreign_key option is given.
52
+ def association_foreign_key(model, name)
53
+ @_cohort_association_foreign_keys ||= {}
54
+ return @_cohort_association_foreign_keys[name] if @_cohort_association_foreign_keys.has_key? name
55
+ association = model.reflect_on_association name
56
+ raise "there is no association #{name.inspect} on #{model}" if association.nil?
57
+ raise "can't use cohort scope on :through associations (#{self.name} #{name})" if association.options.has_key? :through
58
+ foreign_key = association.instance_variable_get(:@options)[:foreign_key]
59
+ if !foreign_key.blank?
60
+ @_cohort_association_foreign_keys[name] = foreign_key
60
61
  else
61
- raise "we need some other way to find primary key"
62
+ @_cohort_association_foreign_keys[name] = association.primary_key_name
63
+ end
64
+ end
65
+
66
+ # Convert constraints that are provided as ActiveRecord::Base objects into their corresponding lookup values
67
+ #
68
+ # Only works for <tt>belongs_to</tt> relationships.
69
+ #
70
+ # For example, :car => <#Car> might get translated into :car_id => 44 or :car_id => 'JHK123' if :primary_key option is given.
71
+ def association_lookup_value(model, name, value)
72
+ association = model.reflect_on_association name
73
+ primary_key = association.instance_variable_get(:@options)[:primary_key]
74
+ if primary_key.blank?
75
+ value.to_param
76
+ else
77
+ value.send primary_key
62
78
  end
63
79
  end
64
80
  end
data/test/helper.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  require 'rubygems'
2
2
  require 'test/unit'
3
3
  require 'shoulda'
4
- require 'ruby-debug'
5
4
  require 'logger'
6
5
 
7
6
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
data/test/test_cohort.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class TestCohortScope < Test::Unit::TestCase
3
+ class TestCohort < Test::Unit::TestCase
4
4
  def setup
5
5
  Citizen.minimum_cohort_size = 3
6
6
  @date_range = (Date.parse('1980-01-01')..Date.parse('1990-01-01'))
@@ -30,15 +30,28 @@ class TestCohortScope < Test::Unit::TestCase
30
30
  end
31
31
  end
32
32
 
33
- context '.association_primary_key' do
34
- should 'include constraints that are models related by a primary key' do
33
+ context '.association_foreign_key' do
34
+ should 'include constraints that are models related by a standard foreign key' do
35
35
  gob = Resident.find_by_name('Gob')
36
- key = CohortScope::Cohort.association_primary_key Resident, :house
36
+ key = CohortScope::Cohort.association_foreign_key Resident, :house
37
37
  assert_equal 'resident_id', key
38
38
  end
39
- should 'include constraints that are models related by a non-primary key' do
40
- key = CohortScope::Cohort.association_primary_key House, :style
39
+ should 'include constraints that are models related by a non-standard foreign key' do
40
+ key = CohortScope::Cohort.association_foreign_key House, :style
41
41
  assert_equal 'period', key
42
42
  end
43
43
  end
44
+
45
+ context '.association_lookup_value' do
46
+ should 'include constraints that are models related by a standard foreign key' do
47
+ gob = Resident.find_by_name('Gob')
48
+ lookup = CohortScope::Cohort.association_lookup_value Resident, :house, gob
49
+ assert_equal gob.to_param, lookup
50
+ end
51
+ should 'include constraints that are models related by a non-standard foreign key key' do
52
+ rev = Style.find_by_name 'classical revival'
53
+ lookup = CohortScope::Cohort.association_lookup_value House, :style, rev
54
+ assert_equal 'arts and crafts', lookup
55
+ end
56
+ end
44
57
  end
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cohort_scope
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
7
  - 1
9
- - 0
10
- version: 0.1.0
8
+ - 1
9
+ version: 0.1.1
11
10
  platform: ruby
12
11
  authors:
13
12
  - Seamus Abshere
@@ -17,7 +16,7 @@ autorequire:
17
16
  bindir: bin
18
17
  cert_chain: []
19
18
 
20
- date: 2010-10-15 00:00:00 -04:00
19
+ date: 2010-10-29 00:00:00 -04:00
21
20
  default_executable:
22
21
  dependencies:
23
22
  - !ruby/object:Gem::Dependency
@@ -28,7 +27,6 @@ dependencies:
28
27
  requirements:
29
28
  - - ">="
30
29
  - !ruby/object:Gem::Version
31
- hash: -1848230024
32
30
  segments:
33
31
  - 3
34
32
  - 0
@@ -45,7 +43,6 @@ dependencies:
45
43
  requirements:
46
44
  - - ">="
47
45
  - !ruby/object:Gem::Version
48
- hash: -1848230024
49
46
  segments:
50
47
  - 3
51
48
  - 0
@@ -62,7 +59,6 @@ dependencies:
62
59
  requirements:
63
60
  - - ">="
64
61
  - !ruby/object:Gem::Version
65
- hash: 33
66
62
  segments:
67
63
  - 2
68
64
  - 10
@@ -78,7 +74,6 @@ dependencies:
78
74
  requirements:
79
75
  - - ">="
80
76
  - !ruby/object:Gem::Version
81
- hash: 3
82
77
  segments:
83
78
  - 0
84
79
  version: "0"
@@ -122,7 +117,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
122
117
  requirements:
123
118
  - - ">="
124
119
  - !ruby/object:Gem::Version
125
- hash: 3
126
120
  segments:
127
121
  - 0
128
122
  version: "0"
@@ -131,7 +125,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
125
  requirements:
132
126
  - - ">="
133
127
  - !ruby/object:Gem::Version
134
- hash: 3
135
128
  segments:
136
129
  - 0
137
130
  version: "0"
@@ -143,6 +136,6 @@ signing_key:
143
136
  specification_version: 3
144
137
  summary: Provides cohorts (in the form of ActiveRecord scopes) that dynamically widen until they contain a certain number of records.
145
138
  test_files:
146
- - test/helper.rb
147
139
  - test/test_cohort.rb
148
140
  - test/test_cohort_scope.rb
141
+ - test/helper.rb