Skip to content

Commit

Permalink
add predefined values to question, to be able to add a range question…
Browse files Browse the repository at this point in the history
… type
  • Loading branch information
mostafa-kenawey committed Sep 17, 2013
1 parent 0118fb6 commit 3e67b50
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 4 deletions.
4 changes: 3 additions & 1 deletion app/models/survey/answer.rb
Expand Up @@ -3,15 +3,17 @@ class Survey::Answer < ActiveRecord::Base
self.table_name = "survey_answers"
belongs_to :attempt
belongs_to :option
belongs_to :predefined_value
belongs_to :question

validates :option_id, :question_id, :presence => true
validates :predefined_value_id, :presence => true , :if => Proc.new{|a| a.question && a.question.predefined_values.count > 0 }
validates :option_text, :presence => true , :if => Proc.new{|a| a.option && ([Survey::OptionsType.text, Survey::OptionsType.multi_choices_with_text, Survey::OptionsType.single_choice_with_text].include?(a.option.options_type_id)) }
validates :option_number, :presence => true , :if => Proc.new{|a| a.option && ([Survey::OptionsType.number, Survey::OptionsType.multi_choices_with_number, Survey::OptionsType.single_choice_with_number].include?(a.option.options_type_id)) }

#rails 3 attr_accessible support
if Rails::VERSION::MAJOR < 4
attr_accessible :option, :attempt, :question, :question_id, :option_id, :attempt_id, :option_text, :option_number
attr_accessible :option, :attempt, :question, :question_id, :option_id, :predefined_value_id, :attempt_id, :option_text, :option_number
end

before_create :characterize_answer
Expand Down
23 changes: 23 additions & 0 deletions app/models/survey/predefined_value.rb
@@ -0,0 +1,23 @@
class Survey::PredefinedValue < ActiveRecord::Base

self.table_name = "survey_predefined_values"

#relations
belongs_to :question

#rails 3 attr_accessible support
if Rails::VERSION::MAJOR < 4
attr_accessible :head_number, :name, :locale_name, :question_id
end

# validations
validates :name, :presence => true

def to_s
self.name
end

def name
I18n.locale == I18n.default_locale ? super : locale_name || super
end
end
6 changes: 5 additions & 1 deletion app/models/survey/question.rb
Expand Up @@ -3,17 +3,21 @@ class Survey::Question < ActiveRecord::Base
self.table_name = "survey_questions"
# relations
has_many :options
has_many :predefined_values
belongs_to :section

#rails 3 attr_accessible support
if Rails::VERSION::MAJOR < 4
attr_accessible :options_attributes, :text, :section_id, :head_number, :description, :locale_text, :locale_head_number, :locale_description, :questions_type_id
attr_accessible :options_attributes, :predefined_values_attributes, :text, :section_id, :head_number, :description, :locale_text, :locale_head_number, :locale_description, :questions_type_id
end

accepts_nested_attributes_for :options,
:reject_if => ->(a) { a[:options_type_id].blank? },
:allow_destroy => true

accepts_nested_attributes_for :predefined_values,
:reject_if => ->(a) { a[:name].blank? },
:allow_destroy => true

# validations
validates :text, :presence => true, :allow_blank => false
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Expand Up @@ -8,6 +8,7 @@ en:
survey_details: Survey Details
questions: Questions
sections: Sections
predefined_values: Predefined Values
activerecord:
models:
survey:
Expand Down Expand Up @@ -38,6 +39,7 @@ en:
locale_text: Localized Question Text
locale_description: Localized Description
options_attributes: Options
predefined_values_attributes: Predefined Values
options:
text: Option Text
locale_text: Localized Option Text
Expand Down
3 changes: 2 additions & 1 deletion lib/generators/survey/install_generator.rb
Expand Up @@ -10,7 +10,8 @@ def copy_migration
{new_file_name: "create_sections", origin_file_name: "migration_section"},
{new_file_name: "update_survey_tables", origin_file_name: "migration_update_survey_tables"},
{new_file_name: "add_types_to_questions_and_options", origin_file_name: "migration_add_types_to_questions_and_options"},
{new_file_name: "add_head_number_to_options_table", origin_file_name: "migration_add_head_number_to_options_table"}
{new_file_name: "add_head_number_to_options_table", origin_file_name: "migration_add_head_number_to_options_table"},
{new_file_name: "create_predefined_values_table", origin_file_name: "migration_create_predefined_values_table"}
]

