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
@@ -21,12 +21,12 @@ describe SurveyorController do
21
21
  response.should render_template('new')
22
22
  end
23
23
 
24
- it "should list codes and survey_versions for all surveys" do
24
+ it "assigns surveys_by_access_code to surveys grouped by access_code" do
25
25
  original = Factory(:survey, :title => "Foo", :access_code => 'foo')
26
26
  supplant = Factory(:survey, :title => "Foo", :access_code => 'foo', :survey_version => 1)
27
- hash = {"foo"=>{"title"=>"Foo", "survey_versions"=>[0, 1]}}
27
+ hash = {"foo"=>[supplant, original]}
28
28
  do_get
29
- assigns(:codes).should eq hash
29
+ assigns(:surveys_by_access_code).should eq hash
30
30
  end
31
31
  end
32
32
 
data/spec/factories.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  # http://github.com/thoughtbot/factory_girl/tree/master
2
3
  require 'rubygems'
3
4
  require 'factory_girl'
@@ -5,13 +6,26 @@ require 'factory_girl'
5
6
  Factory.sequence(:unique_survey_access_code){|n| "simple survey #{UUIDTools::UUID.random_create.to_s}" }
6
7
 
7
8
  Factory.define :survey do |s|
8
- s.title {"Simple survey"}
9
- s.description {"A simple survey for testing"}
10
- s.access_code {Factory.next :unique_survey_access_code}
11
- s.active_at {Time.now}
12
- s.inactive_at {}
13
- s.css_url {}
14
- s.survey_version {0}
9
+ s.title "Simple survey"
10
+ s.description "A simple survey for testing"
11
+ s.access_code { Factory.next :unique_survey_access_code }
12
+ s.survey_version 0
13
+ end
14
+
15
+ Factory.define :survey_translation do |t|
16
+ t.locale "es"
17
+ t.translation %(title: "Un idioma nunca es suficiente"
18
+ survey_sections:
19
+ one:
20
+ title: "Uno"
21
+ questions:
22
+ hello:
23
+ text: "¡Hola!"
24
+ name:
25
+ text: "¿Cómo se llama Usted?"
26
+ answers:
27
+ name:
28
+ help_text: "Mi nombre es...")
15
29
  end
16
30
 
17
31
  Factory.sequence(:survey_section_display_order){|n| n }
@@ -5,12 +5,11 @@ describe Surveyor::Helpers::FormtasticCustomInput do
5
5
  context "input helpers" do
6
6
  it "should translate response class into attribute" do
7
7
  helper.response_class_to_method(:string).should == :string_value
8
- helper.response_class_to_method(:other_and_string).should == :string_value
9
8
  helper.response_class_to_method(:integer).should == :integer_value
10
9
  helper.response_class_to_method(:float).should == :float_value
11
10
  helper.response_class_to_method(:datetime).should == :datetime_value
12
- helper.response_class_to_method(:date).should == :datetime_value
13
- helper.response_class_to_method(:time).should == :datetime_value
11
+ helper.response_class_to_method(:date).should == :date_value
12
+ helper.response_class_to_method(:time).should == :time_value
14
13
  end
15
14
  end
16
15
  end
@@ -1,110 +1,77 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe SurveyorHelper do
4
-
5
- before(:each) do
6
-
7
- end
8
- it "should return the question text with number" do
9
- q1 = Factory(:question)
10
- q2 = Factory(:question, :display_type => "label")
11
- q3 = Factory(:question, :dependency => Factory(:dependency))
12
- q4 = Factory(:question, :display_type => "image", :text => "something.jpg")
13
- q5 = Factory(:question, :question_group => Factory(:question_group))
14
- helper.q_text(q1).should == "<span class='qnum'>1) </span>#{q1.text}"
15
- helper.q_text(q2).should == q2.text
16
- helper.q_text(q3).should == q3.text
17
-
18
- dir = "images"
19
- if Rails.application.config.respond_to?(:assets) && Rails.application.config.assets
20
- dir = "assets"
21
- end
22
-
23
- helper.q_text(q4).should == %Q(<img alt="Something" src="/#{dir}/something.jpg" />)
24
- helper.q_text(q5).should == q5.text
25
- end
26
- require 'mustache'
27
- class FakeMustacheContext < ::Mustache
28
- def site
29
- "Northwestern"
4
+ context "numbering" do
5
+ let(:asset_directory){ asset_pipeline_enabled? ? "assets" : "images" }
6
+ before do
7
+ ActionController::Base.helpers.config.assets_dir = "public" unless asset_pipeline_enabled?
30
8
  end
