census 0.4.0 → 0.4.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.
- data/README.rdoc +19 -0
- data/VERSION +1 -1
- data/lib/census/user.rb +8 -0
- data/lib/census/user_data.rb +53 -0
- data/test/models/answer_test.rb +0 -7
- data/test/models/question_test.rb +0 -38
- data/test/models/user_test.rb +38 -0
- data/test/rails_root/config/initializers/census.rb +11 -6
- data/test/rails_root/db/migrate/{20100416173354_create_census_tables.rb → 20100421154808_create_census_tables.rb} +0 -0
- data/test/rails_root/db/schema.rb +1 -1
- metadata +5 -4
data/README.rdoc
CHANGED
@@ -84,6 +84,25 @@ to render the partial:
|
|
84
84
|
|
85
85
|
<%= render 'census/user_answers', :user => @user %>
|
86
86
|
|
87
|
+
== Accessing a User's Answers
|
88
|
+
|
89
|
+
From your User object, you can get to that user's answers using methods that
|
90
|
+
are automatically generated for each of your data groups and questions. For
|
91
|
+
example, let's assume you have a data group called "Physical Attributes" with
|
92
|
+
questions for "Weight" and "Hair Color." You can retrieve a user's answers
|
93
|
+
like this:
|
94
|
+
|
95
|
+
user.census_data.physical_attributes.weight
|
96
|
+
user.census_data.physical_attributes.hair_color
|
97
|
+
|
98
|
+
If your data group and question names change frequently, or if you've exposed
|
99
|
+
the census admin UI so that the questions can be changed by your users, you'll
|
100
|
+
probably want to avoid relying on the auto-generated method names. Instead,
|
101
|
+
you can access the user's answers using strings, like this:
|
102
|
+
|
103
|
+
user.census_data['Physical Attributes']['Weight']
|
104
|
+
user.census_data['Physical Attributes']['Hair Color']
|
105
|
+
|
87
106
|
== Searching
|
88
107
|
|
89
108
|
Census provides a search interface for finding users whose answers match a list
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.1
|
data/lib/census/user.rb
CHANGED
@@ -67,6 +67,14 @@ module Census
|
|
67
67
|
def answer_for_choice(choice)
|
68
68
|
answers.select {|a| a.question == choice.question && a.data == choice.value}.first || answers.build(:question => choice.question, :data => '')
|
69
69
|
end
|
70
|
+
|
71
|
+
#
|
72
|
+
# Returns a Census::UserData object that can be used to retrieve this user's
|
73
|
+
# answers.
|
74
|
+
#
|
75
|
+
def census_data
|
76
|
+
@census_data ||= Census::UserData.new(self)
|
77
|
+
end
|
70
78
|
|
71
79
|
private
|
72
80
|
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Census
|
2
|
+
class UserData
|
3
|
+
|
4
|
+
def initialize(user, data_group = nil)
|
5
|
+
@user = user
|
6
|
+
|
7
|
+
if data_group
|
8
|
+
@questions = {}
|
9
|
+
define_question_methods(data_group)
|
10
|
+
else
|
11
|
+
@data_groups = {}
|
12
|
+
define_data_group_methods
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def [](key)
|
17
|
+
if @data_groups
|
18
|
+
@data_groups[key]
|
19
|
+
else
|
20
|
+
@user.first_answer_for(@questions[key]).formatted_data
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
|
28
|
+
def define_data_group_methods
|
29
|
+
DataGroup.all.each do |group|
|
30
|
+
@data_groups[group.name] = Census::UserData.new(@user, group)
|
31
|
+
|
32
|
+
(class << self; self; end).class_eval do
|
33
|
+
define_method group.name.parameterize.underscore do
|
34
|
+
@data_groups[group.name]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def define_question_methods(data_group)
|
41
|
+
data_group.questions.each do |question|
|
42
|
+
@questions[question.prompt] = question
|
43
|
+
|
44
|
+
(class << self; self; end).class_eval do
|
45
|
+
define_method question.prompt.parameterize.underscore do
|
46
|
+
@user.first_answer_for(question).formatted_data
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
data/test/models/answer_test.rb
CHANGED
@@ -29,13 +29,6 @@ class AnswerTest < ActiveSupport::TestCase
|
|
29
29
|
a = Factory(:answer, :question => Factory(:question, :data_type => 'Number'), :data => '5389')
|
30
30
|
assert_equal 5389, a.formatted_data
|
31
31
|
end
|
32
|
-
|
33
|
-
should "format booleans" do
|
34
|
-
a = Factory(:answer, :question => Factory(:question, :data_type => 'Yes/No'), :data => '0')
|
35
|
-
assert_equal false, a.formatted_data
|
36
|
-
a.data = '1'
|
37
|
-
assert_equal true, a.formatted_data
|
38
|
-
end
|
39
32
|
|
40
33
|
end
|
41
34
|
|
@@ -168,44 +168,6 @@ class QuestionTest < ActiveSupport::TestCase
|
|
168
168
|
|
169
169
|
end
|
170
170
|
|
171
|
-
context "with Yes/No data type" do
|
172
|
-
|
173
|
-
setup do
|
174
|
-
@question.data_type = 'Yes/No'
|
175
|
-
end
|
176
|
-
|
177
|
-
should "return a boolean sql transform" do
|
178
|
-
assert_equal "CAST(? AS CHAR)", @question.sql_transform
|
179
|
-
end
|
180
|
-
|
181
|
-
context "with answers" do
|
182
|
-
|
183
|
-
setup do
|
184
|
-
@answer1 = Factory(:answer, :question => @question)
|
185
|
-
@answer1.update_attribute(:data, false)
|
186
|
-
@answer2 = Factory(:answer, :question => @question, :data => true)
|
187
|
-
end
|
188
|
-
|
189
|
-
should "find answers matching true" do
|
190
|
-
assert @question.find_answers_matching(true).include?(@answer2)
|
191
|
-
end
|
192
|
-
|
193
|
-
should "not find answers not matching true" do
|
194
|
-
assert !@question.find_answers_matching(true).include?(@answer1)
|
195
|
-
end
|
196
|
-
|
197
|
-
should "find answers matching false" do
|
198
|
-
assert @question.find_answers_matching(false).include?(@answer1)
|
199
|
-
end
|
200
|
-
|
201
|
-
should "not find answers not matching false" do
|
202
|
-
assert !@question.find_answers_matching(false).include?(@answer2)
|
203
|
-
end
|
204
|
-
|
205
|
-
end
|
206
|
-
|
207
|
-
end
|
208
|
-
|
209
171
|
end
|
210
172
|
|
211
173
|
end
|
data/test/models/user_test.rb
CHANGED
@@ -6,4 +6,42 @@ class UserTest < ActiveSupport::TestCase
|
|
6
6
|
should_accept_nested_attributes_for :answers
|
7
7
|
should_allow_mass_assignment_of :answers_attributes
|
8
8
|
|
9
|
+
context 'Looking up answers' do
|
10
|
+
|
11
|
+
setup do
|
12
|
+
@data_group = Factory(:data_group, :name => 'Physical Attributes')
|
13
|
+
@question1 = Factory(:question, :prompt => 'Hair Color', :data_group => @data_group)
|
14
|
+
@question2 = Factory(:question, :prompt => 'Weight', :data_type => 'Number', :data_group => @data_group)
|
15
|
+
|
16
|
+
@user = Factory(:user)
|
17
|
+
@user.answers << Factory(:answer, :question => @question1, :data => 'Brown')
|
18
|
+
@user.answers << Factory(:answer, :question => @question2, :data => '150')
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'using auto-generated methods' do
|
22
|
+
|
23
|
+
should 'return string values for string data types' do
|
24
|
+
assert_equal 'Brown', @user.census_data.physical_attributes.hair_color
|
25
|
+
end
|
26
|
+
|
27
|
+
should 'return integer values for numeric data types' do
|
28
|
+
assert_equal 150, @user.census_data.physical_attributes.weight
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'using data group and question strings' do
|
34
|
+
|
35
|
+
should 'return string values for string data types' do
|
36
|
+
assert_equal 'Brown', @user.census_data['Physical Attributes']['Hair Color']
|
37
|
+
end
|
38
|
+
|
39
|
+
should 'return integer values for numeric data types' do
|
40
|
+
assert_equal 150, @user.census_data['Physical Attributes']['Weight']
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
9
47
|
end
|
@@ -6,6 +6,17 @@ Census.configure do |config|
|
|
6
6
|
# config.admin_role = 'current_user.admin?'
|
7
7
|
end
|
8
8
|
|
9
|
+
#
|
10
|
+
# Define data types. All data is stored in the database as strings, and you can
|
11
|
+
# provide procs that will be used to convert from the stored strings to any data
|
12
|
+
# type you need.
|
13
|
+
#
|
14
|
+
# :sql_transform - should return a SQL fragment that will be used for comparing
|
15
|
+
# data when doing search queries
|
16
|
+
# :format_data - is used to convert data from its string representation
|
17
|
+
# :validate_data - is used to validate form field submissions, and should return
|
18
|
+
# nil if the data is valid and an error message if it is invalid
|
19
|
+
#
|
9
20
|
Census::DataType.define("String")
|
10
21
|
|
11
22
|
Census::DataType.define(
|
@@ -14,9 +25,3 @@ Census::DataType.define(
|
|
14
25
|
:format_data => lambda {|data| data.to_i unless data.blank? },
|
15
26
|
:validate_data => lambda {|data| "must be a number" unless data =~ /^\d*$/}
|
16
27
|
)
|
17
|
-
|
18
|
-
Census::DataType.define(
|
19
|
-
"Yes/No",
|
20
|
-
:sql_transform => lambda {|column_name| "CAST(#{column_name} AS CHAR)"},
|
21
|
-
:format_data => lambda {|data| %w(1 T t Y y).include?(data) unless data.blank? }
|
22
|
-
)
|
File without changes
|
@@ -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 =>
|
12
|
+
ActiveRecord::Schema.define(:version => 20100421154808) do
|
13
13
|
|
14
14
|
create_table "answers", :force => true do |t|
|
15
15
|
t.integer "question_id"
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 4
|
8
|
-
-
|
9
|
-
version: 0.4.
|
8
|
+
- 1
|
9
|
+
version: 0.4.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Mark Kendall
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-04-
|
17
|
+
date: 2010-04-21 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -116,6 +116,7 @@ files:
|
|
116
116
|
- lib/census/data_type.rb
|
117
117
|
- lib/census/search.rb
|
118
118
|
- lib/census/user.rb
|
119
|
+
- lib/census/user_data.rb
|
119
120
|
- rails/init.rb
|
120
121
|
- shoulda_macros/census.rb
|
121
122
|
has_rdoc: true
|
@@ -171,7 +172,7 @@ test_files:
|
|
171
172
|
- test/rails_root/config/initializers/new_rails_defaults.rb
|
172
173
|
- test/rails_root/config/initializers/session_store.rb
|
173
174
|
- test/rails_root/config/routes.rb
|
174
|
-
- test/rails_root/db/migrate/
|
175
|
+
- test/rails_root/db/migrate/20100421154808_create_census_tables.rb
|
175
176
|
- test/rails_root/db/schema.rb
|
176
177
|
- test/rails_root/test/factories/census.rb
|
177
178
|
- test/rails_root/test/performance/browsing_test.rb
|