From e9ae442431070d94e740c4f7b1bae5e6be72a3d8 Mon Sep 17 00:00:00 2001 From: pixeldesu Date: Fri, 17 Jul 2015 20:29:19 +0200 Subject: [PATCH] add ability to show/search questions from specific users --- app/assets/javascripts/inbox.coffee | 43 ++++++++++++++++++++++++ app/controllers/ajax/inbox_controller.rb | 19 +++++++++++ app/controllers/inbox_controller.rb | 22 +++++++++++- app/views/inbox/_sidebar.html.haml | 11 +++++- config/routes.rb | 2 ++ 5 files changed, 95 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/inbox.coffee b/app/assets/javascripts/inbox.coffee index d6440e07..67011af3 100644 --- a/app/assets/javascripts/inbox.coffee +++ b/app/assets/javascripts/inbox.coffee @@ -18,6 +18,12 @@ complete: (jqxhr, status) -> btn.button "reset" +($ document).on "click", "button#ib-author", -> + $('#author_form').submit -> + query = $('#author').val() + window.location.href = '/inbox/' + query + false + ($ document).on "click", "button#ib-delete-all", -> btn = ($ this) @@ -56,6 +62,43 @@ btn.removeClass 'disabled' btn[0].dataset.ibCount = 0 +($ document).on "click", "button#ib-delete-all-author", -> + btn = ($ this) + count = btn[0].dataset.ibCount + swal + title: translate('frontend.inbox.confirm_all.title', {count: count}) + text: translate('frontend.inbox.confirm_all.text') + type: "warning" + showCancelButton: true + confirmButtonColor: "#DD6B55" + confirmButtonText: translate('views.actions.delete') + cancelButtonText: translate('views.actions.cancel') + closeOnConfirm: true + , -> + btn.button "loading" + succ = no + $.ajax + url: "/ajax/delete_all_inbox/#{location.pathname.split('/')[2]}" + type: 'POST' + dataType: 'json' + success: (data, status, jqxhr) -> + if data.success + succ = yes + ($ "div#pagination, button#load-more-btn").slideUp() + entries = ($ "div#entries") + entries.slideUp 400, -> + entries.html("Nothing to see here.") + entries.fadeIn() + error: (jqxhr, status, error) -> + console.log jqxhr, status, error + showNotification translate('frontend.error.message'), false + complete: (jqxhr, status) -> + if succ +# and now: a (broken?) re-implementation of Bootstrap's button.js + btn.html btn.data('resetText') + btn.removeClass 'disabled' + btn[0].dataset.ibCount = 0 + $(document).on "keydown", "textarea[name=ib-answer]", (evt) -> iid = $(this)[0].dataset.id diff --git a/app/controllers/ajax/inbox_controller.rb b/app/controllers/ajax/inbox_controller.rb index 9a8a2f3e..110adc40 100644 --- a/app/controllers/ajax/inbox_controller.rb +++ b/app/controllers/ajax/inbox_controller.rb @@ -69,4 +69,23 @@ class Ajax::InboxController < ApplicationController @success = true render 'ajax/inbox/remove' end + + def remove_all_author + begin + @target_user = User.find_by_screen_name params[:author] + @inbox = current_user.inboxes.joins(:question) + .where(questions: { user_id: @target_user.id, author_is_anonymous: false }) + @inbox.each { |i| i.remove } + rescue + @status = :err + @message = I18n.t('messages.error') + @success = false + return + end + + @status = :okay + @message = I18n.t('messages.inbox.remove_all.okay') + @success = true + render 'ajax/inbox/remove' + end end diff --git a/app/controllers/inbox_controller.rb b/app/controllers/inbox_controller.rb index 728141bd..a63a4bce 100644 --- a/app/controllers/inbox_controller.rb +++ b/app/controllers/inbox_controller.rb @@ -2,7 +2,27 @@ class InboxController < ApplicationController before_filter :authenticate_user! def show - @inbox = Inbox.where(user: current_user).order(:created_at).reverse_order.paginate(page: params[:page]) + @inbox = Inbox.where(user: current_user) + .order(:created_at).reverse_order + .paginate(page: params[:page]) + if params[:author].present? + begin + @author = true + @target_user = User.find_by_screen_name params[:author] + @inbox_author = current_user.inboxes.joins(:question) + .where(questions: { user_id: @target_user.id, author_is_anonymous: false }) + .paginate(page: params[:page]) + if @inbox_author.empty? + flash[:info] = "No questions from @#{params[:author]} found, showing default entries instead!" + @inbox + else + @inbox = @inbox_author + end + rescue + flash[:error] = "No user with the name @#{params[:author]} found, showing default entries instead!" + @inbox + end + end respond_to do |format| format.html format.js diff --git a/app/views/inbox/_sidebar.html.haml b/app/views/inbox/_sidebar.html.haml index b12bc91f..024e73f1 100644 --- a/app/views/inbox/_sidebar.html.haml +++ b/app/views/inbox/_sidebar.html.haml @@ -13,8 +13,17 @@ %a.btn.btn-block.btn-primary{target: '_blank', href: "http://www.tumblr.com/share/link?url=#{show_user_profile_url(current_user.screen_name)}&name=Ask%20me%20anything%21"} %i.fa.fa-fw.fa-tumblr = raw t('views.inbox.sidebar.share.button', service: "Tumblr") +.panel.panel-default.inbox--panel + .panel-heading + %h3.panel-title Show author + .panel-body + %form#author_form + = bootstrap_form_tag url: inbox_path, method: :get do |f| + = f.text_field :author, value: params[:author], placeholder: "username", prepend: "@" , hide_label: true + = f.button "Show", name: nil, class: "btn btn-default btn-block btn-sm", id: "ib-author" .panel.panel-default.warning--panel .panel-heading %h3.panel-title= t 'views.inbox.sidebar.actions.title' .panel-body - %button.btn.btn-block.btn-danger{type: :button, id: 'ib-delete-all', disabled: (Inbox.where(user: current_user).empty? ? 'disabled' : nil), data: { ib_count: Inbox.where(user: current_user).count }}= t 'views.inbox.sidebar.actions.button' + %button.btn.btn-block.btn-danger{type: :button, id: @author ? 'ib-delete-all-author' : 'ib-delete-all', disabled: (Inbox.where(user: current_user).empty? ? 'disabled' : nil), data: { ib_count: Inbox.where(user: current_user).count }}= t 'views.inbox.sidebar.actions.button' + diff --git a/config/routes.rb b/config/routes.rb index 21d6ea56..6b00da95 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -73,6 +73,7 @@ Rails.application.routes.draw do match '/generate_question', to: 'inbox#create', via: :post, as: :generate_question match '/delete_inbox', to: 'inbox#remove', via: :post, as: :delete_inbox match '/delete_all_inbox', to: 'inbox#remove_all', via: :post, as: :delete_all_inbox + match '/delete_all_inbox/:author', to: 'inbox#remove_all_author', via: :post, as: :delete_all_author match '/answer', to: 'answer#create', via: :post, as: :answer match '/destroy_answer', to: 'answer#destroy', via: :post, as: :destroy_answer match '/create_friend', to: 'friend#create', via: :post, as: :create_friend @@ -99,6 +100,7 @@ Rails.application.routes.draw do match '/notifications(/:type)', to: 'notifications#index', via: :get, as: :notifications, defaults: {type: 'all'} match '/inbox', to: 'inbox#show', via: 'get' + match '/inbox/:author', to: 'inbox#show', via: 'get' match '/user/:username(/p/:page)', to: 'user#show', via: 'get', defaults: {page: 1} match '/@:username(/p/:page)', to: 'user#show', via: 'get', as: :show_user_profile_alt, defaults: {page: 1}