surveyor 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. data/CHANGELOG.md +26 -0
  2. data/README.md +67 -168
  3. data/app/models/survey_translation.rb +5 -0
  4. data/app/views/partials/_answer.html.haml +9 -5
  5. data/app/views/partials/_question.html.haml +3 -3
  6. data/app/views/partials/_question_group.html.haml +9 -8
  7. data/app/views/partials/_section.html.haml +1 -1
  8. data/app/views/surveyor/edit.html.haml +3 -3
  9. data/app/views/surveyor/export.json.rabl +12 -8
  10. data/app/views/surveyor/new.html.haml +9 -14
  11. data/app/views/surveyor/show.html.haml +2 -2
  12. data/doc/surveyor_models.png +0 -0
  13. data/doc/surveyor_models2.png +0 -0
  14. data/features/export_to_json.feature +97 -1
  15. data/features/internationalization.feature +55 -0
  16. data/features/step_definitions/parser_steps.rb +7 -0
  17. data/features/step_definitions/surveyor_steps.rb +43 -6
  18. data/features/step_definitions/ui_steps.rb +22 -0
  19. data/features/support/env.rb +2 -0
  20. data/features/support/hooks.rb +4 -0
  21. data/features/surveyor.feature +117 -1
  22. data/features/surveyor_dependencies.feature +56 -2
  23. data/features/surveyor_parser.feature +10 -10
  24. data/lib/assets/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
  25. data/lib/assets/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
  26. data/lib/assets/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
  27. data/lib/assets/images/ui-bg_glass_75_dadada_1x400.png +0 -0
  28. data/lib/assets/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
  29. data/lib/assets/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
  30. data/lib/assets/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
  31. data/lib/assets/images/{ui-icons_ef8c08_256x240.png → ui-icons_222222_256x240.png} +0 -0
  32. data/lib/assets/images/ui-icons_2e83ff_256x240.png +0 -0
  33. data/lib/assets/images/{ui-icons_ffffff_256x240.png → ui-icons_454545_256x240.png} +0 -0
  34. data/lib/assets/images/ui-icons_888888_256x240.png +0 -0
  35. data/lib/assets/images/ui-icons_cd0a0a_256x240.png +0 -0
  36. data/lib/assets/javascripts/surveyor/jquery-1.9.0.js +9555 -0
  37. data/lib/assets/javascripts/surveyor/jquery-ui-1.10.0.custom.js +14850 -0
  38. data/lib/assets/javascripts/surveyor/jquery-ui-timepicker-addon.js +1795 -1153
  39. data/lib/assets/javascripts/surveyor/jquery.maskedinput.js +338 -0
  40. data/lib/assets/javascripts/surveyor/jquery.surveyor.js +8 -0
  41. data/lib/assets/stylesheets/surveyor.sass +2 -2
  42. data/lib/assets/stylesheets/surveyor/jquery-ui-1.10.0.custom.css +1174 -0
  43. data/lib/assets/stylesheets/surveyor/jquery-ui-timepicker-addon.css +7 -2
  44. data/lib/assets/stylesheets/surveyor/reset.css +24 -26
  45. data/lib/generators/surveyor/install_generator.rb +4 -0
  46. data/lib/generators/surveyor/templates/app/assets/javascripts/surveyor_all.js +4 -3
  47. data/lib/generators/surveyor/templates/app/assets/stylesheets/surveyor_all.css +2 -2
  48. data/lib/generators/surveyor/templates/config/locales/surveyor_en.yml +1 -0
  49. data/lib/generators/surveyor/templates/config/locales/surveyor_he.yml +1 -1
  50. data/lib/generators/surveyor/templates/config/locales/surveyor_ko.yml +18 -0
  51. data/lib/generators/surveyor/templates/db/migrate/add_input_mask_attributes_to_answer.rb +12 -0
  52. data/lib/generators/surveyor/templates/db/migrate/create_survey_translations.rb +19 -0
  53. data/lib/generators/surveyor/templates/surveys/kitchen_sink_survey.rb +5 -0
  54. data/lib/generators/surveyor/templates/surveys/languages.rb +10 -0
  55. data/lib/generators/surveyor/templates/surveys/translations/languages.es.yml +14 -0
  56. data/lib/generators/surveyor/templates/surveys/translations/languages.he.yml +14 -0
  57. data/lib/generators/surveyor/templates/surveys/translations/languages.ko.yml +14 -0
  58. data/lib/surveyor/helpers/formtastic_custom_input.rb +4 -8
  59. data/lib/surveyor/helpers/surveyor_helper_methods.rb +22 -26
  60. data/lib/surveyor/models/answer_methods.rb +41 -10
  61. data/lib/surveyor/models/question_group_methods.rb +17 -3
  62. data/lib/surveyor/models/question_methods.rb +32 -5
  63. data/lib/surveyor/models/response_methods.rb +7 -7
  64. data/lib/surveyor/models/survey_methods.rb +20 -4
  65. data/lib/surveyor/models/survey_section_methods.rb +10 -5
  66. data/lib/surveyor/models/survey_translation_methods.rb +33 -0
  67. data/lib/surveyor/mustache_context.rb +11 -0
  68. data/lib/surveyor/parser.rb +15 -2
  69. data/lib/surveyor/surveyor_controller_methods.rb +19 -17
  70. data/lib/surveyor/version.rb +1 -1
  71. data/spec/controllers/surveyor_controller_spec.rb +3 -3
  72. data/spec/factories.rb +21 -7
  73. data/spec/helpers/formtastic_custom_input_spec.rb +2 -3
  74. data/spec/helpers/surveyor_helper_spec.rb +64 -100
  75. data/spec/models/answer_spec.rb +166 -58
  76. data/spec/models/question_group_spec.rb +84 -38
  77. data/spec/models/question_spec.rb +188 -116
  78. data/spec/models/survey_section_spec.rb +77 -40
  79. data/spec/models/survey_spec.rb +176 -145
  80. data/spec/spec_helper.rb +3 -0
  81. data/stacktests.sh +52 -0
  82. metadata +36 -18
  83. data/lib/assets/images/ui-bg_glass_100_f6f6f6_1x400.png +0 -0
  84. data/lib/assets/images/ui-bg_glass_100_fdf5ce_1x400.png +0 -0
  85. data/lib/assets/images/ui-bg_gloss-wave_35_f6a828_500x100.png +0 -0
  86. data/lib/assets/images/ui-bg_highlight-soft_100_eeeeee_1x100.png +0 -0
  87. data/lib/assets/images/ui-bg_highlight-soft_75_ffe45c_1x100.png +0 -0
  88. data/lib/assets/javascripts/surveyor/jquery-ui.js +0 -11729
  89. data/lib/assets/javascripts/surveyor/jquery.tools.min.js +0 -204
  90. data/lib/assets/stylesheets/surveyor/dateinput.css +0 -149
  91. data/lib/assets/stylesheets/surveyor/jquery-ui.custom.css +0 -572
  92. data/lib/surveyor/render_text.rb +0 -23
  93. data/lib/surveyor/surveyor_admin_controller_methods.rb +0 -55