migration_files.each do |migration_file|
Expand Down
8 changes: 8 additions & 0 deletions lib/generators/templates/active_admin.rb
Expand Up @@ -52,6 +52,14 @@
q.input :locale_description
q.input :questions_type_id, :as => :select, :collection => Survey::QuestionsType.questions_types_title

q.inputs I18n.t("predefined_values") do
q.has_many :predefined_values do |p|
p.input :head_number
p.input :name
p.input :locale_name
end
end

q.has_many :options do |a|
a.input :head_number
a.input :text
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/templates/attempts_plain.rb
Expand Up @@ -30,7 +30,7 @@ def attempt_params
if Rails::VERSION::MAJOR < 4
params[:survey_attempt]
else
params.require(:survey_attempt).permit(answers_attributes: [:question_id, :option_id, :option_text, :option_number])
params.require(:survey_attempt).permit(answers_attributes: [:question_id, :option_id, :option_text, :option_number, :predefined_value_id])
end
end
end
@@ -0,0 +1,14 @@
class CreatePredefinedValuesTable < ActiveRecord::Migration
def change
create_table :survey_predefined_values do |t|
t.string :head_number
t.string :name
t.string :locale_name
t.integer :question_id

t.timestamps
end

add_column :survey_answers, :predefined_value_id, :integer
end
end
@@ -0,0 +1,14 @@
class CreatePredefinedValuesTable < ActiveRecord::Migration
def change
create_table :survey_predefined_values do |t|
t.string :head_number
t.string :name
t.string :locale_name
t.integer :question_id

t.timestamps
end

add_column :survey_answers, :predefined_value_id, :integer
end
end
23 changes: 23 additions & 0 deletions test/models/answer_test.rb
Expand Up @@ -108,6 +108,29 @@ class AnswerTest < ActiveSupport::TestCase
assert_equal answer_try_1.option_text, nil
end

test "should create an answer with a predefined_value_id field for single_choice type" do
survey, option, attempt, question = create_answer_with_option_type(Survey::OptionsType.single_choice)
predefined_value = create_predefined_value
question.predefined_values << predefined_value
question.save
answer_try_1 = create_answer(:option => option, :attempt => attempt, :question => question, :predefined_value_id => predefined_value.id)

should_be_persisted survey
should_be_persisted question
should_be_persisted answer_try_1
assert_equal answer_try_1.predefined_value_id, predefined_value.id
end

test "should not create an answer with an empty predefined_value_id field for single_choice type" do
survey, option, attempt, question = create_answer_with_option_type(Survey::OptionsType.single_choice)
question.predefined_values << create_predefined_value
question.save
answer_try_1 = create_answer(:option => option, :attempt => attempt, :question => question, :predefined_value_id => nil)

should_be_persisted survey
should_not_be_persisted answer_try_1
end

test "can create an answer already made to the same attempt" do
answer_try_1 = create_answer
attempt = answer_try_1.attempt
Expand Down
17 changes: 17 additions & 0 deletions test/models/predefined_value_test.rb
@@ -0,0 +1,17 @@
require 'test_helper'

class PredefinedValueTest < ActiveSupport::TestCase

test "should create a valid predefined_value" do
predefined_value = create_predefined_value
should_be_persisted predefined_value
end

test "should not create a predefined_value with a empty or nil name field" do
predefined_value_a = create_predefined_value({:name => nil})
predefined_value_b = create_predefined_value({:name => ''})

should_not_be_persisted predefined_value_a
should_not_be_persisted predefined_value_b
end
end
7 changes: 7 additions & 0 deletions test/models/question_test.rb
Expand Up @@ -19,6 +19,13 @@ class QuestionTest < ActiveSupport::TestCase
should_not_be_persisted question
end

test "should create a valid question with predefined_values" do
question = create_question({:predefined_values => [create_predefined_value] })

should_be_persisted question
assert_equal question.predefined_values.count, 1
end

test "should not create a question with a empty or nil questions_type_id field" do
question = create_question({:questions_type_id => nil})

Expand Down
7 changes: 7 additions & 0 deletions test/support/factories.rb
Expand Up @@ -27,6 +27,13 @@ def create_question(opts = {})
}.merge(opts))
end

# Create a Survey::PredefinedValue
def create_predefined_value(opts = {})
Survey::PredefinedValue.create({
:name => ::Faker::Name.name
}.merge(opts))
end

# Create a Survey::option but not saved
def new_option(opts = {})
Survey::Option.new(option_attributes.merge(opts))
Expand Down

0 comments on commit 3e67b50

Please sign in to comment.