Improving surveys system: showing surveys in admin UI as they will be for the user

This commit is contained in:
mdeniz 2016-06-29 17:23:01 +02:00
parent 335608e6c4
commit 03168b4efe
15 changed files with 213 additions and 46 deletions

View file

@ -12,4 +12,22 @@ $(function() {
$('.survey-possible-answers').addClass('hidden'); $('.survey-possible-answers').addClass('hidden');
} }
}); });
$('#survey_question_title').on('keyup', function(){
$('#survey_question_preview #title').text($(this).val())
});
function render_possible_answers_preview() {
var options_html = '';
var options_array = $('#survey_question_possible_answers').val().split(',');
var input_type = ($('#survey_question_min_choices').val() == 1 &&
$('#survey_question_max_choices').val() == 1) ? 'radio' : 'checkbox';
$.each(options_array, function(index, option) {
options_html += '<input type="' + input_type + '" name="preview_option"/> ' + option.trim() + '<br/>';
});
$('#survey_question_preview .choice').html(options_html)
};
$('#survey_question_possible_answers').on('keyup', render_possible_answers_preview);
$('#survey_question_min_choices').on('change', render_possible_answers_preview);
$('#survey_question_max_choices').on('change', render_possible_answers_preview);
}); });

View file

@ -11,13 +11,16 @@ module Admin
end end
def new def new
@survey_question = @survey.survey_questions.new @survey_question = @survey.survey_questions.new(min_choices: 1, max_choices: 1)
@url = admin_conference_survey_survey_questions_path(@conference.short_title, @survey) @url = admin_conference_survey_survey_questions_path(@conference.short_title, @survey)
end end
def create def create
@survey.survey_questions.create(survey_question_params) if @survey.survey_questions.create(survey_question_params)
redirect_to admin_conference_survey_survey_questions_path(@conference.short_title, @survey) redirect_to admin_conference_survey_path(@conference.short_title, @survey), notice: 'Successfully created Survey Question.'
else
render :new
end
end end
# GET questions/1/edit # GET questions/1/edit
@ -28,14 +31,20 @@ module Admin
# PUT questions/1 # PUT questions/1
def update def update
if @survey_question.update_attributes(survey_question_params) if @survey_question.update_attributes(survey_question_params)
redirect_to admin_conference_survey_survey_questions_path(@conference.short_title, @survey), notice: 'Successfully updated survey question.' redirect_to admin_conference_survey_path(@conference.short_title, @survey), notice: 'Successfully updated Survey Question.'
else else
@url = admin_conference_survey_survey_question_path(@conference.short_title, @survey, @survey_question)
render :edit render :edit
end end
end end
# DELETE questions/1 # DELETE questions/1
def destroy def destroy
if @survey_question.destroy
redirect_to admin_conference_survey_path(@conference.short_title, @survey), notice: 'Successfully deleted Survey Question.'
else
redirect_to admin_conference_survey_path(@conference.short_title, @survey), error: "Can't delete this Survey Question"
end
end end
private private

View file

@ -24,8 +24,12 @@ module Admin
end end
def update def update
@survey.update_attributes(survey_params) if @survey.update_attributes(survey_params)
redirect_to admin_conference_surveys_path(@conference.short_title) redirect_to admin_conference_surveys_path(@conference.short_title)
else
@url = admin_conference_survey_path(@conference.short_title, @survey)
render action: :edit
end
end end
def destroy def destroy

View file

@ -34,7 +34,11 @@ class Conference < ActiveRecord::Base
has_many :campaigns, dependent: :destroy has_many :campaigns, dependent: :destroy
has_many :commercials, as: :commercialable, dependent: :destroy has_many :commercials, as: :commercialable, dependent: :destroy
has_many :subscriptions, dependent: :destroy has_many :subscriptions, dependent: :destroy
has_many :surveys, as: :surveyable, dependent: :destroy has_many :surveys, as: :surveyable, dependent: :destroy do
def for_registration
where(target: targets[:registration])
end
end
accepts_nested_attributes_for :venue accepts_nested_attributes_for :venue
accepts_nested_attributes_for :tickets, allow_destroy: true accepts_nested_attributes_for :tickets, allow_destroy: true