@@ -1,48 +1,85 @@
1
+ # encoding: UTF-8
1
2
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
3
 
3
- describe SurveySection, "when saving a survey_section" do
4
- before(:each) do
5
- @valid_attributes={:title => "foo", :survey_id => 2, :display_order => 4}
6
- @survey_section = SurveySection.new(@valid_attributes)
7
- end
4
+ describe SurveySection do
5
+ let(:survey_section){ Factory(:survey_section) }
8
6
 
9
- it "should be invalid without title" do
10
- @survey_section.title = nil
11
- @survey_section.should have(1).error_on(:title)
12
- end
13
-
14
- it "should have a parent survey" do
15
- # this causes issues with building and saving
16
- # @survey_section.survey_id = nil
17
- # @survey_section.should have(1).error_on(:survey)
18
- end
19
- it "should protect timestamps" do
20
- saved_attrs = @survey_section.attributes
21
- if defined? ActiveModel::MassAssignmentSecurity::Error
22
- lambda {@survey_section.update_attributes(:created_at => 3.days.ago, :updated_at => 3.hours.ago)}.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
23
- else
24
- @survey_section.attributes = {:created_at => 3.days.ago, :updated_at => 3.hours.ago} # automatically protected by Rails
25
- end
26
- @survey_section.attributes.should == saved_attrs
7
+ context "when creating" do
8
+ it "is invalid without #title" do
9
+ survey_section.title = nil
10
+ survey_section.should have(1).error_on(:title)
11
+ end
12
+ it "protects #api_id" do
13
+ saved_attrs = survey_section.attributes
14
+ if defined? ActiveModel::MassAssignmentSecurity::Error
15
+ expect { survey_section.update_attributes(:api_id => "NEW") }.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
16
+ else
17
+ survey_section.attributes = {:api_id => "NEW"} # Rails doesn't return false, but this will be checked in the comparison to saved_attrs
18
+ end
19
+ survey_section.attributes.should == saved_attrs
20
+ end
21
+ it "protects #created_at" do
22
+ saved_attrs = survey_section.attributes
23
+ if defined? ActiveModel::MassAssignmentSecurity::Error
24
+ expect { survey_section.update_attributes(:created_at => 3.days.ago) }.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
25
+ else
26
+ survey_section.attributes = {:created_at => 3.days.ago} # Rails doesn't return false, but this will be checked in the comparison to saved_attrs
27
+ end
28
+ survey_section.attributes.should == saved_attrs
29
+ end
30
+ it "protects #updated_at" do
31
+ saved_attrs = survey_section.attributes
32
+ if defined? ActiveModel::MassAssignmentSecurity::Error
33
+ expect { survey_section.update_attributes(:updated_at => 3.hours.ago) }.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
34
+ else
35
+ survey_section.attributes = {:updated_at => 3.hours.ago} # Rails doesn't return false, but this will be checked in the comparison to saved_attrs
36
+ end
37
+ survey_section.attributes.should == saved_attrs
38
+ end
27
39
  end