31
- def somethingElse
32
- "something new"
9
+ it "should return the question text with number, except for labels, dependencies, images, and grouped questions" do
10
+ q1 = Factory(:question)
11
+ q2 = Factory(:question, :display_type => "label")
12
+ q3 = Factory(:question, :dependency => Factory(:dependency))
13
+ q4 = Factory(:question, :display_type => "image", :text => "something.jpg")
14
+ q5 = Factory(:question, :question_group => Factory(:question_group))
15
+ helper.q_text(q1).should == "<span class='qnum'>1) </span>#{q1.text}"
16
+ helper.q_text(q2).should == q2.text
17
+ helper.q_text(q3).should == q3.text
18
+ helper.q_text(q4).should == %Q(<img alt="Something" src="/#{asset_directory}/something.jpg" />)
19
+ helper.q_text(q5).should == q5.text
33
20
  end
34
-
35
- def group
36
- "NUBIC"
37
- end
38
- end
39
- it "should return text with with substituted value" do
40
- q1 = Factory(:question, :text => "You are in {{site}}")
41
- label = Factory(:question, :display_type => "label", :text => "Testing {{somethingElse}}")
42
- helper.q_text(q1, FakeMustacheContext).should == "<span class='qnum'>1) </span>You are in Northwestern"
43
- helper.q_text(label, FakeMustacheContext).should == "Testing something new"
44
- end
45
- it "should return help_text for question with substituted value" do
46
- q2 = Factory(:question, :display_type => "label", :text => "Is you site Northwestern?", :help_text => "If your site is not {{site}}, pick 'no' for the answer")
47
- helper.render_help_text(q2, FakeMustacheContext).should == "If your site is not Northwestern, pick 'no' for the answer"
48
- end
49
- it "should return help_text for group text with number" do
50
- g1 = Factory(:question_group, :text => "You are part of the {{group}}")
51
- helper.q_text(g1, FakeMustacheContext).should == "<span class='qnum'>1) </span>You are part of the NUBIC"
52
- end
53
- it "should return help_text for group text" do
54
- g1 = Factory(:question_group, :text => "You are part of the {{group}}", :help_text => "Make sure you know what the {{group}} stands for")
55
- helper.render_help_text(g1, FakeMustacheContext).should == "Make sure you know what the NUBIC stands for"
56
- end
57
-
58
- it "should return rendered text for answer" do
59
- q1 = Factory(:question, :text => "Do you work for {{site}}", :answers => [a1 = Factory(:answer, :text => "No, I don't work for {{site}}"), a2 = Factory(:answer, :text => "Yes, I do work for {{site}}") ])
60
- helper.a_text(a1, nil, FakeMustacheContext).should == "No, I don't work for Northwestern"
61
- helper.a_text(a2, nil, FakeMustacheContext).should == "Yes, I do work for Northwestern"
62
21
  end
63
22
 
64
- it "should return the group text with number" do
65
- g1 = Factory(:question_group)
66
- helper.q_text(g1, FakeMustacheContext).should == "<span class='qnum'>1) </span>#{g1.text}"
23
+ context "with mustache text substitution" do
24
+ require 'mustache'
25
+ let(:mustache_context){ Class.new(::Mustache){ def site; "Northwestern"; end; def somethingElse; "something new"; end; def group; "NUBIC"; end } }
26
+ it "substitues values into Question#text" do
27
+ q1 = Factory(:question, :text => "You are in {{site}}")
28
+ label = Factory(:question, :display_type => "label", :text => "Testing {{somethingElse}}")
29
+ helper.q_text(q1, mustache_context).should == "<span class='qnum'>1) </span>You are in Northwestern"
30
+ helper.q_text(label, mustache_context).should == "Testing something new"
31
+ end
67
32
  end