View file

@ -2,5 +2,7 @@ class Survey < ActiveRecord::Base
belongs_to :surveyable, polymorphic: true belongs_to :surveyable, polymorphic: true
has_many :survey_questions has_many :survey_questions
enum target: [:conference, :registration]
validates :title, presence: true validates :title, presence: true
end end

View file

@ -2,9 +2,40 @@ class SurveyQuestion < ActiveRecord::Base
belongs_to :survey belongs_to :survey
# Order of this list should not be changed without proper action! # Order of this list should not be changed without proper action!
enum type: [:boolean, :choice, :string, :text, :datetime, :numeric] enum kind: [:boolean, :choice, :string, :text, :datetime, :numeric]
ICONS = { boolean: 'dot-circle-o', choice: 'check-square-o', string: 'edit', text: 'align-left', datetime: 'clock-o', numeric: 'slack' } ICONS = { boolean: 'dot-circle-o', choice: 'check-square-o', string: 'edit', text: 'align-left', datetime: 'clock-o', numeric: 'slack' }
validates :title, presence: true validates :title, presence: true
validates :possible_answers, :max_choices, :min_choices, presence: true, if: "choice?"
validates :min_choices, numericality: { greater_than_or_equal_to: 1 }, allow_blank: true
validates :max_choices, numericality: { greater_than_or_equal_to: 1 }, allow_blank: true
validate :max_choices_greater_than_min
def single_choice?
choice? && max_choices == 1 && min_choices == 1
end
def multiple_choice?
choice? && max_choices > 1
end
def possible_answers=(value)
write_attribute(:possible_answers, choice? ? value : nil)
end
def min_choices=(value)
write_attribute(:min_choices, choice? ? value : nil)
end
def max_choices=(value)
write_attribute(:max_choices, choice? ? value : nil)
end
private
def max_choices_greater_than_min
errors.add(:max_choices, "Max choices should not be less than min choices") if choice? && max_choices.to_i < min_choices.to_i
end
end end

View file