28
- end
29
40
 
30
- describe SurveySection, "with questions" do
31
- before(:each) do
32
- @survey_section = Factory(:survey_section, :title => "Rhymes", :display_order => 4)
33
- @q1 = @survey_section.questions.create(:text => "Peep", :display_order => 3)
34
- @q2 = @survey_section.questions.create(:text => "Little", :display_order => 1)
35
- @q3 = @survey_section.questions.create(:text => "Bo", :display_order => 2)
36
- end
37
-
38
- it "should return questions sorted in display order" do
39
- @survey_section.questions.should have(3).questions
40
- @survey_section.questions.should == [@q2,@q3,@q1]
41
+ context "with questions" do
42
+ let(:question_1){ Factory(:question, :survey_section => survey_section, :display_order => 3, :text => "Peep")}
43
+ let(:question_2){ Factory(:question, :survey_section => survey_section, :display_order => 1, :text => "Little")}
44
+ let(:question_3){ Factory(:question, :survey_section => survey_section, :display_order => 2, :text => "Bo")}
45
+ before do
46
+ [question_1, question_2, question_3].each{|q| survey_section.questions << q }
47
+ end
48
+ it{ survey_section.should have(3).questions}
49
+ it "gets questions in order" do
50
+ survey_section.questions.should == [question_2, question_3, question_1]
51
+ survey_section.questions.map(&:display_order).should == [1,2,3]
52
+ end
53
+ it "deletes child questions when deleted" do
54
+ question_ids = survey_section.questions.map(&:id)
55
+ survey_section.destroy
56
+ question_ids.each{|id| Question.find_by_id(id).should be_nil}
57
+ end
41
58
  end
