census 0.5.0 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -70,7 +70,7 @@ Census provides a partial that you can include in a form that collects answers
70
70
  from your users. This partial assumes it's inside of a form_for(:user) block. It
71
71
  will render fields for each of the questions you've defined.
72
72
 
73
- <%= render 'census/user_answers', :user => @user %>
73
+ <%= render 'census/user_questions', :user => @user %>
74
74
 
75
75
  Data groups are rendered as fieldsets that contain a list of input fields, one
76
76
  for each question. If you take a look at the rendered HTML, you'll see several
@@ -147,6 +147,12 @@ method to query the database and get back the list of matching users.
147
147
 
148
148
  users = search.perform
149
149
 
150
+ == Running the tests
151
+
152
+ In order to run the test suite for Census, first make sure you have all of the dependencies installed. Then, you need create a blank rails app in the test/rails_root folder and add 'shoulda' and 'factory_girl' as gem dependencies for that app's test environment (test/rails_root/config/environments/test.rb).
153
+
154
+ The default rake task runs the test suite for Census.
155
+
150
156
  == Copyright
151
157
 
152
158
  Copyright (c) 2010 Envy Labs LLC. See LICENSE for details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.0
1
+ 0.5.2
@@ -7,5 +7,10 @@ class Choice < ActiveRecord::Base
7
7
 
8
8
  validates_presence_of :value,
9
9
  :question
10
+
11
+ named_scope :from_group, lambda{ |group_name| {
12
+ :conditions => { :group => group_name } }
13
+ }
14
+
10
15
 
11
16
  end
@@ -12,7 +12,7 @@ class Question < ActiveRecord::Base
12
12
 
13
13
  validates_presence_of :prompt,
14
14
  :data_group
15
-
15
+
16
16
  def sql_transform(column_name = '?')
17
17
  data_type_definition.sql_transform.call(column_name)
18
18
  end
@@ -22,6 +22,7 @@ class CreateCensusTables < ActiveRecord::Migration
22
22
  t.integer :question_id
23
23
  t.string :value
24
24
  t.integer :position
25
+ t.string :group
25
26
  t.timestamps
26
27
  end
27
28
 
@@ -22,6 +22,7 @@ class CreateCensusTables < ActiveRecord::Migration
22
22
  t.integer :question_id
23
23
  t.string :value
24
24
  t.integer :position
25
+ t.string :group
25
26
  t.timestamps
26
27
  end
27
28
 
@@ -20,6 +20,7 @@ module Census
20
20
  model.send(:include, InstanceMethods)
21
21
  model.send(:include, Associations)
22
22
  model.send(:include, Callbacks)
23
+ model.send(:include, Scopes)
23
24
  end
24
25
 
25
26
  module Associations
@@ -43,6 +44,28 @@ module Census
43
44
  end
44
45
  end
45
46
 
47
+ module Scopes
48
+ #
49
+ # This scope can be used to find other users who have census answers in common with the
50
+ # given user. The returned list is sorted such that users with the most answers in
51
+ # common are at the beginning of the list.
52
+ #
53
+ def self.included(model)
54
+ model.class_eval do
55
+ named_scope :with_matching_census_answers, lambda { |user, limit|
56
+ {
57
+ :select => "DISTINCT users.*, COUNT(answers.id) AS census_match_score",
58
+ :joins => "LEFT JOIN `answers` AS answers ON answers.user_id = users.id LEFT JOIN `answers` AS other_answers ON other_answers.user_id = #{user.id}",
59
+ :conditions => ['users.id <> ? AND answers.data = other_answers.data AND answers.question_id = other_answers.question_id', user.id],
60
+ :group => 'users.id',
61
+ :order => 'census_match_score DESC',
62
+ :limit => limit
63
+ }
64
+ }
65
+ end
66
+ end
67
+ end
68
+
46
69
  module InstanceMethods
47
70
  #
48
71
  # Returns this user's first answer for the given question, or a new empty
@@ -21,4 +21,32 @@ class ChoiceTest < ActiveSupport::TestCase
21
21
 
22
22
  end
23
23
 
24
+ context 'A Choice with a Group' do
25
+
26
+ setup do
27
+ @movie_question = Factory(:question, :prompt => 'Favorite Movie')
28
+
29
+ @titanic_movie = Factory(:choice, :value => 'Titanic', :group => 'Drama',
30
+ :question => @movie_question)
31
+
32
+ @avatar_movie = Factory(:choice, :value => 'Avatar', :group => 'Adventure',
33
+ :question => @movie_question)
34
+
35
+ @avatar_movie = Factory(:choice, :value => 'The Last Airbender', :group => 'Adventure',
36
+ :question => @movie_question)
37
+ end
38
+
39
+ should 'belong to that group' do
40
+ assert_equal 'Drama', @titanic_movie.group
41
+ assert_equal 'Adventure', @avatar_movie.group
42
+ end
43
+
44
+ should 'return all from a group' do
45
+ assert_equal 1, @movie_question.choices.from_group('Drama').size
46
+ assert_equal 2, @movie_question.choices.from_group('Adventure').size
47
+ assert_equal 0, @movie_question.choices.from_group('Comedy').size
48
+ end
49
+
50
+ end
51
+
24
52
  end
@@ -22,6 +22,7 @@ class CreateCensusTables < ActiveRecord::Migration
22
22
  t.integer :question_id
23
23
  t.string :value
24
24
  t.integer :position
25
+ t.string :group
25
26
  t.timestamps
26
27
  end
27
28
 
@@ -9,7 +9,7 @@
9
9
  #
10
10
  # It's strongly recommended to check this file into your version control system.
11
11
 
12
- ActiveRecord::Schema.define(:version => 20100630131015) do
12
+ ActiveRecord::Schema.define(:version => 20100802152451) do
13
13
 
14
14
  create_table "answers", :force => true do |t|
15
15
  t.integer "question_id"
@@ -24,6 +24,7 @@ ActiveRecord::Schema.define(:version => 20100630131015) do
24
24
  t.integer "question_id"
25
25
  t.string "value"
26
26
  t.integer "position"
27
+ t.string "group"
27
28
  t.datetime "created_at"
28
29
  t.datetime "updated_at"
29
30
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: census
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 5
9
- - 0
10
- version: 0.5.0
9
+ - 2
10
+ version: 0.5.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Mark Kendall
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-06-30 00:00:00 -04:00
18
+ date: 2010-08-02 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -151,7 +151,7 @@ files:
151
151
  - test/rails_root/config/initializers/new_rails_defaults.rb
152
152
  - test/rails_root/config/initializers/session_store.rb
153
153
  - test/rails_root/config/routes.rb
154
- - test/rails_root/db/migrate/20100630131015_create_census_tables.rb
154
+ - test/rails_root/db/migrate/20100802152451_create_census_tables.rb
155
155
  - test/rails_root/db/schema.rb
156
156
  - test/rails_root/test/factories/census.rb
157
157
  - test/rails_root/test/performance/browsing_test.rb
@@ -215,7 +215,7 @@ test_files:
215
215
  - test/rails_root/config/initializers/new_rails_defaults.rb
216
216
  - test/rails_root/config/initializers/session_store.rb
217
217
  - test/rails_root/config/routes.rb
218
- - test/rails_root/db/migrate/20100630131015_create_census_tables.rb
218
+ - test/rails_root/db/migrate/20100802152451_create_census_tables.rb
219
219
  - test/rails_root/db/schema.rb
220
220
  - test/rails_root/test/factories/census.rb
221
221
  - test/rails_root/test/performance/browsing_test.rb