68
33
 
69
- it "should find or create responses, with index" do
70
- q1 = Factory(:question, :answers => [a = Factory(:answer, :text => "different")])
71
- q2 = Factory(:question, :answers => [b = Factory(:answer, :text => "strokes")])
72
- q3 = Factory(:question, :answers => [c = Factory(:answer, :text => "folks")])
73
- rs = Factory(:response_set, :responses => [r1 = Factory(:response, :question => q1, :answer => a), r3 = Factory(:response, :question => q3, :answer => c, :response_group => 1)])
34
+ context "response methods" do
35
+ it "should find or create responses, with index" do
36
+ q1 = Factory(:question, :answers => [a = Factory(:answer, :text => "different")])
37
+ q2 = Factory(:question, :answers => [b = Factory(:answer, :text => "strokes")])
38
+ q3 = Factory(:question, :answers => [c = Factory(:answer, :text => "folks")])
39
+ rs = Factory(:response_set, :responses => [r1 = Factory(:response, :question => q1, :answer => a), r3 = Factory(:response, :question => q3, :answer => c, :response_group => 1)])
40
+
41
+ helper.response_for(rs, nil).should == nil
42
+ helper.response_for(nil, q1).should == nil
43
+ helper.response_for(rs, q1).should == r1
44
+ helper.response_for(rs, q1, a).should == r1
45
+ helper.response_for(rs, q2).attributes.reject{|k,v| k == "api_id"}.should == Response.new(:question => q2, :response_set => rs).attributes.reject{|k,v| k == "api_id"}
46
+ helper.response_for(rs, q2, b).attributes.reject{|k,v| k == "api_id"}.should == Response.new(:question => q2, :response_set => rs).attributes.reject{|k,v| k == "api_id"}
47
+ helper.response_for(rs, q3, c, "1").should == r3
74
48
 
75
- helper.response_for(rs, nil).should == nil
76
- helper.response_for(nil, q1).should == nil
77
- helper.response_for(rs, q1).should == r1
78
- helper.response_for(rs, q1, a).should == r1
79
- helper.response_for(rs, q2).attributes.reject{|k,v| k == "api_id"}.should == Response.new(:question => q2, :response_set => rs).attributes.reject{|k,v| k == "api_id"}
80
- helper.response_for(rs, q2, b).attributes.reject{|k,v| k == "api_id"}.should == Response.new(:question => q2, :response_set => rs).attributes.reject{|k,v| k == "api_id"}
81
- helper.response_for(rs, q3, c, "1").should == r3
82
-
83
- end
84
- it "should keep an index of responses" do
85
- helper.response_idx.should == "1"
86
- helper.response_idx.should == "2"
87
- helper.response_idx(false).should == "2"
88
- helper.response_idx.should == "3"
89
- end
90
- it "should translate response class into attribute" do
91
- helper.rc_to_attr(:string).should == :string_value
92
- helper.rc_to_attr(:text).should == :text_value
93
- helper.rc_to_attr(:integer).should == :integer_value
94
- helper.rc_to_attr(:float).should == :float_value
95
- helper.rc_to_attr(:datetime).should == :datetime_value
96
- helper.rc_to_attr(:date).should == :date_value
97
- helper.rc_to_attr(:time).should == :time_value
98
- end
49
+ end
50
+ it "should keep an index of responses" do
51
+ helper.response_idx.should == "1"
52
+ helper.response_idx.should == "2"
53
+ helper.response_idx(false).should == "2"
54
+ helper.response_idx.should == "3"
55
+ end
56
+ it "should translate response class into attribute" do
57
+ helper.rc_to_attr(:string).should == :string_value
58
+ helper.rc_to_attr(:text).should == :text_value
59
+ helper.rc_to_attr(:integer).should == :integer_value
60
+ helper.rc_to_attr(:float).should == :float_value
61
+ helper.rc_to_attr(:datetime).should == :datetime_value
62
+ helper.rc_to_attr(:date).should == :date_value
63
+ helper.rc_to_attr(:time).should == :time_value
64
+ end
99
65
 