42
- it "should delete questions when it is deleted" do
43
- question_ids = @survey_section.questions.map(&:id)
44
- @survey_section.destroy
45
- question_ids.each{|id| Question.find_by_id(id).should be_nil}
59
+
60
+ context "with translations" do
61
+ require 'yaml'
62
+ let(:survey){ Factory(:survey) }
63
+ let(:survey_translation){
64
+ Factory(:survey_translation, :locale => :es, :translation => {
65
+ :survey_sections => {
66
+ :one => {
67
+ :title => "Uno"
68
+ }
69
+ }
70
+ }.to_yaml)
71
+ }
72
+ before do
73
+ survey_section.reference_identifier = "one"
74
+ survey_section.survey = survey
75
+ survey.translations << survey_translation
76
+ end
77
+ it "returns its own translation" do
78
+ YAML.load(survey_translation.translation).should_not be_nil
79
+ survey_section.translation(:es)[:title].should == "Uno"
80
+ end
81
+ it "returns its own default values" do
82
+ survey_section.translation(:de).should == {"title" => survey_section.title, "description" => survey_section.description}
83
+ end
46
84
  end
47
-
48
85
  end
@@ -1,160 +1,191 @@
1
+ # encoding: UTF-8
1
2
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
3
 
3
- # Validations
4
- describe Survey, "when saving a new one" do
5
- before(:each) do
6
- @survey = Factory(:survey, :title => "Foo")
7
- end
8
-
9
- it "should be invalid without a title" do
10
- @survey.title = nil
11
- @survey.should have(1).error_on(:title)
12
- end
13
-
14
- it "should adjust the survey_version to save unique survey_version for each title" do
15
- original = Survey.new(:title => "Foo")
16
- original.save.should be_true
17
- original.survey_version.should == 0
18
- imposter = Survey.new(:title => "Foo")
19
- imposter.save.should be_true
20
- imposter.title.should == "Foo"
21
- imposter.survey_version.should == 1
22
- bandwagoneer = Survey.new(:title => "Foo")
23
- bandwagoneer.save.should be_true
24
- bandwagoneer.title.should == "Foo"
25
- bandwagoneer.survey_version.should == 2
26
- end
27
-
28
- it "should not allow to have duplicate survey_versions of the survey" do
29
- survey = Survey.new(:title => "Foo")
30
- survey.save.should be_true
31
- imposter = Survey.new(:title => "Foo")
32
- imposter.save.should be_true
33
- imposter.survey_version = 0
34
- imposter.save.should be_false
35
- imposter.should have(1).error_on(:survey_version)
36
- end
37
-
38
- it "should not adjust the title when updating itself" do
39
- original = Factory(:survey, :title => "Foo")
40
- original.save.should be_true
41
- original.update_attributes(:title => "Foo")
42
- original.title.should == "Foo"
43
- end
44
-
45
- it "should have an api_id" do
46
- @survey.api_id.length.should == 36
47
- end
48
- end
49
-
50
- # Associations
51
- describe Survey, "that has sections" do
52
- before(:each) do
53
- @survey = Factory(:survey, :title => "Foo")
54
- @s1 = Factory(:survey_section, :survey => @survey, :title => "wise", :display_order => 2)
55
- @s2 = Factory(:survey_section, :survey => @survey, :title => "er", :display_order => 3)
56
- @s3 = Factory(:survey_section, :survey => @survey, :title => "bud", :display_order => 1)
57
- @q1 = Factory(:question, :survey_section => @s1, :text => "what is wise?", :display_order => 2)
58
- @q2 = Factory(:question, :survey_section => @s2, :text => "what is er?", :display_order => 4)
59
- @q3 = Factory(:question, :survey_section => @s2, :text => "what is mill?", :display_order => 3)
60
- @q4 = Factory(:question, :survey_section => @s3, :text => "what is bud?", :display_order => 1)
61
- end
62
-
63
- it "should return survey_sections in display order" do
64
- @survey.sections.should have(3).sections
65
- @survey.sections.should == [@s3, @s1, @s2]
66
- end
4
+ describe Survey do
5
+ let(:survey){ Factory(:survey) }
67
6
 
