Order in the admin page using jquery-ui sortable widget.

This commit is contained in:
iagirre
2017-10-09 17:36:48 +02:00
parent b95dc572c5
commit 2b10b59e2a
9 changed files with 61 additions and 25 deletions

View File

@@ -15,6 +15,7 @@
//= require jquery-ui/widgets/datepicker //= require jquery-ui/widgets/datepicker
//= require jquery-ui/i18n/datepicker-es //= require jquery-ui/i18n/datepicker-es
//= require jquery-ui/widgets/autocomplete //= require jquery-ui/widgets/autocomplete
//= require jquery-ui/widgets/sortable
//= require jquery-fileupload/basic //= require jquery-fileupload/basic
//= require foundation //= require foundation
//= require turbolinks //= require turbolinks
@@ -71,6 +72,7 @@
//= require leaflet //= require leaflet
//= require map //= require map
//= require polls //= require polls
//= require sortable
var initialize_modules = function() { var initialize_modules = function() {
App.Comments.initialize(); App.Comments.initialize();
@@ -110,6 +112,7 @@ var initialize_modules = function() {
App.PollsAdmin.initialize(); App.PollsAdmin.initialize();
App.Map.initialize(); App.Map.initialize();
App.Polls.initialize(); App.Polls.initialize();
App.Sortable.initialize();
}; };
$(function(){ $(function(){

View File

@@ -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'

View File

@@ -18,4 +18,5 @@
@import 'datepicker_overrides'; @import 'datepicker_overrides';
@import 'jquery-ui/autocomplete'; @import 'jquery-ui/autocomplete';
@import 'autocomplete_overrides'; @import 'autocomplete_overrides';
@import 'jquery-ui/sortable';
@import 'leaflet'; @import 'leaflet';

View File

@@ -39,6 +39,12 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController
render 'admin/poll/questions/answers/documents' render 'admin/poll/questions/answers/documents'
end 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 private
def answer_params def answer_params

View File

@@ -14,4 +14,12 @@ class Poll::Question::Answer < ActiveRecord::Base
def description def description
super.try :html_safe super.try :html_safe
end 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 end

View File

@@ -47,8 +47,9 @@
<th scope="col" class="text-center"><%= t("admin.questions.show.answers.videos") %></th> <th scope="col" class="text-center"><%= t("admin.questions.show.answers.videos") %></th>
</tr> </tr>
<tbody class="sortable" data-js-url="<%= admin_question_answers_order_answers_path(@question.id) %>">
<% @question.question_answers.each do |answer| %> <% @question.question_answers.each do |answer| %>
<tr id="<%= dom_id(answer) %>" class="poll_question_answer"> <tr id="<%= dom_id(answer) %>" class="poll_question_answer" data-answer-id="<%= answer.id %>">
<td><%= link_to answer.title, admin_answer_path(answer) %></td> <td><%= link_to answer.title, admin_answer_path(answer) %></td>
<td><%= answer.description %></td> <td><%= answer.description %></td>
<td class="text-center"> <td class="text-center">
@@ -71,6 +72,7 @@
</td> </td>
</tr> </tr>
<% end %> <% end %>
</tbody>
</table> </table>
<% if @question.video_url.present? %> <% if @question.video_url.present? %>

View File

@@ -306,6 +306,7 @@ Rails.application.routes.draw do
resources :videos, controller: 'questions/answers/videos' resources :videos, controller: 'questions/answers/videos'
get :documents, to: 'questions/answers#documents' get :documents, to: 'questions/answers#documents'
end end
post '/answers/order_answers', to: 'questions/answers#order_answers'
end end
end end

View File

@@ -0,0 +1,5 @@
class AddGivenOrderToPollQuestionAnswers < ActiveRecord::Migration
def change
add_column :poll_question_answers, :given_order, :integer, default: 1
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: 20171006145053) do ActiveRecord::Schema.define(version: 20171010143623) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@@ -680,6 +680,7 @@ ActiveRecord::Schema.define(version: 20171006145053) do
t.string "title" t.string "title"
t.text "description" t.text "description"
t.integer "question_id" t.integer "question_id"
t.integer "given_order", default: 1
end end
add_index "poll_question_answers", ["question_id"], name: "index_poll_question_answers_on_question_id", using: :btree add_index "poll_question_answers", ["question_id"], name: "index_poll_question_answers_on_question_id", using: :btree