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 CHANGED
@@ -1 +1 @@
1
- 0.4.2
1
+ 0.4.3
@@ -1,6 +1,8 @@
1
1
  class Choice < ActiveRecord::Base
2
2
 
3
3
  belongs_to :question, :inverse_of => :choices
4
+
5
+ acts_as_list
4
6
  default_scope :order => :position
5
7
 
6
8
  validates_presence_of :value,
@@ -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 'Enter your response'
22
+ question.prompt { Factory.next :question }
19
23
  question.multiple false
20
24
  question.other false
21
25
  end
@@ -3,43 +3,77 @@ module Census
3
3
 
4
4
  def initialize(user, data_group = nil)
5
5
  @user = user
6
-
7
- if data_group
8
- @questions = {}
9
- define_question_methods(data_group)
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 = Hash.new { |h,v| h[v] = {} }
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
- @data_groups[key]
26
+ find_data_group(key)
19
27
  else
20
- @user.first_answer_for(@questions[key]).formatted_data if @questions[key]
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
- @data_groups[key] = value
35
+ raise ArgumentError, "Can't be invoked on a Data Group"
27
36
  else
28
- @user.first_answer_for(@questions[key]).update_attribute(:data, @questions[key].format_data(value).to_s) if @questions[key]
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[group.name] = Census::UserData.new(@user, group)
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
- @data_groups[group.name]
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[question.prompt] = question
84
+ @questions << question
51
85
 
52
86
  (class << self; self; end).class_eval do
53
87
  define_method question.prompt.parameterize.underscore do
@@ -17,6 +17,8 @@ class ChoiceTest < ActiveSupport::TestCase
17
17
 
18
18
  should_allow_mass_assignment_of :value
19
19
 
20
+ should_act_as_list
21
+
20
22
  end
21
23
 
22
24
  end
@@ -23,6 +23,8 @@ class QuestionTest < ActiveSupport::TestCase
23
23
  :other
24
24
 
25
25
  should_accept_nested_attributes_for :choices
26
+
27
+ should_act_as_list
26
28
 
27
29
  should "return a default sql transform" do
28
30
  assert '"?"', @question.sql_transform
@@ -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
@@ -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 => 20100526201135) do
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 'Enter your response'
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: 11
4
+ hash: 9
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 2
10
- version: 0.4.2
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-05-26 00:00:00 -04:00
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/20100526201135_create_census_tables.rb
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/20100526201135_create_census_tables.rb
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