100
- it "should translate response class into as" do
101
- helper.rc_to_as(:string).should == :string
102
- helper.rc_to_as(:text).should == :text
103
- helper.rc_to_as(:integer).should == :string
104
- helper.rc_to_as(:float).should == :string
105
- helper.rc_to_as(:datetime).should == :string
106
- helper.rc_to_as(:date).should == :string
107
- helper.rc_to_as(:time).should == :string
66
+ it "should translate response class into as" do
67
+ helper.rc_to_as(:string).should == :string
68
+ helper.rc_to_as(:text).should == :text
69
+ helper.rc_to_as(:integer).should == :string
70
+ helper.rc_to_as(:float).should == :string
71
+ helper.rc_to_as(:datetime).should == :string
72
+ helper.rc_to_as(:date).should == :string
73
+ helper.rc_to_as(:time).should == :string
74
+ end
108
75
  end
109
76
 
110
77
  context "overriding methods" do
@@ -121,7 +88,6 @@ describe SurveyorHelper do
121
88
  end
122
89
  end
123
90
  end
124
-
125
91
  it "should translate response class into as" do
126
92
  helper.rc_to_as(:string).should == :string
127
93
  helper.rc_to_as(:text).should == :text
@@ -131,7 +97,6 @@ describe SurveyorHelper do
131
97
  helper.rc_to_as(:date).should == :date # not string
132
98
  helper.rc_to_as(:time).should == :time
133
99
  end
134
-
135
100
  after do
136
101
  module SurveyorHelper
137
102
  include Surveyor::Helpers::SurveyorHelperMethods
@@ -142,7 +107,7 @@ describe SurveyorHelper do
142
107
  end
143
108
  end
144
109
 
145
- # Make sure that this context runs after the previous context 'overriding methods'
110
+ # run this context after 'overriding methods'
146
111
  context "post override test" do
147
112
  # Sanity check
148
113
  it "should translate response class into as after override" do
@@ -150,5 +115,4 @@ describe SurveyorHelper do
150
115
  helper.rc_to_as(:date).should == :string # back to string
151
116
  end
152
117
  end
153
-
154
118
  end
@@ -1,67 +1,175 @@
1
+ # encoding: UTF-8
1
2
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
3
 
3
- describe Answer, "when creating a new answer" do
4
- before(:each) do
5
- @answer = Factory(:answer, :text => "Red")
6
- end
7
-
8
- it "should be valid" do
9
- @answer.should be_valid
10
- end
11
-
12
- # this causes issues with building and saving answers to questions within a grid.
13
- # it "should be invalid without a question_id" do
14
- # @answer.question_id = nil
15
- # @answer.should_not be_valid
16
- # end
17
-
18
- it "should tell me its css class" do
19
- @answer.custom_class = "foo bar"
20
- @answer.css_class.should == "foo bar"
21
- @answer.is_exclusive = true
22
- @answer.css_class.should == "exclusive foo bar"
23
- end
24
-
25
- it "should hide the label when display_type hidden_label is set" do
26
- @answer.split_or_hidden_text.should == "Red"
27
- @answer.display_type = "hidden_label"
28
- @answer.split_or_hidden_text.should == ""
29
- end
30
- it "should split up pre/post labels" do
31
- @answer.text = "before|after|extra"
32
- @answer.split_or_hidden_text(:pre).should == "before"
33
- @answer.split_or_hidden_text(:post).should == "after|extra"
34
- end
35
- it "should delete validation when it is deleted" do
36
- v_id = Factory(:validation, :answer => @answer).id
37
- @answer.destroy
38
- Validation.find_by_id(v_id).should be_nil
4
+ describe Answer do
5
+ let(:answer){ Factory(:answer) }
6
+
7
+ context "when creating" do
8
+ it { answer.should be_valid }
9
+ it "deletes validation when deleted" do
10
+ v_id = Factory(:validation, :answer => answer).id
11
+ answer.destroy
12
+ Validation.find_by_id(v_id).should be_nil
13
+ end
14
+ it "protects #api_id" do
15
+ saved_attrs = answer.attributes
16
+ if defined? ActiveModel::MassAssignmentSecurity::Error
17
+ expect { answer.update_attributes(:api_id => "NEW") }.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
18
+ else
19
+ answer.attributes = {:api_id => "NEW"} # Rails doesn't return false, but this will be checked in the comparison to saved_attrs
20
+ end
21
+ answer.attributes.should == saved_attrs
22
+ end
23
+ it "protects #created_at" do
24
+ saved_attrs = answer.attributes
25
+ if defined? ActiveModel::MassAssignmentSecurity::Error
26
+ expect { answer.update_attributes(:created_at => 3.days.ago) }.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
27
+ else
28
+ answer.attributes = {:created_at => 3.days.ago} # Rails doesn't return false, but this will be checked in the comparison to saved_attrs
29
+ end
30
+ answer.attributes.should == saved_attrs
31
+ end
32
+ it "protects #updated_at" do
33
+ saved_attrs = answer.attributes
34
+ if defined? ActiveModel::MassAssignmentSecurity::Error
35
+ expect { answer.update_attributes(:updated_at => 3.hours.ago) }.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
36
+ else
37
+ answer.attributes = {:updated_at => 3.hours.ago} # Rails doesn't return false, but this will be checked in the comparison to saved_attrs
38
+ end
39
+ answer.attributes.should == saved_attrs
40
+ end
39
41
  end