@ -17,8 +17,13 @@
.col-md-12 .col-md-12
= f.input :title = f.input :title
= f.input :mandatory = f.input :mandatory
%div.survey-possible-answers.hidden %div.survey-possible-answers{ class: @survey_question.choice? ? '' : 'hidden'}
= f.input :possible_answers, hint: 'Comma separated', input_html: { rows: 3 } = f.input :possible_answers, hint: 'Comma separated', input_html: { rows: 3 }
.row
.col-md-6
= f.input :min_choices
.col-md-6
= f.input :max_choices
%hr %hr
.row .row
.col-md-6 .col-md-6
@ -26,38 +31,62 @@
Type of Question: Type of Question:
.form-group .form-group
%select.selectpicker.form-control{ id: 'survey_question_kind', name: 'survey_question[kind]' } %select.selectpicker.form-control{ id: 'survey_question_kind', name: 'survey_question[kind]' }
- SurveyQuestion.types.each do |type| - SurveyQuestion.kinds.each do |kind|
%option{ id: "#{type.second}", 'data-icon' => "fa fa-#{SurveyQuestion::ICONS[type.first.to_sym]}" } %option{ id: "#{kind.second}", 'data-icon' => "fa fa-#{SurveyQuestion::ICONS[kind.first.to_sym]}", selected: @survey_question.kind == kind.first }
= type.first = kind.first
.col-md-6 .col-md-6
%label
Preview:
.panel.panel-default .panel.panel-default
.panel-body .panel-body{id: 'survey_question_preview'}
%p %p{id: 'title', style: 'word-wrap: break-word'}
What is your answer? = @survey_question.title.blank? ? 'What is your answer?' : @survey_question.title
%div.kinds.boolean %div.kinds.boolean{ class: @survey_question.boolean? ? '' : 'hidden'}
%input{type: 'radio', name: 'radio'} Yes %input{type: 'radio', name: 'radio'} Yes
%br %br
%input{type: 'radio', name: 'radio'} No %input{type: 'radio', name: 'radio'} No
%div.kinds.choice.hidden %div.kinds.choice{ id: '', class: @survey_question.choice? ? '' : 'hidden'}
- if @survey_question.possible_answers.blank?
- if @survey_question.single_choice?
%input{ type: 'radio', name: 'radio' } Choice 1
%br
%input{ type: 'radio', name: 'radio' } Choice 2
%br
%input{ type: 'radio', name: 'radio' } Choice 3
- else
%input{ type: 'checkbox', name: 'checkbox' } Choice 1 %input{ type: 'checkbox', name: 'checkbox' } Choice 1
%br %br
%input{ type: 'checkbox', name: 'checkbox' } Choice 2 %input{ type: 'checkbox', name: 'checkbox' } Choice 2
%br %br
%input{ type: 'checkbox', name: 'checkbox' } Choice 3 %input{ type: 'checkbox', name: 'checkbox' } Choice 3
- else
- @survey_question.possible_answers.split(',').map(&:strip).each do |option|
- if @survey_question.single_choice?
%input{ type: 'radio', name: 'radio' }
= option
%br
- else
%input{ type: 'checkbox', name: 'checkbox' }
= option
%br
%div.kinds.string.hidden %div.kinds.string{ class: @survey_question.string? ? '' : 'hidden'}
%input.form-control %input.form-control
%div.kinds.text.hidden %div.kinds.text{ class: @survey_question.text? ? '' : 'hidden'}
%textarea.form-control{ rows: 4 } %textarea.form-control{ rows: 4 }
%div.kinds.datetime.hidden %div.kinds.datetime{ class: @survey_question.datetime? ? '' : 'hidden'}
%input.form-control{id: 'registration-arrival-datepicker', readonly: 'readonly'} .form-group{class: 'datetimepicker'}
.input-group
.input-group-addon
%span.fa.fa-calendar
%input.form-control{readonly: 'readonly'}
%div.kinds.numeric.hidden %div.kinds.numeric{ class: @survey_question.numeric? ? '' : 'hidden'}
%input.form-control{ type: 'number' } %input.form-control{ type: 'number' }
= f.submit 'Save', class: 'btn btn-primary' = f.submit 'Save', class: 'btn btn-primary'

View file

@ -2,7 +2,8 @@
.col-md-12 .col-md-12
.page-header .page-header
%h1 %h1
Survey Questions Questions for survey
= link_to @survey.title, admin_conference_survey_path(@conference.short_title, @survey)
= "(#{@survey_questions.length})" = "(#{@survey_questions.length})"
- if @survey_questions.any? - if @survey_questions.any?

View file

@ -0,0 +1,50 @@
.panel.panel-default
.panel-heading
%span.badge
= index
= survey_question.title
- if survey_question.multiple_choice?
( Select
- if survey_question.min_choices != survey_question.max_choices
a minimum of
= survey_question.min_choices
- if survey_question.min_choices != survey_question.max_choices
and a maximum of
= survey_question.max_choices
choices )
- if survey_question.mandatory
%span.fa.fa-asterisk.text-danger
%p.pull-right
= link_to edit_admin_conference_survey_survey_question_path(@conference.short_title, @survey, survey_question) do
%span.fa.fa-edit
= link_to admin_conference_survey_survey_question_path(@conference.short_title, @survey, survey_question), method: :delete, data: { confirm: 'Are you sure you want to delete this question?' } do
%span.fa.fa-times
.panel-body
- if survey_question.boolean?
%input{type: 'radio', name: 'radio'}
Yes
%br
%input{type: 'radio', name: 'radio'}
No
- elsif survey_question.choice?
- survey_question.possible_answers.split(',').map(&:strip).each do |option|
- if survey_question.single_choice?
%input{ type: 'radio', name: 'radio' }
= option
%br
- else
%input{ type: 'checkbox', name: 'checkbox' }
= option
%br
- elsif survey_question.string?
%input.form-control
- elsif survey_question.text?
%textarea.form-control{ rows: 4 }
- elsif survey_question.datetime?
.form-group{class: 'datetimepicker'}
.input-group
.input-group-addon
%span.fa.fa-calendar
%input.form-control{readonly: 'readonly'}
- elsif survey_question.numeric?
%input.form-control{ type: 'number' }

