census 0.4.2 → 0.4.3
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/VERSION +1 -1
- data/app/models/choice.rb +2 -0
- data/app/models/question.rb +3 -1
- data/generators/census/templates/factories.rb +5 -1
- data/lib/census/user_data.rb +46 -12
- data/test/models/choice_test.rb +2 -0
- data/test/models/question_test.rb +2 -0
- data/test/models/user_test.rb +40 -0
- data/test/rails_root/db/migrate/{20100526201135_create_census_tables.rb → 20100602175828_create_census_tables.rb} +0 -0
- data/test/rails_root/db/schema.rb +1 -1
- data/test/rails_root/test/factories/census.rb +5 -1
- metadata +6 -6
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.3
|
data/app/models/choice.rb
CHANGED
data/app/models/question.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
class Question < ActiveRecord::Base
|
2
2
|
|
3
3
|
belongs_to :data_group, :inverse_of => :questions
|
4
|
+
|
5
|
+
acts_as_list
|
4
6
|
default_scope :order => :position
|
5
7
|
|
6
8
|
has_many :choices, :dependent => :destroy, :inverse_of => :question
|
@@ -10,7 +12,7 @@ class Question < ActiveRecord::Base
|
|
10
12
|
|
11
13
|
validates_presence_of :prompt,
|
12
14
|
:data_group
|
13
|
-
|
15
|
+
|
14
16
|
def sql_transform(column_name = '?')
|
15
17
|
data_type_definition.sql_transform.call(column_name)
|
16
18
|
end
|
@@ -1,3 +1,7 @@
|
|
1
|
+
Factory.sequence :question do |n|
|
2
|
+
"Question #{n}"
|
3
|
+
end
|
4
|
+
|
1
5
|
Factory.define :answer do |answer|
|
2
6
|
answer.association :question
|
3
7
|
answer.association :user
|
@@ -15,7 +19,7 @@ end
|
|
15
19
|
|
16
20
|
Factory.define :question do |question|
|
17
21
|
question.association :data_group
|
18
|
-
question.prompt
|
22
|
+
question.prompt { Factory.next :question }
|
19
23
|
question.multiple false
|
20
24
|
question.other false
|
21
25
|
end
|
data/lib/census/user_data.rb
CHANGED
@@ -3,43 +3,77 @@ module Census
|
|
3
3
|
|
4
4
|
def initialize(user, data_group = nil)
|
5
5
|
@user = user
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
@data_group = data_group
|
7
|
+
reload
|
8
|
+
end
|
9
|
+
|
10
|
+
def reload
|
11
|
+
if @data_group
|
12
|
+
@questions = []
|
13
|
+
define_question_methods(@data_group)
|
10
14
|
else
|
11
|
-
@data_groups =
|
15
|
+
@data_groups = []
|
12
16
|
define_data_group_methods
|
13
17
|
end
|
14
18
|
end
|
15
19
|
|
20
|
+
def name
|
21
|
+
@data_group.name if @data_group
|
22
|
+
end
|
23
|
+
|
16
24
|
def [](key)
|
17
25
|
if @data_groups
|
18
|
-
|
26
|
+
find_data_group(key)
|
19
27
|
else
|
20
|
-
|
28
|
+
question = find_question(key)
|
29
|
+
@user.first_answer_for(question).formatted_data if question
|
21
30
|
end
|
22
31
|
end
|
23
32
|
|
24
33
|
def []=(key, value)
|
25
34
|
if @data_groups
|
26
|
-
|
35
|
+
raise ArgumentError, "Can't be invoked on a Data Group"
|
27
36
|
else
|
28
|
-
|
37
|
+
question = find_question(key)
|
38
|
+
@user.first_answer_for(question).update_attribute(:data, question.format_data(value).to_s) if question
|
29
39
|
end
|
30
40
|
end
|
41
|
+
|
42
|
+
def each_pair
|
43
|
+
self.keys.each{ |key| yield key, self.send(key.parameterize.underscore.to_sym) }
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
protected
|
48
|
+
|
31
49
|
|
50
|
+
def keys
|
51
|
+
if @data_groups
|
52
|
+
@data_groups.map(&:name)
|
53
|
+
else
|
54
|
+
@questions.map(&:prompt)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
32
58
|
|
33
59
|
private
|
60
|
+
|
34
61
|
|
62
|
+
def find_data_group(name)
|
63
|
+
@data_groups.select {|group| group.name == name}.first
|
64
|
+
end
|
35
65
|
|
66
|
+
def find_question(prompt)
|
67
|
+
@questions.select {|question| question.prompt == prompt}.first
|
68
|
+
end
|
69
|
+
|
36
70
|
def define_data_group_methods
|
37
71
|
DataGroup.all.each do |group|
|
38
|
-
@data_groups
|
72
|
+
@data_groups << Census::UserData.new(@user, group)
|
39
73
|
|
40
74
|
(class << self; self; end).class_eval do
|
41
75
|
define_method group.name.parameterize.underscore do
|
42
|
-
|
76
|
+
find_data_group(group.name)
|
43
77
|
end
|
44
78
|
end
|
45
79
|
end
|
@@ -47,7 +81,7 @@ module Census
|
|
47
81
|
|
48
82
|
def define_question_methods(data_group)
|
49
83
|
data_group.questions.each do |question|
|
50
|
-
@questions
|
84
|
+
@questions << question
|
51
85
|
|
52
86
|
(class << self; self; end).class_eval do
|
53
87
|
define_method question.prompt.parameterize.underscore do
|
data/test/models/choice_test.rb
CHANGED
data/test/models/user_test.rb
CHANGED
@@ -58,6 +58,25 @@ class UserTest < ActiveSupport::TestCase
|
|
58
58
|
end
|
59
59
|
|
60
60
|
end
|
61
|
+
|
62
|
+
context 'using each_pair' do
|
63
|
+
|
64
|
+
should 'retrieve data groups and their question lists when called directly on census_data' do
|
65
|
+
@user.census_data.each_pair do |key, value|
|
66
|
+
assert_contains ['Physical Attributes'], key
|
67
|
+
assert value.kind_of?(Census::UserData)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
should 'retrieve pairs of questions and answers when called for a data group' do
|
72
|
+
@user.census_data['Physical Attributes'].each_pair do |key, value|
|
73
|
+
assert_contains ['Hair Color', 'Weight'], key
|
74
|
+
assert_equal 'Brown', value if key == 'Hair Color'
|
75
|
+
assert_equal 150, value if key == 'Weight'
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
61
80
|
|
62
81
|
end
|
63
82
|
|
@@ -119,5 +138,26 @@ class UserTest < ActiveSupport::TestCase
|
|
119
138
|
end
|
120
139
|
|
121
140
|
end
|
141
|
+
|
142
|
+
context 'Enumerating answers with each_pair' do
|
143
|
+
|
144
|
+
setup do
|
145
|
+
@data_group = Factory(:data_group)
|
146
|
+
10.times {|i| Factory(:question, :prompt => i.to_s, :data_group => @data_group)}
|
147
|
+
@user = Factory(:user)
|
148
|
+
end
|
149
|
+
|
150
|
+
should 'maintain the correct order for questions' do
|
151
|
+
assert_equal ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"],
|
152
|
+
@user.census_data[@data_group.name].each_pair {|key, value| key}
|
153
|
+
|
154
|
+
@data_group.questions.first.move_to_bottom
|
155
|
+
@user.census_data.reload
|
156
|
+
|
157
|
+
assert_equal ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"],
|
158
|
+
@user.census_data[@data_group.name].each_pair {|key, value| key}
|
159
|
+
end
|
160
|
+
|
161
|
+
end
|
122
162
|
|
123
163
|
end
|
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 => 20100602175828) do
|
13
13
|
|
14
14
|
create_table "answers", :force => true do |t|
|
15
15
|
t.integer "question_id"
|
@@ -1,3 +1,7 @@
|
|
1
|
+
Factory.sequence :question do |n|
|
2
|
+
"Question #{n}"
|
3
|
+
end
|
4
|
+
|
1
5
|
Factory.define :answer do |answer|
|
2
6
|
answer.association :question
|
3
7
|
answer.association :user
|
@@ -15,7 +19,7 @@ end
|
|
15
19
|
|
16
20
|
Factory.define :question do |question|
|
17
21
|
question.association :data_group
|
18
|
-
question.prompt
|
22
|
+
question.prompt { Factory.next :question }
|
19
23
|
question.multiple false
|
20
24
|
question.other false
|
21
25
|
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:
|
4
|
+
hash: 9
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 4
|
9
|
-
-
|
10
|
-
version: 0.4.
|
9
|
+
- 3
|
10
|
+
version: 0.4.3
|
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-
|
18
|
+
date: 2010-06-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/
|
154
|
+
- test/rails_root/db/migrate/20100602175828_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/
|
218
|
+
- test/rails_root/db/migrate/20100602175828_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
|