40
-
41
- it "should have an api_id" do
42
- @answer.api_id.length.should == 36
42
+
43
+ context "with mustache text substitution" do
44
+ require 'mustache'
45
+ let(:mustache_context){ Class.new(::Mustache){ def site; "Northwestern"; end; def foo; "bar"; end } }
46
+ it "subsitutes Mustache context variables" do
47
+ answer.text = "You are in {{site}}"
48
+ answer.in_context(answer.text, mustache_context).should == "You are in Northwestern"
49
+ answer.text_for(nil, mustache_context).should == "You are in Northwestern"
50
+
51
+ answer.help_text = "{{site}} is your site"
52
+ answer.in_context(answer.help_text, mustache_context).should == "Northwestern is your site"
53
+ answer.help_text_for(mustache_context).should == "Northwestern is your site"
54
+
55
+ answer.default_value = "{{site}}"
56
+ answer.in_context(answer.default_value, mustache_context).should == "Northwestern"
57
+ answer.default_value_for(mustache_context).should == "Northwestern"
58
+ end
43
59
  end
44
-
45
- it "should protect api_id, timestamps" do
46
- saved_attrs = @answer.attributes
47
- if defined? ActiveModel::MassAssignmentSecurity::Error
48
- lambda {@answer.update_attributes(:created_at => 3.days.ago, :updated_at => 3.hours.ago)}.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
49
- lambda {@answer.update_attributes(:api_id => "NEW")}.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
50
- else
51
- @answer.attributes = {:created_at => 3.days.ago, :updated_at => 3.hours.ago} # automatically protected by Rails
52
- @answer.attributes = {:api_id => "NEW"} # Rails doesn't return false, but this will be checked in the comparison to saved_attrs
53
- end
54
- @answer.attributes.should == saved_attrs
60
+
61
+ context "with translations" do
62
+ require 'yaml'
63
+ let(:survey){ Factory(:survey) }
64
+ let(:survey_section){ Factory(:survey_section) }
65
+ let(:survey_translation){
66
+ Factory(:survey_translation, :locale => :es, :translation => {
67
+ :questions => {
68
+ :name => {
69
+ :answers => {
70
+ :name => {
71
+ :help_text => "Mi nombre es..."
72
+ }
73
+ }
74
+ }
75
+ }
76
+ }.to_yaml)
77
+ }
78
+ let(:question){ Factory(:question, :reference_identifier => "name") }
79
+ before do
80
+ answer.reference_identifier = "name"
81
+ answer.help_text = "My name is..."
82
+ answer.text = nil
83
+ answer.question = question
84
+ question.survey_section = survey_section
85
+ survey_section.survey = survey
86
+ survey.translations << survey_translation
87
+ end
88
+ it "returns its own translation" do
89
+ answer.translation(:es)[:help_text].should == "Mi nombre es..."
90
+ end
91
+ it "returns translations in views" do
92
+ answer.help_text_for(nil, :es).should == "Mi nombre es..."
93
+ end
94
+ it "returns its own default values" do
95
+ answer.translation(:de).should == {"text" => nil, "help_text" => "My name is...", "default_value" => nil}
96
+ end
97
+ it "returns default values in views" do
98
+ answer.help_text_for(nil, :de).should == "My name is..."
99
+ end
55
100
  end