View file

@ -21,14 +21,15 @@
- @surveys.each_with_index do |survey, index| - @surveys.each_with_index do |survey, index|
%tr %tr
%td %td
= survey.title = link_to survey.title, admin_conference_survey_path(@conference.short_title, survey)
%td %td
= survey.start_date = survey.start_date
%td %td
= survey.end_date = survey.end_date
%td %td
= link_to 'Edit', edit_admin_conference_survey_path(@conference.short_title, survey.id), .btn-group
method: :get, class: 'btn btn-primary' = link_to 'Edit', edit_admin_conference_survey_path(@conference.short_title, survey),
= link_to 'Delete', admin_conference_survey_path(@conference.short_title, survey.id), class: 'btn btn-primary'
= link_to 'Delete', admin_conference_survey_path(@conference.short_title, survey),
method: :delete, class: 'btn btn-danger', method: :delete, class: 'btn btn-danger',
data: { confirm: "Do you really want to delete #{survey.title}?"} data: { confirm: "Do you really want to delete #{survey.title}?"}

View file

@ -1,6 +1,18 @@
%p#notice= notice .row
.col-md-12
.page-header
= link_to 'Edit', edit_admin_survey_path(@admin_survey) %h1
\| Survey #{@survey.title}
= link_to 'Back', admin_surveys_path = link_to 'Edit Survey', edit_admin_conference_survey_path(@conference.short_title, @survey), class: 'btn btn-primary pull-right'
%p.text-muted
From:
= @survey.start_date
to
= @survey.end_date
.row
.col-md-12
- @survey.survey_questions.each.with_index(1) do |survey_question, index|
.row
.col-md-12
= render partial: 'survey_question', locals: { survey_question: survey_question, index: index }
= link_to 'Add Question', new_admin_conference_survey_survey_question_path(@conference.short_title, @survey), class: 'btn btn-success pull-right'

View file

@ -1,5 +1,5 @@
- if @conference.questions.any? - if @conference.surveys.for_registration.any?
= render partial: 'conference_registrations/questions', locals: { f: f } Something
- if @conference.program.events.with_registration_open.any? || @registration.events.any? - if @conference.program.events.with_registration_open.any? || @registration.events.any?
= f.inputs 'Pre-registration required for the following:' do = f.inputs 'Pre-registration required for the following:' do

View file

@ -133,7 +133,7 @@
E-Mails E-Mails
%li{ class: active_nav_li(admin_conference_surveys_path(@conference.short_title)) } %li{ class: active_nav_li(admin_conference_surveys_path(@conference.short_title)) }
= link_to(admin_conference_surveys_path(@conference.short_title)) do = link_to(admin_conference_surveys_path(@conference.short_title)) do
%span.fa.fa-group %span.fa.fa-list-alt
Surveys Surveys
- if can? :index, Role.new(resource: @conference) - if can? :index, Role.new(resource: @conference)
%li{:class=> active_nav_li(admin_conference_roles_path(@conference.short_title))} %li{:class=> active_nav_li(admin_conference_roles_path(@conference.short_title))}

View file

@ -0,0 +1,5 @@
class AddTargetToSurveys < ActiveRecord::Migration
def change
add_column :surveys, :target, :integer, default: 0
end
end

View file

@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160628093634) do ActiveRecord::Schema.define(version: 20160629145954) do
create_table "ahoy_events", force: :cascade do |t| create_table "ahoy_events", force: :cascade do |t|
t.uuid "visit_id", limit: 16 t.uuid "visit_id", limit: 16
@ -394,6 +394,7 @@ ActiveRecord::Schema.define(version: 20160628093634) do
t.string "surveyable_type" t.string "surveyable_type"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.integer "target", default: 0
end end
add_index "surveys", ["surveyable_type", "surveyable_id"], name: "index_surveys_on_surveyable_type_and_surveyable_id" add_index "surveys", ["surveyable_type", "surveyable_id"], name: "index_surveys_on_surveyable_type_and_surveyable_id"