diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 915cb98..dae2c86 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -21,8 +21,6 @@ def index end end - def edit; end - def create if params[:archive].present? UserBulkImportJob.perform_later create_blob, current_user @@ -32,12 +30,14 @@ def create redirect_to admin_users_path end + def edit; end + def update if @user.update user_params flash[:success] = t '.success' redirect_to admin_users_path else - render :edit + render :edit, status: :unprocessable_entity end end @@ -71,4 +71,4 @@ def authorize_user! authorize(@user || User) end end -end +end \ No newline at end of file diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index 1f32397..793078c 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -7,45 +7,69 @@ class QuestionsController < ApplicationController before_action :authorize_question! after_action :verify_authorized - def index - @tags = Tag.where(id: params[:tag_ids]) if params[:tag_ids] - @pagy, @questions = pagy Question.all_by_tags(@tags) - @questions = @questions.decorate - end - def show load_question_answers end - def new - @question = Question.new - end - - def edit; end + def destroy + @question.destroy + respond_to do |format| + format.html do + flash[:success] = t('.success') + redirect_to questions_path, status: :see_other + end - def create - @question = current_user.questions.build question_params - if @question.save - flash[:success] = t('.success') - redirect_to questions_path - else - render :new, status: :unprocessable_entity + format.turbo_stream { flash.now[:success] = t('.success') } end end + def edit; end + def update if @question.update question_params - flash[:success] = t('.success') - redirect_to questions_path + respond_to do |format| + format.html do + flash[:success] = t('.success') + redirect_to questions_path + end + + format.turbo_stream do + @question = @question.decorate + flash.now[:success] = t('.success') + end + end else render :edit, status: :unprocessable_entity end end - def destroy - @question.destroy - flash[:success] = t('.success') - redirect_to questions_path, status: :see_other + def index + @tags = Tag.where(id: params[:tag_ids]) if params[:tag_ids] + @pagy, @questions = pagy Question.all_by_tags(@tags), link_extra: 'data-turbo-frame="pagination_pagy"' + @questions = @questions.decorate + end + + def new + @question = Question.new + end + + def create + @question = current_user.questions.build question_params + if @question.save + respond_to do |format| + format.html do + flash[:success] = t('.success') + redirect_to questions_path + end + + format.turbo_stream do + @question = @question.decorate + flash.now[:success] = t('.success') + end + end + else + render :new + end end private @@ -61,4 +85,4 @@ def set_question! def authorize_question! authorize(@question || Question) end -end +end \ No newline at end of file diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 50be8ce..5104889 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -3,6 +3,10 @@ module ApplicationHelper include Pagy::Frontend + def prepend_flash + turbo_stream.prepend 'flash', partial: 'shared/flash' + end + def pagination(obj) # rubocop:disable Rails/OutputSafety raw(pagy_bootstrap_nav(obj)) if obj.pages > 1 diff --git a/app/javascript/scripts/select.js b/app/javascript/scripts/select.js index e061a7a..b853f1e 100644 --- a/app/javascript/scripts/select.js +++ b/app/javascript/scripts/select.js @@ -12,41 +12,44 @@ document.addEventListener("turbo:before-cache", function() { const rerender = function() { const i18n = Translations[document.querySelector('body').dataset.lang] - document.querySelectorAll('.js-multiple-select').forEach((element) => { - let opts = { - plugins: { - 'remove_button': { - title: i18n['remove_button'] + document.querySelectorAll('select.js-multiple-select').forEach((element) => { + if(!element.classList.contains('tomselected')) { + let opts = { + plugins: { + 'remove_button': { + title: i18n['remove_button'] + }, + 'no_backspace_delete': {}, + 'restore_on_backspace': {} }, - 'no_backspace_delete': {}, - 'restore_on_backspace': {} - }, - valueField: 'id', - labelField: 'title', - searchField: 'title', - create: false, - load: function(query, callback) { - const url = element.dataset.ajaxUrl + '.json?term=' + encodeURIComponent(query) + valueField: 'id', + labelField: 'title', + searchField: 'title', + create: false, + load: function(query, callback) { + const url = element.dataset.ajaxUrl + '.json?term=' + encodeURIComponent(query) - fetch(url) - .then(response => response.json()) - .then (json => { - callback(json) - }).catch(() => { - callback() - }) - }, - render: { - no_results: function(_data, _escape){ - return '