diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index ce9861547..51de9c678 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -15,6 +15,7 @@ //= require jquery-ui/widgets/datepicker //= require jquery-ui/i18n/datepicker-es //= require jquery-ui/widgets/autocomplete +//= require jquery-ui/widgets/sortable //= require jquery-fileupload/basic //= require foundation //= require turbolinks @@ -71,6 +72,7 @@ //= require leaflet //= require map //= require polls +//= require sortable var initialize_modules = function() { App.Comments.initialize(); @@ -110,6 +112,7 @@ var initialize_modules = function() { App.PollsAdmin.initialize(); App.Map.initialize(); App.Polls.initialize(); + App.Sortable.initialize(); }; $(function(){ diff --git a/app/assets/javascripts/sortable.js.coffee b/app/assets/javascripts/sortable.js.coffee new file mode 100644 index 000000000..1af543f6a --- /dev/null +++ b/app/assets/javascripts/sortable.js.coffee @@ -0,0 +1,9 @@ +App.Sortable = + initialize: -> + $(".sortable").sortable + update: (event, ui) -> + new_order = $(this).sortable('toArray', {attribute: 'data-answer-id'}); + $.ajax + url: $('.sortable').data('js-url'), + data: {ordered_list: new_order}, + type: 'POST' diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index aff55c88c..3f93ffa6b 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -18,4 +18,5 @@ @import 'datepicker_overrides'; @import 'jquery-ui/autocomplete'; @import 'autocomplete_overrides'; +@import 'jquery-ui/sortable'; @import 'leaflet'; diff --git a/app/controllers/admin/poll/questions/answers_controller.rb b/app/controllers/admin/poll/questions/answers_controller.rb index 51c44dd94..a2490475b 100644 --- a/app/controllers/admin/poll/questions/answers_controller.rb +++ b/app/controllers/admin/poll/questions/answers_controller.rb @@ -37,6 +37,12 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController @documents = @answer.documents render 'admin/poll/questions/answers/documents' + end + + def order_answers + ::Poll::Question::Answer.order_answers(params[:ordered_list]) + #redirect_to admin_question_path(params[:question_id]) + render :nothing => true end private diff --git a/app/models/poll/question/answer.rb b/app/models/poll/question/answer.rb index b3324e57f..ba7fe366a 100644 --- a/app/models/poll/question/answer.rb +++ b/app/models/poll/question/answer.rb @@ -14,4 +14,12 @@ class Poll::Question::Answer < ActiveRecord::Base def description super.try :html_safe end + + def self.order_answers(ordered_array) + ordered_array.each_with_index do |answer_id, order| + answer = self.find(answer_id) + answer.update_attribute(:given_order, (order + 1)) + answer.save + end + end end diff --git a/app/views/admin/poll/questions/show.html.erb b/app/views/admin/poll/questions/show.html.erb index 5baabeafe..205b707fb 100644 --- a/app/views/admin/poll/questions/show.html.erb +++ b/app/views/admin/poll/questions/show.html.erb @@ -47,30 +47,32 @@ <%= t("admin.questions.show.answers.videos") %> - <% @question.question_answers.each do |answer| %> - - <%= link_to answer.title, admin_answer_path(answer) %> - <%= answer.description %> - - (<%= answer.images.count %>) -
- <%= link_to t("admin.questions.show.answers.images_list"), - admin_answer_images_path(answer) %> - - - (<%= answer.documents.count rescue 0 %>) -
- <%= link_to t("admin.questions.show.answers.documents_list"), - admin_answer_documents_path(answer) %> - - - (<%= answer.videos.count %>) -
- <%= link_to t("admin.questions.show.answers.video_list"), - admin_answer_videos_path(answer) %> - - - <% end %> + + <% @question.question_answers.each do |answer| %> + + <%= link_to answer.title, admin_answer_path(answer) %> + <%= answer.description %> + + (<%= answer.images.count %>) +
+ <%= link_to t("admin.questions.show.answers.images_list"), + admin_answer_images_path(answer) %> + + + (<%= answer.documents.count rescue 0 %>) +
+ <%= link_to t("admin.questions.show.answers.documents_list"), + admin_answer_documents_path(answer) %> + + + (<%= answer.videos.count %>) +
+ <%= link_to t("admin.questions.show.answers.video_list"), + admin_answer_videos_path(answer) %> + + + <% end %> + <% if @question.video_url.present? %> diff --git a/config/routes.rb b/config/routes.rb index 83b5754d7..1fb74aa53 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -306,6 +306,7 @@ Rails.application.routes.draw do resources :videos, controller: 'questions/answers/videos' get :documents, to: 'questions/answers#documents' end + post '/answers/order_answers', to: 'questions/answers#order_answers' end end diff --git a/db/migrate/20171010143623_add_given_order_to_poll_question_answers.rb b/db/migrate/20171010143623_add_given_order_to_poll_question_answers.rb new file mode 100644 index 000000000..6161e55e4 --- /dev/null +++ b/db/migrate/20171010143623_add_given_order_to_poll_question_answers.rb @@ -0,0 +1,5 @@ +class AddGivenOrderToPollQuestionAnswers < ActiveRecord::Migration + def change + add_column :poll_question_answers, :given_order, :integer, default: 1 + end +end diff --git a/db/schema.rb b/db/schema.rb index 9adba12b1..b9a1524ab 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: 20171006145053) do +ActiveRecord::Schema.define(version: 20171010143623) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -680,6 +680,7 @@ ActiveRecord::Schema.define(version: 20171006145053) do t.string "title" t.text "description" t.integer "question_id" + t.integer "given_order", default: 1 end add_index "poll_question_answers", ["question_id"], name: "index_poll_question_answers_on_question_id", using: :btree