Improving surveys system: showing surveys in admin UI as they will be for the user
This commit is contained in:
parent
335608e6c4
commit
03168b4efe
15 changed files with 213 additions and 46 deletions
|
|
@ -12,4 +12,22 @@ $(function() {
|
|||
$('.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);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -11,13 +11,16 @@ module Admin
|
|||
end
|
||||
|
||||
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)
|
||||
end
|
||||
|
||||
def create
|
||||
@survey.survey_questions.create(survey_question_params)
|
||||
redirect_to admin_conference_survey_survey_questions_path(@conference.short_title, @survey)
|
||||
if @survey.survey_questions.create(survey_question_params)
|
||||
redirect_to admin_conference_survey_path(@conference.short_title, @survey), notice: 'Successfully created Survey Question.'
|
||||
else
|
||||
render :new
|
||||
end
|
||||
end
|
||||
|
||||
# GET questions/1/edit
|
||||
|
|
@ -28,14 +31,20 @@ module Admin
|
|||
# PUT questions/1
|
||||
def update
|
||||
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
|
||||
@url = admin_conference_survey_survey_question_path(@conference.short_title, @survey, @survey_question)
|
||||
render :edit
|
||||
end
|
||||
end
|
||||
|
||||
# DELETE questions/1
|
||||
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
|
||||
|
||||
private
|
||||
|
|
|
|||
|
|
@ -24,8 +24,12 @@ module Admin
|
|||
end
|
||||
|
||||
def update
|
||||
@survey.update_attributes(survey_params)
|
||||
redirect_to admin_conference_surveys_path(@conference.short_title)
|
||||
if @survey.update_attributes(survey_params)
|
||||
redirect_to admin_conference_surveys_path(@conference.short_title)
|
||||
else
|
||||
@url = admin_conference_survey_path(@conference.short_title, @survey)
|
||||
render action: :edit
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
|
|
|
|||
|
|
@ -34,7 +34,11 @@ class Conference < ActiveRecord::Base
|
|||
has_many :campaigns, dependent: :destroy
|
||||
has_many :commercials, as: :commercialable, 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 :tickets, allow_destroy: true
|
||||
|
|
|
|||
|
|
@ -2,5 +2,7 @@ class Survey < ActiveRecord::Base
|
|||
belongs_to :surveyable, polymorphic: true
|
||||
has_many :survey_questions
|
||||
|
||||
enum target: [:conference, :registration]
|
||||
|
||||
validates :title, presence: true
|
||||
end
|
||||
|
|
|
|||
|
|
@ -2,9 +2,40 @@ class SurveyQuestion < ActiveRecord::Base
|
|||
belongs_to :survey
|
||||
|
||||
# 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' }
|
||||
|
||||
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
|
||||
|
|
|
|||
|
|
@ -17,8 +17,13 @@
|
|||
.col-md-12
|
||||
= f.input :title
|
||||
= 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 }
|
||||
.row
|
||||
.col-md-6
|
||||
= f.input :min_choices
|
||||
.col-md-6
|
||||
= f.input :max_choices
|
||||
%hr
|
||||
.row
|
||||
.col-md-6
|
||||
|
|
@ -26,38 +31,62 @@
|
|||
Type of Question:
|
||||
.form-group
|
||||
%select.selectpicker.form-control{ id: 'survey_question_kind', name: 'survey_question[kind]' }
|
||||
- SurveyQuestion.types.each do |type|
|
||||
%option{ id: "#{type.second}", 'data-icon' => "fa fa-#{SurveyQuestion::ICONS[type.first.to_sym]}" }
|
||||
= type.first
|
||||
- SurveyQuestion.kinds.each do |kind|
|
||||
%option{ id: "#{kind.second}", 'data-icon' => "fa fa-#{SurveyQuestion::ICONS[kind.first.to_sym]}", selected: @survey_question.kind == kind.first }
|
||||
= kind.first
|
||||
.col-md-6
|
||||
%label
|
||||
Preview:
|
||||
.panel.panel-default
|
||||
.panel-body
|
||||
%p
|
||||
What is your answer?
|
||||
.panel-body{id: 'survey_question_preview'}
|
||||
%p{id: 'title', style: 'word-wrap: break-word'}
|
||||
= @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
|
||||
%br
|
||||
%input{type: 'radio', name: 'radio'} No
|
||||
|
||||
%div.kinds.choice.hidden
|
||||
%input{ type: 'checkbox', name: 'checkbox' } Choice 1
|
||||
%br
|
||||
%input{ type: 'checkbox', name: 'checkbox' } Choice 2
|
||||
%br
|
||||
%input{ type: 'checkbox', name: 'checkbox' } Choice 3
|
||||
%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
|
||||
%br
|
||||
%input{ type: 'checkbox', name: 'checkbox' } Choice 2
|
||||
%br
|
||||
%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
|
||||
|
||||
%div.kinds.text.hidden
|
||||
%div.kinds.text{ class: @survey_question.text? ? '' : 'hidden'}
|
||||
%textarea.form-control{ rows: 4 }
|
||||
|
||||
|
||||
%div.kinds.datetime.hidden
|
||||
%input.form-control{id: 'registration-arrival-datepicker', readonly: 'readonly'}
|
||||
%div.kinds.datetime{ class: @survey_question.datetime? ? '' : 'hidden'}
|
||||
.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' }
|
||||
|
||||
= f.submit 'Save', class: 'btn btn-primary'
|
||||
|
|
|
|||
|
|
@ -2,7 +2,8 @@
|
|||
.col-md-12
|
||||
.page-header
|
||||
%h1
|
||||
Survey Questions
|
||||
Questions for survey
|
||||
= link_to @survey.title, admin_conference_survey_path(@conference.short_title, @survey)
|
||||
= "(#{@survey_questions.length})"
|
||||
|
||||
- if @survey_questions.any?
|
||||
|
|
|
|||
50
app/views/admin/surveys/_survey_question.html.haml
Normal file
50
app/views/admin/surveys/_survey_question.html.haml
Normal 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' }
|
||||
|
|
@ -21,14 +21,15 @@
|
|||
- @surveys.each_with_index do |survey, index|
|
||||
%tr
|
||||
%td
|
||||
= survey.title
|
||||
= link_to survey.title, admin_conference_survey_path(@conference.short_title, survey)
|
||||
%td
|
||||
= survey.start_date
|
||||
%td
|
||||
= survey.end_date
|
||||
%td
|
||||
= link_to 'Edit', edit_admin_conference_survey_path(@conference.short_title, survey.id),
|
||||
method: :get, class: 'btn btn-primary'
|
||||
= link_to 'Delete', admin_conference_survey_path(@conference.short_title, survey.id),
|
||||
method: :delete, class: 'btn btn-danger',
|
||||
data: { confirm: "Do you really want to delete #{survey.title}?"}
|
||||
.btn-group
|
||||
= link_to 'Edit', edit_admin_conference_survey_path(@conference.short_title, survey),
|
||||
class: 'btn btn-primary'
|
||||
= link_to 'Delete', admin_conference_survey_path(@conference.short_title, survey),
|
||||
method: :delete, class: 'btn btn-danger',
|
||||
data: { confirm: "Do you really want to delete #{survey.title}?"}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,18 @@
|
|||
%p#notice= notice
|
||||
|
||||
|
||||
= link_to 'Edit', edit_admin_survey_path(@admin_survey)
|
||||
\|
|
||||
= link_to 'Back', admin_surveys_path
|
||||
.row
|
||||
.col-md-12
|
||||
.page-header
|
||||
%h1
|
||||
Survey #{@survey.title}
|
||||
= 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'
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
- if @conference.questions.any?
|
||||
= render partial: 'conference_registrations/questions', locals: { f: f }
|
||||
- if @conference.surveys.for_registration.any?
|
||||
Something
|
||||
- if @conference.program.events.with_registration_open.any? || @registration.events.any?
|
||||
= f.inputs 'Pre-registration required for the following:' do
|
||||
|
||||
|
|
|
|||
|
|
@ -133,7 +133,7 @@
|
|||
E-Mails
|
||||
%li{ class: active_nav_li(admin_conference_surveys_path(@conference.short_title)) }
|
||||
= link_to(admin_conference_surveys_path(@conference.short_title)) do
|
||||
%span.fa.fa-group
|
||||
%span.fa.fa-list-alt
|
||||
Surveys
|
||||
- if can? :index, Role.new(resource: @conference)
|
||||
%li{:class=> active_nav_li(admin_conference_roles_path(@conference.short_title))}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue