From 03168b4efe525b37bcae5f4d742f095ce41cb76b Mon Sep 17 00:00:00 2001 From: mdeniz Date: Wed, 29 Jun 2016 17:23:01 +0200 Subject: [PATCH] Improving surveys system: showing surveys in admin UI as they will be for the user --- app/assets/javascripts/osem-survey.js | 18 +++++ .../admin/survey_questions_controller.rb | 17 +++-- app/controllers/admin/surveys_controller.rb | 8 ++- app/models/conference.rb | 6 +- app/models/survey.rb | 2 + app/models/survey_question.rb | 33 ++++++++- .../admin/survey_questions/_form.html.haml | 67 +++++++++++++------ .../admin/survey_questions/index.html.haml | 3 +- .../admin/surveys/_survey_question.html.haml | 50 ++++++++++++++ app/views/admin/surveys/index.html.haml | 13 ++-- app/views/admin/surveys/show.html.haml | 24 +++++-- .../_registration_info.html.haml | 4 +- app/views/layouts/_admin_sidebar.html.haml | 2 +- .../20160629145954_add_target_to_surveys.rb | 5 ++ db/schema.rb | 7 +- 15 files changed, 213 insertions(+), 46 deletions(-) create mode 100644 app/views/admin/surveys/_survey_question.html.haml create mode 100644 db/migrate/20160629145954_add_target_to_surveys.rb diff --git a/app/assets/javascripts/osem-survey.js b/app/assets/javascripts/osem-survey.js index 17c3ecb0..20ed2a19 100644 --- a/app/assets/javascripts/osem-survey.js +++ b/app/assets/javascripts/osem-survey.js @@ -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 += ' ' + option.trim() + '
'; + }); + $('#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); }); diff --git a/app/controllers/admin/survey_questions_controller.rb b/app/controllers/admin/survey_questions_controller.rb index a4380fb6..2f1d016f 100644 --- a/app/controllers/admin/survey_questions_controller.rb +++ b/app/controllers/admin/survey_questions_controller.rb @@ -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 diff --git a/app/controllers/admin/surveys_controller.rb b/app/controllers/admin/surveys_controller.rb index be8d8018..4e99a96d 100644 --- a/app/controllers/admin/surveys_controller.rb +++ b/app/controllers/admin/surveys_controller.rb @@ -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 diff --git a/app/models/conference.rb b/app/models/conference.rb index eb9457eb..15352867 100644 --- a/app/models/conference.rb +++ b/app/models/conference.rb @@ -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 diff --git a/app/models/survey.rb b/app/models/survey.rb index df05d33b..d08df5dc 100644 --- a/app/models/survey.rb +++ b/app/models/survey.rb @@ -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 diff --git a/app/models/survey_question.rb b/app/models/survey_question.rb index 07d48bfb..1b10fdb7 100644 --- a/app/models/survey_question.rb +++ b/app/models/survey_question.rb @@ -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 diff --git a/app/views/admin/survey_questions/_form.html.haml b/app/views/admin/survey_questions/_form.html.haml index f5ef3057..7cd7bbdb 100644 --- a/app/views/admin/survey_questions/_form.html.haml +++ b/app/views/admin/survey_questions/_form.html.haml @@ -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' diff --git a/app/views/admin/survey_questions/index.html.haml b/app/views/admin/survey_questions/index.html.haml index 1967d1d9..54b1116a 100644 --- a/app/views/admin/survey_questions/index.html.haml +++ b/app/views/admin/survey_questions/index.html.haml @@ -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? diff --git a/app/views/admin/surveys/_survey_question.html.haml b/app/views/admin/surveys/_survey_question.html.haml new file mode 100644 index 00000000..399ebd27 --- /dev/null +++ b/app/views/admin/surveys/_survey_question.html.haml @@ -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' } diff --git a/app/views/admin/surveys/index.html.haml b/app/views/admin/surveys/index.html.haml index b5f6475f..17320355 100644 --- a/app/views/admin/surveys/index.html.haml +++ b/app/views/admin/surveys/index.html.haml @@ -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}?"} diff --git a/app/views/admin/surveys/show.html.haml b/app/views/admin/surveys/show.html.haml index b9b2019a..f60d3653 100644 --- a/app/views/admin/surveys/show.html.haml +++ b/app/views/admin/surveys/show.html.haml @@ -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' diff --git a/app/views/conference_registrations/_registration_info.html.haml b/app/views/conference_registrations/_registration_info.html.haml index 0bf15c71..c1015d08 100644 --- a/app/views/conference_registrations/_registration_info.html.haml +++ b/app/views/conference_registrations/_registration_info.html.haml @@ -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 diff --git a/app/views/layouts/_admin_sidebar.html.haml b/app/views/layouts/_admin_sidebar.html.haml index d112ae5c..ab48150f 100644 --- a/app/views/layouts/_admin_sidebar.html.haml +++ b/app/views/layouts/_admin_sidebar.html.haml @@ -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))} diff --git a/db/migrate/20160629145954_add_target_to_surveys.rb b/db/migrate/20160629145954_add_target_to_surveys.rb new file mode 100644 index 00000000..16c10b8d --- /dev/null +++ b/db/migrate/20160629145954_add_target_to_surveys.rb @@ -0,0 +1,5 @@ +class AddTargetToSurveys < ActiveRecord::Migration + def change + add_column :surveys, :target, :integer, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index ae721ddd..da777c4f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # 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| t.uuid "visit_id", limit: 16 @@ -392,8 +392,9 @@ ActiveRecord::Schema.define(version: 20160628093634) do t.text "description" t.integer "surveyable_id" t.string "surveyable_type" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.integer "target", default: 0 end add_index "surveys", ["surveyable_type", "surveyable_id"], name: "index_surveys_on_surveyable_type_and_surveyable_id"