census 0.4.3 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.3
1
+ 0.4.4
@@ -25,8 +25,7 @@ module Census
25
25
  if @data_groups
26
26
  find_data_group(key)
27
27
  else
28
- question = find_question(key)
29
- @user.first_answer_for(question).formatted_data if question
28
+ answer_for(find_question(key))
30
29
  end
31
30
  end
32
31
 
@@ -34,8 +33,7 @@ module Census
34
33
  if @data_groups
35
34
  raise ArgumentError, "Can't be invoked on a Data Group"
36
35
  else
37
- question = find_question(key)
38
- @user.first_answer_for(question).update_attribute(:data, question.format_data(value).to_s) if question
36
+ set_answer_for(find_question(key), value)
39
37
  end
40
38
  end
41
39
 
@@ -66,6 +64,27 @@ module Census
66
64
  def find_question(prompt)
67
65
  @questions.select {|question| question.prompt == prompt}.first
68
66
  end
67
+
68
+ def answer_for(question)
69
+ if question
70
+ if question.multiple?
71
+ @user.all_answers_for(question).map(&:formatted_data)
72
+ else
73
+ @user.first_answer_for(question).formatted_data
74
+ end
75
+ end
76
+ end
77
+
78
+ def set_answer_for(question, value)
79
+ if question
80
+ if question.multiple? && value.kind_of?(Array)
81
+ @user.all_answers_for(question).each { |a| a.destroy }
82
+ value.each { |v| @user.answers.build(:question => question, :data => v) }
83
+ else
84
+ @user.first_answer_for(question).update_attribute(:data, question.format_data(value).to_s)
85
+ end
86
+ end
87
+ end
69
88
 
70
89
  def define_data_group_methods
71
90
  DataGroup.all.each do |group|
@@ -85,11 +104,11 @@ module Census
85
104
 
86
105
  (class << self; self; end).class_eval do
87
106
  define_method question.prompt.parameterize.underscore do
88
- @user.first_answer_for(question).formatted_data
107
+ answer_for(question)
89
108
  end
90
109
 
91
110
  define_method "#{question.prompt.parameterize.underscore}=" do |value|
92
- @user.first_answer_for(question).update_attribute(:data, question.format_data(value).to_s)
111
+ set_answer_for(question, value)
93
112
  end
94
113
  end
95
114
  end
@@ -12,10 +12,18 @@ class UserTest < ActiveSupport::TestCase
12
12
  @data_group = Factory(:data_group, :name => 'Physical Attributes')
13
13
  @question1 = Factory(:question, :prompt => 'Hair Color', :data_group => @data_group)
14
14
  @question2 = Factory(:question, :prompt => 'Weight', :data_type => 'Number', :data_group => @data_group)
15
+ @question3 = Factory(:question, :prompt => 'Favorite Color', :multiple => true, :data_group => @data_group)
16
+ @red = Factory(:choice, :question => @question3, :value => 'Red')
17
+ @green = Factory(:choice, :question => @question3, :value => 'Green')
18
+ @blue = Factory(:choice, :question => @question3, :value => 'Blue')
19
+ @yellow = Factory(:choice, :question => @question3, :value => 'Yellow')
15
20
 
16
21
  @user = Factory(:user)
17
- @user.answers << Factory(:answer, :question => @question1, :data => 'Brown')
18
- @user.answers << Factory(:answer, :question => @question2, :data => '150')
22
+ Factory(:answer, :question => @question1, :data => 'Brown', :user => @user)
23
+ Factory(:answer, :question => @question2, :data => '150', :user => @user)
24
+ Factory(:answer, :question => @question3, :data => 'Blue', :user => @user)
25
+ Factory(:answer, :question => @question3, :data => 'Green', :user => @user)
26
+ @user.reload
19
27
  end
20
28
 
21
29
  context 'using auto-generated methods' do
@@ -27,6 +35,10 @@ class UserTest < ActiveSupport::TestCase
27
35
  should 'return integer values for numeric data types' do
28
36
  assert_equal 150, @user.census_data.physical_attributes.weight
29
37
  end
38
+
39
+ should 'return an array of values for questions with multiple answers' do
40
+ assert_equal ['Blue', 'Green'], @user.census_data.physical_attributes.favorite_color
41
+ end
30
42
 
31
43
  end
32
44
 
@@ -40,6 +52,10 @@ class UserTest < ActiveSupport::TestCase
40
52
  assert_equal 150, @user.census_data['Physical Attributes']['Weight']
41
53
  end
42
54
 
55
+ should 'return an array of values for questions with multiple answers' do
56
+ assert_equal ['Blue', 'Green'], @user.census_data['Physical Attributes']['Favorite Color']
57
+ end
58
+
43
59
  end
44
60
 
45
61
  context 'using expose_census_data' do
@@ -47,6 +63,7 @@ class UserTest < ActiveSupport::TestCase
47
63
  setup do
48
64
  @user.class.expose_census_data('Physical Attributes', 'Hair Color', :hair_color)
49
65
  @user.class.expose_census_data('Physical Attributes', 'Weight', :weight)