68
- it "should return survey_sections_with_questions in display order" do
69
- @survey.sections_with_questions.map(&:questions).flatten.should have(4).questions
70
- @survey.sections_with_questions.map(&:questions).flatten.should == [@q4,@q1,@q3,@q2]
71
- end
72
- it "should delete survey sections when it is deleted" do
73
- section_ids = @survey.sections.map(&:id)
74
- @survey.destroy
75
- section_ids.each{|id| SurveySection.find_by_id(id).should be_nil}
7
+ context "when creating" do
8
+ it "is invalid without #title" do
9
+ survey.title = nil
10
+ survey.should have(1).error_on :title
11
+ end
12
+ it "adjust #survey_version" do
13
+ original = Survey.new(:title => "Foo")
14
+ original.save.should be_true
15
+ original.survey_version.should == 0
16
+ imposter = Survey.new(:title => "Foo")
17
+ imposter.save.should be_true
18
+ imposter.title.should == "Foo"
19
+ imposter.survey_version.should == 1
20
+ bandwagoneer = Survey.new(:title => "Foo")
21
+ bandwagoneer.save.should be_true
22
+ bandwagoneer.title.should == "Foo"
23
+ bandwagoneer.survey_version.should == 2
24
+ end
25
+ it "prevents duplicate #survey_version" do
26
+ original = Survey.new(:title => "Foo")
27
+ original.save.should be_true
28
+ imposter = Survey.new(:title => "Foo")
29
+ imposter.save.should be_true
30
+ imposter.survey_version = 0
31
+ imposter.save.should be_false
32
+ imposter.should have(1).error_on(:survey_version)
33
+ end
34
+ it "doesn't adjust #title when" do
35
+ original = Factory(:survey, :title => "Foo")
36
+ original.save.should be_true
37
+ original.update_attributes(:title => "Foo")
38
+ original.title.should == "Foo"
39
+ end
40
+ it "has #api_id with 36 characters by default" do
41
+ survey.api_id.length.should == 36
42
+ end
43
+ it "protects #access_code" do
44
+ saved_attrs = survey.attributes
45
+ if defined? ActiveModel::MassAssignmentSecurity::Error
46
+ expect { survey.update_attributes(:access_code => "NEW") }.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
47
+ else
48
+ survey.attributes = {:access_code => "NEW"} # Rails doesn't return false, but this will be checked in the comparison to saved_attrs
49
+ end
50
+ survey.attributes.should == saved_attrs
51
+ end
52
+ it "protects #api_id" do
53
+ saved_attrs = survey.attributes
54
+ if defined? ActiveModel::MassAssignmentSecurity::Error
55
+ expect { survey.update_attributes(:api_id => "NEW") }.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
56
+ else
57
+ survey.attributes = {:api_id => "NEW"} # Rails doesn't return false, but this will be checked in the comparison to saved_attrs
58
+ end
59
+ survey.attributes.should == saved_attrs
60
+ end
61
+ it "protects #created_at" do
62
+ saved_attrs = survey.attributes
63
+ if defined? ActiveModel::MassAssignmentSecurity::Error
64
+ expect { survey.update_attributes(:created_at => 3.days.ago) }.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
65
+ else
66
+ survey.attributes = {:created_at => 3.days.ago} # Rails doesn't return false, but this will be checked in the comparison to saved_attrs
67
+ end
68
+ survey.attributes.should == saved_attrs
69
+ end
70
+ it "protects #updated_at" do
71
+ saved_attrs = survey.attributes
72
+ if defined? ActiveModel::MassAssignmentSecurity::Error
73
+ expect { survey.update_attributes(:updated_at => 3.hours.ago) }.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
74
+ else
75
+ survey.attributes = {:updated_at => 3.hours.ago} # Rails doesn't return false, but this will be checked in the comparison to saved_attrs
76
+ end
77
+ survey.attributes.should == saved_attrs
78
+ end
76
79
  end
77
- end
78
80
 
79
- # Methods
80
- describe Survey do
81
- before(:each) do
82
- @survey = Survey.new
81
+ context "activating" do
82
+ it { survey.active?.should }
83
+ it "both #inactive_at and #active_at == nil by default" do
84
+ survey.active_at.should be_nil
85
+ survey.inactive_at.should be_nil
86
+ end
87
+ it "#active_at on a certain date/time" do
88
+ survey.inactive_at = 2.days.from_now
89
+ survey.active_at = 2.days.ago
90
+ survey.active?.should be_true
91
+ end
92
+ it "#inactive_at on a certain date/time" do
93
+ survey.active_at = 3.days.ago
94
+ survey.inactive_at = 1.days.ago
95
+ survey.active?.should be_false
96
+ end
97
+ it "#activate! and #deactivate!" do
98
+ survey.activate!
99
+ survey.active?.should be_true
100
+ survey.deactivate!
101
+ survey.active?.should be_false
102
+ end
103
+ it "nils out past values of #inactive_at on #activate!" do
104
+ survey.inactive_at = 5.days.ago
105
+ survey.active?.should be_false
106
+ survey.activate!
107
+ survey.active?.should be_true
108
+ survey.inactive_at.should be_nil
109
+ end
110
+ it "nils out pas values of #active_at on #deactivate!" do
111
+ survey.active_at = 5.days.ago
112
+ survey.active?.should be_true
113
+ survey.deactivate!
114
+ survey.active?.should be_false
115
+ survey.active_at.should be_nil
116
+ end
83
117
  end
84
118
 
85
- it "should be inactive by default" do
86
- @survey.active?.should == false
87
- end
88
- it "should have both inactive_at and active_at be null by default" do
89
- @survey.active_at.should be_nil
90
- @survey.inactive_at.should be_nil
91
- end
92
-
93
- it "should be active or active as of a certain date/time" do
94
- @survey.inactive_at = 2.days.from_now
95
- @survey.active_at = 2.days.ago
96
- @survey.active?.should be_true
97
- end
119
+ context "with survey_sections" do
120
+ let(:s1){ Factory(:survey_section, :survey => survey, :title => "wise", :display_order => 2)}
121
+ let(:s2){ Factory(:survey_section, :survey => survey, :title => "er", :display_order => 3)}
122
+ let(:s3){ Factory(:survey_section, :survey => survey, :title => "bud", :display_order => 1)}
123
+ let(:q1){ Factory(:question, :survey_section => s1, :text => "what is wise?", :display_order => 2)}
124
+ let(:q2){ Factory(:question, :survey_section => s2, :text => "what is er?", :display_order => 4)}
125
+ let(:q3){ Factory(:question, :survey_section => s2, :text => "what is mill?", :display_order => 3)}
126
+ let(:q4){ Factory(:question, :survey_section => s3, :text => "what is bud?", :display_order => 1)}
127
+ before do
128
+ [s1, s2, s3].each{|s| survey.sections << s }
129
+ s1.questions << q1
130
+ s2.questions << q2
131
+ s2.questions << q3
132
+ s3.questions << q4
133
+ end
98
134
 
99
- it "should be able to deactivate as of a certain date/time" do
100
- @survey.active_at = 3.days.ago
101
- @survey.inactive_at = 1.days.ago
102
- @survey.active?.should be_false
135
+ it{ survey.should have(3).sections}
136
+ it "gets survey_sections in order" do
137
+ survey.sections.should == [s3, s1, s2]
138
+ survey.sections.map(&:display_order).should == [1,2,3]
139
+ end
140
+ it "gets survey_sections_with_questions in order" do
141
+ survey.sections_with_questions.map(&:questions).flatten.should have(4).questions
142
+ survey.sections_with_questions.map(&:questions).flatten.should == [q4,q1,q3,q2]
143
+ end
144
+ it "deletes child survey_sections when deleted" do
145
+ survey_section_ids = survey.sections.map(&:id)
146
+ survey.destroy
147
+ survey_section_ids.each{|id| SurveySection.find_by_id(id).should be_nil}
148
+ end
103
149
  end
104
150
 
105
- it "should activate and deactivate" do
106
- @survey.activate!
107
- @survey.active?.should be_true
108
- @survey.deactivate!
109
- @survey.active?.should be_false
110
- end
151
+ context "serialization" do
152
+ let(:s1){ Factory(:survey_section, :survey => survey, :title => "wise") }
153
+ let(:s2){ Factory(:survey_section, :survey => survey, :title => "er") }
154
+ let(:q1){ Factory(:question, :survey_section => s1, :text => "what is wise?") }
155
+ let(:q2){ Factory(:question, :survey_section => s2, :text => "what is er?") }
156
+ let(:q3){ Factory(:question, :survey_section => s2, :text => "what is mill?") }
157
+ before do
158
+ [s1, s2].each{|s| survey.sections << s }
159
+ s1.questions << q1
160
+ s2.questions << q2
161
+ s2.questions << q3
162
+ end
111
163
 
