SurveyConstructor
IMPORTANT MESSAGE: Require pre-installation of rails
This is a gem that allow generate survey models using pre existing models. This models are
models
├── answer_choice.rb
├── answer.rb
├── choice.rb
├── question.rb
├── survey.rb
└── survey_response.rb
In addition, it allows generating and running the corresponding migrations:
db
└── migrate
├── 20180523221331_create_surveys.rb
├── 20180523221722_create_choices.rb
├── 20180523222506_create_questions.rb
├── 20180523222646_create_answers.rb
├── 20180608225714_create_survey_responses.rb
├── 20180608230124_add_survey_respose_to_answer.rb
└── 20180611182929_create_answer_choices.rb
This files are copied to "./app/models"
and "./db/migrate"
respectively
Installation
Add this line to your application's Gemfile:
gem 'survey_constructor'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install survey_constructor
Usage
To use this gem run:
surveyConstructor models
What do the generated files contain?
This files are directly generated to "./app/models"
and "./db/migrate"
respectively
In the case of models:
class AnswerChoice < ApplicationRecord
has_paper_trail
belongs_to :choice, optional: true
belongs_to :answer
end
class Answer < ApplicationRecord
has_paper_trail
belongs_to :question
belongs_to :survey_response
has_many :answer_choices
has_many :choices, through: :answer_choices
scope :ordered, -> { includes(:question).order('questions.position') }
accepts_nested_attributes_for :answer_choices, allow_destroy: true
def text_answer
return justification if question.text?
array_answers = choices.order(:id).map do |c|
placeholder = question.placeholder.blank? ? ', ' : ", #{question.placeholder} "
if c.justifiable
"#{c.value}#{placeholder}#{justification.capitalize}"
else
c.value
end
end
array_answers.join(', ')
end
end
class Choice < ApplicationRecord
has_paper_trail
belongs_to :question
has_many :answer_choices
has_many :answers, through: :answer_choices
validates_presence_of :value
end
class Question < ApplicationRecord
has_paper_trail
belongs_to :survey
acts_as_list scope: :survey
has_many :answers, dependent: :destroy
has_many :choices, dependent: :destroy
validates_presence_of :title, :question_type
accepts_nested_attributes_for :choices, allow_destroy: true
enum question_type: %i[text multiple_choice unique_choice]
after_save :reorder_studios
private
def reorder_studios
self.insert_at(self.position) if self.position_changed?
end
end
class Survey < ApplicationRecord
has_paper_trail
has_many :platform_surveys, dependent: :destroy
has_many :platform, through: :platform_surveys
has_many :questions, dependent: :destroy
has_many :survey_responses, dependent: :destroy
has_many :patients
validates_presence_of :title, :description
accepts_nested_attributes_for :questions, allow_destroy: true
end
class SurveyResponse < ApplicationRecord
has_paper_trail
belongs_to :survey
has_one :order
has_many :answers, dependent: :destroy
accepts_nested_attributes_for :answers, allow_destroy: true
end
In the case of migrations:
class CreateSurveys < ActiveRecord::Migration[5.2]
def change
create_table :surveys do |t|
t.string :description
t.string :title
t.timestamps
end
end
end
class CreateChoices < ActiveRecord::Migration[5.2]
def change
create_table :choices do |t|
t.string :value
t.belongs_to :question, index: true
t.timestamps
end
end
end
class CreateQuestions < ActiveRecord::Migration[5.2]
def change
create_table :questions do |t|
t.string :title
t.integer :question_type
t.belongs_to :survey, index: true
t.timestamps
end
end
end
class CreateAnswers < ActiveRecord::Migration[5.2]
def change
create_table :answers do |t|
t.string :justification
t.belongs_to :question, index: true
t.timestamps
end
end
end
class CreateSurveyResponses < ActiveRecord::Migration[5.2]
def change
create_table :survey_responses do |t|
t.belongs_to :survey, index: true
t.belongs_to :patient, index: true
t.timestamps
end
end
end
class AddSurveyResposeToAnswer < ActiveRecord::Migration[5.2]
def change
add_column :answers, :survey_response_id, :integer
end
end
class CreateAnswerChoices < ActiveRecord::Migration[5.2]
def change
create_table :answer_choices do |t|
t.belongs_to :answer, index: true
t.belongs_to :choice, index: true
t.timestamps
end
end
end
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/emrszon/survey_constructor. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the SurveyConstructor project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.