66
+ @user.class.expose_census_data('Physical Attributes', 'Favorite Color', :favorite_color)
50
67
  end
51
68
 
52
69
  should 'return string values for string data types' do
@@ -57,6 +74,10 @@ class UserTest < ActiveSupport::TestCase
57
74
  assert_equal 150, @user.weight
58
75
  end
59
76
 
77
+ should 'return an array of values for questions with multiple answers' do
78
+ assert_equal ['Blue', 'Green'], @user.favorite_color
79
+ end
80
+
60
81
  end
61
82
 
62
83
  context 'using each_pair' do
@@ -70,9 +91,10 @@ class UserTest < ActiveSupport::TestCase
70
91
 
71
92
  should 'retrieve pairs of questions and answers when called for a data group' do
72
93
  @user.census_data['Physical Attributes'].each_pair do |key, value|
73
- assert_contains ['Hair Color', 'Weight'], key
94
+ assert_contains ['Hair Color', 'Weight', 'Favorite Color'], key
74
95
  assert_equal 'Brown', value if key == 'Hair Color'
75
96
  assert_equal 150, value if key == 'Weight'
97
+ assert_equal ['Blue', 'Green'], value if key == 'Favorite Color'
76
98
  end
77
99
  end
78
100
 
@@ -86,6 +108,11 @@ class UserTest < ActiveSupport::TestCase
86
108
  @data_group = Factory(:data_group, :name => 'Physical Attributes')
87
109
  @question1 = Factory(:question, :prompt => 'Hair Color', :data_group => @data_group)
88
110
  @question2 = Factory(:question, :prompt => 'Weight', :data_type => 'Number', :data_group => @data_group)
111
+ @question3 = Factory(:question, :prompt => 'Favorite Color', :multiple => true, :data_group => @data_group)
112
+ @red = Factory(:choice, :question => @question3, :value => 'Red')
113
+ @green = Factory(:choice, :question => @question3, :value => 'Green')
114
+ @blue = Factory(:choice, :question => @question3, :value => 'Blue')
115
+ @yellow = Factory(:choice, :question => @question3, :value => 'Yellow')
89
116
 
90
117
  @user = Factory(:user)
91
118
  end
@@ -101,6 +128,11 @@ class UserTest < ActiveSupport::TestCase
101
128
  @user.census_data.physical_attributes.weight = 210
102
129
  assert_equal 210, @user.first_answer_for(@question2).formatted_data
103
130
  end
131
+
132
+ should 'accept arrays for questions that allow multiple answers' do
133
+ @user.census_data.physical_attributes.favorite_color = ['Yellow', 'Red']
134
+ assert_equal ['Yellow', 'Red'], @user.all_answers_for(@question3).map(&:formatted_data)
135
+ end
104
136
 
105
137
  end
106
138
 
@@ -116,6 +148,11 @@ class UserTest < ActiveSupport::TestCase
116
148
  assert_equal 210, @user.first_answer_for(@question2).formatted_data
117
149
  end
118
150
 
151
+ should 'accept arrays for questions that allow multiple answers' do
152
+ @user.census_data["Physical Attributes"]["Favorite Color"] = ['Yellow', 'Red']
153
+ assert_equal ['Yellow', 'Red'], @user.all_answers_for(@question3).map(&:formatted_data)
154
+ end
155
+
119
156
  end
120
157
 
121
158
  context 'using expose_census_data' do
@@ -123,6 +160,7 @@ class UserTest < ActiveSupport::TestCase
123
160
  setup do
124
161
  @user.class.expose_census_data('Physical Attributes', 'Hair Color', :hair_color)
125
162
  @user.class.expose_census_data('Physical Attributes', 'Weight', :weight)
163
+ @user.class.expose_census_data('Physical Attributes', 'Favorite Color', :favorite_color)
126
164
  end
127
165
 
128
166
  should 'allow string values for string data types' do
@@ -135,6 +173,11 @@ class UserTest < ActiveSupport::TestCase
135
173
  assert_equal 210, @user.first_answer_for(@question2).formatted_data
136
174
  end
137
175
 
176
+ should 'accept arrays for questions that allow multiple answers' do
177
+ @user.favorite_color = ['Yellow', 'Red']
178
+ assert_equal ['Yellow', 'Red'], @user.all_answers_for(@question3).map(&:formatted_data)
179
+ end
180
+
138
181
  end
139
182
 
140
183
  end
@@ -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 => 20100602175828) do
12
+ ActiveRecord::Schema.define(:version => 20100630131015) do
13
13
 
14
14
  create_table "answers", :force => true do |t|
15
15
  t.integer "question_id"
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: 9
4
+ hash: 7
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 3
10
- version: 0.4.3
9
+ - 4
10
+ version: 0.4.4
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-02 00:00:00 -04:00
18
+ date: 2010-06-30 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/20100602175828_create_census_tables.rb
154
+ - test/rails_root/db/migrate/20100630131015_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/20100602175828_create_census_tables.rb
218
+ - test/rails_root/db/migrate/20100630131015_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