112
- it "should should nil out values of inactive_at that are in the past on activate" do
113
- @survey.inactive_at = 5.days.ago
114
- @survey.active?.should be_false
115
- @survey.activate!
116
- @survey.active?.should be_true
117
- @survey.inactive_at.should be_nil
164
+ it "includes title, sections, and questions" do
165
+ actual = survey.as_json
166
+ actual[:title].should == 'Simple survey'
167
+ actual[:sections].size.should == 2
168
+ actual[:sections][0][:questions_and_groups].size.should == 1
169
+ actual[:sections][1][:questions_and_groups].size.should == 2
170
+ end
118
171
  end
119
172
 
120
- it "should should nil out values of active_at that are in the past on deactivate" do
121
- @survey.active_at = 5.days.ago
122
- @survey.active?.should be_true
123
- @survey.deactivate!
124
- @survey.active?.should be_false
125
- @survey.active_at.should be_nil
126
- end
127
-
128
- it "should protect access_code, api_id, active_at, inactive_at, timestamps" do
129
- saved_attrs = @survey.attributes
130
- if defined? ActiveModel::MassAssignmentSecurity::Error
131
- lambda {@survey.update_attributes(:access_code => "NEW")}.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
132
- lambda {@survey.update_attributes(:api_id => "AND")}.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
133
- lambda {@survey.update_attributes(:active_at => 2.days.ago)}.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
134
- lambda {@survey.update_attributes(:inactive_at => 3.days.from_now)}.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
135
- lambda {@survey.update_attributes(:created_at => 3.days.ago, :updated_at => 3.hours.ago)}.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
136
- else
137
- @survey.update_attributes(:access_code => "NEW").should be_false
138
- @survey.update_attributes(:api_id => "AND").should be_false
139
- @survey.update_attributes(:active_at => 2.days.ago).should be_false
140
- @survey.update_attributes(:inactive_at => 3.days.from_now).should be_false
141
- @survey.attributes = {:created_at => 3.days.ago, :updated_at => 3.hours.ago} # automatically protected by Rails
142
- end
143
- @survey.attributes.should == saved_attrs
144
- end
145
-
146
- it "should include title, sections, and questions when serialized" do
147
- survey = Factory(:survey, :title => "Foo")
148
- s1 = Factory(:survey_section, :survey => survey, :title => "wise")
149
- s2 = Factory(:survey_section, :survey => survey, :title => "er")
150
- q1 = Factory(:question, :survey_section => s1, :text => "what is wise?")
151
- q2 = Factory(:question, :survey_section => s2, :text => "what is er?")
152
- q3 = Factory(:question, :survey_section => s2, :text => "what is mill?")
153
-
154
- actual = survey.as_json
155
- actual[:title].should == 'Foo'
156
- actual[:sections].size.should == 2
157
- actual[:sections][0][:questions_and_groups].size.should == 1
158
- actual[:sections][1][:questions_and_groups].size.should == 2
173
+ context "with translations" do
174
+ require 'yaml'
175
+ let(:survey_translation){
176
+ Factory(:survey_translation, :locale => :es, :translation => {
177
+ :title => "Un idioma nunca es suficiente"
178
+ }.to_yaml)
179
+ }
180
+ before do
181
+ survey.translations << survey_translation
182
+ end
183
+ it "returns its own translation" do
184
+ YAML.load(survey_translation.translation).should_not be_nil
185
+ survey.translation(:es)[:title].should == "Un idioma nunca es suficiente"
186
+ end
187
+ it "returns its own default values" do
188
+ survey.translation(:de).should == {"title" => survey.title, "description" => survey.description}
189
+ end
159
190
  end
160
191
  end