56
-
57
- require 'mustache'
58
- class FakeMustacheContext < ::Mustache
59
- def site
60
- "Northwestern"
101
+
102
+ context "handling strings" do
103
+ it "#split preserves strings" do
104
+ answer.split(answer.text).should == "My favorite color is clear"
105
+ end
106
+ it "#split(:pre) preserves strings" do
107
+ answer.split(answer.text, :pre).should == "My favorite color is clear"
108
+ end
109
+ it "#split(:post) preserves strings" do
110
+ answer.split(answer.text, :post).should == ""
111
+ end
112
+ it "#split splits strings" do
113
+ answer.text = "before|after|extra"
114
+ answer.split(answer.text).should == "before|after|extra"
115
+ end
116
+ it "#split(:pre) splits strings" do
117
+ answer.text = "before|after|extra"
118
+ answer.split(answer.text, :pre).should == "before"
119
+ end
120
+ it "#split(:post) splits strings" do
121
+ answer.text = "before|after|extra"
122
+ answer.split(answer.text, :post).should == "after|extra"
61
123
  end
62
124
  end
63
- it "should substitute 'Northwestern' in place {{site}}" do
64
- @answer.text = "You are in {{site}}"
65
- @answer.split_or_hidden_text(nil, FakeMustacheContext).should == "You are in Northwestern"
125
+
126
+ context "for views" do
127
+ let(:asset_directory){ asset_pipeline_enabled? ? "assets" : "images" }
128
+ before do
129
+ ActionController::Base.helpers.config.assets_dir = "public" unless asset_pipeline_enabled?
130
+ end
131
+ it "#text_for with #display_type == image" do
132
+ answer.text = "rails.png"
133
+ answer.display_type = :image
134
+ answer.text_for.should == %(<img alt="Rails" src="/#{asset_directory}/rails.png" />)
135
+ end
136
+ it "#text_for with #display_type == hidden_label" do
137
+ answer.text = "Red"
138
+ answer.text_for.should == "Red"
139
+ answer.display_type = "hidden_label"
140
+ answer.text_for.should == ""
141
+ end
142
+ it "#default_value_for"
143
+ it "#help_text_for"
144
+ it "reports DOM ready #css_class from #custom_class" do
145
+ answer.custom_class = "foo bar"
146
+ answer.css_class.should == "foo bar"
147
+ end
148
+ it "reports DOM ready #css_class from #custom_class and #is_exclusive" do
149
+ answer.custom_class = "foo bar"
150
+ answer.is_exclusive = true
151
+ answer.css_class.should == "exclusive foo bar"
152
+ end
153
+ it "#text_for preserves strings" do
154
+ answer.text_for.should == "My favorite color is clear"
155
+ end
156
+ it "#text_for(:pre) preserves strings" do
157
+ answer.text_for(:pre).should == "My favorite color is clear"
158
+ end
159
+ it "#text_for(:post) preserves strings" do
160
+ answer.text_for(:post).should == ""
161
+ end
162
+ it "#text_for splits strings" do
163
+ answer.text = "before|after|extra"
164
+ answer.text_for.should == "before|after|extra"
165
+ end
166
+ it "#text_for(:pre) splits strings" do
167
+ answer.text = "before|after|extra"
168
+ answer.text_for(:pre).should == "before"
169
+ end
170
+ it "#text_for(:post) splits strings" do
171
+ answer.text = "before|after|extra"
172
+ answer.text_for(:post).should == "after|extra"
173
+ end
66
174
  end
67
175
  end