Merge pull request #3085 from evazion/feat-bur-search
Add search form to /bulk_update_requests.
This commit is contained in:
@@ -46,7 +46,7 @@ class BulkUpdateRequestsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@bulk_update_requests = BulkUpdateRequest.search(params[:search]).order("(case status when 'pending' then 0 when 'approved' then 1 else 2 end), id desc").paginate(params[:page], :limit => params[:limit])
|
@bulk_update_requests = BulkUpdateRequest.search(params[:search]).paginate(params[:page], :limit => params[:limit])
|
||||||
respond_with(@bulk_update_requests)
|
respond_with(@bulk_update_requests)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -19,15 +19,58 @@ class BulkUpdateRequest < ActiveRecord::Base
|
|||||||
before_validation :normalize_text
|
before_validation :normalize_text
|
||||||
after_create :create_forum_topic
|
after_create :create_forum_topic
|
||||||
|
|
||||||
|
scope :pending_first, lambda { order("(case status when 'pending' then 0 when 'approved' then 1 else 2 end)") }
|
||||||
|
|
||||||
module SearchMethods
|
module SearchMethods
|
||||||
def search(params)
|
def search(params = {})
|
||||||
q = where("true")
|
q = where("true")
|
||||||
return q if params.blank?
|
|
||||||
|
|
||||||
if params[:id].present?
|
if params[:id].present?
|
||||||
q = q.where("id in (?)", params[:id].split(",").map(&:to_i))
|
q = q.where("id in (?)", params[:id].split(",").map(&:to_i))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if params[:user_name].present?
|
||||||
|
q = q.where(user_id: User.name_to_id(params[:user_name]))
|
||||||
|
end
|
||||||
|
|
||||||
|
if params[:user_id].present?
|
||||||
|
q = q.where(user_id: params[:user_id].split(",").map(&:to_i))
|
||||||
|
end
|
||||||
|
|
||||||
|
if params[:approver_name].present?
|
||||||
|
q = q.where(approver_id: User.name_to_id(params[:approver_name]))
|
||||||
|
end
|
||||||
|
|
||||||
|
if params[:approver_id].present?
|
||||||
|
q = q.where(approver_id: params[:approver_id].split(",").map(&:to_i))
|
||||||
|
end
|
||||||
|
|
||||||
|
if params[:forum_topic_id].present?
|
||||||
|
q = q.where(forum_topic_id: params[:forum_topic_id].split(",").map(&:to_i))
|
||||||
|
end
|
||||||
|
|
||||||
|
if params[:forum_post_id].present?
|
||||||
|
q = q.where(forum_post_id: params[:forum_post_id].split(",").map(&:to_i))
|
||||||
|
end
|
||||||
|
|
||||||
|
if params[:status].present?
|
||||||
|
q = q.where(status: params[:status].split(","))
|
||||||
|
end
|
||||||
|
|
||||||
|
params[:order] ||= "status_desc"
|
||||||
|
case params[:order]
|
||||||
|
when "id_desc"
|
||||||
|
q = q.order(id: :desc)
|
||||||
|
when "id_asc"
|
||||||
|
q = q.order(id: :asc)
|
||||||
|
when "updated_at_desc"
|
||||||
|
q = q.order(updated_at: :desc)
|
||||||
|
when "updated_at_asc"
|
||||||
|
q = q.order(updated_at: :asc)
|
||||||
|
when "status_desc"
|
||||||
|
q = q.pending_first.order(id: :desc)
|
||||||
|
end
|
||||||
|
|
||||||
q
|
q
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
7
app/views/bulk_update_requests/_search.html.erb
Normal file
7
app/views/bulk_update_requests/_search.html.erb
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
<%= simple_form_for(:search, url: bulk_update_requests_path, method: :get, defaults: { required: false }, html: { class: "inline-form" }) do |f| %>
|
||||||
|
<%= f.input :user_name, label: "Creator", input_html: { value: params[:search][:user_name] } %>
|
||||||
|
<%= f.input :approver_name, label: "Approver", input_html: { value: params[:search][:approver_name] } %>
|
||||||
|
<%= f.input :status, label: "Status", collection: %w[pending approved rejected], include_blank: true, selected: params[:search][:status] %>
|
||||||
|
<%= f.input :order, collection: [%w[Status status_desc], %w[Last\ updated updated_at_desc], %w[Newest id_desc], %w[Oldest id_asc]], include_blank: false, selected: params[:search][:order] %>
|
||||||
|
<%= f.submit "Search" %>
|
||||||
|
<% end %>
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
<div id="a-index">
|
<div id="a-index">
|
||||||
<h1>Bulk Update Requests</h1>
|
<h1>Bulk Update Requests</h1>
|
||||||
|
|
||||||
|
<%= render "search" %>
|
||||||
<%= render "listing", :bulk_update_requests => @bulk_update_requests %>
|
<%= render "listing", :bulk_update_requests => @bulk_update_requests %>
|
||||||
|
|
||||||
<%= numbered_paginator(@bulk_update_requests) %>
|
<%= numbered_paginator(@bulk_update_requests) %>
|
||||||
|
|||||||
@@ -106,5 +106,18 @@ class BulkUpdateRequestTest < ActiveSupport::TestCase
|
|||||||
assert_match(/\[REJECTED\]/, @topic.title)
|
assert_match(/\[REJECTED\]/, @topic.title)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "when searching" do
|
||||||
|
setup do
|
||||||
|
@bur1 = FactoryGirl.create(:bulk_update_request, title: "foo", script: "create alias aaa -> bbb", user_id: @admin.id)
|
||||||
|
@bur2 = FactoryGirl.create(:bulk_update_request, title: "bar", script: "create implication bbb -> ccc", user_id: @admin.id)
|
||||||
|
@bur1.approve!(@admin)
|
||||||
|
end
|
||||||
|
|
||||||
|
should "work" do
|
||||||
|
assert_equal([@bur2.id, @bur1.id], BulkUpdateRequest.search.map(&:id))
|
||||||
|
assert_equal([@bur1.id], BulkUpdateRequest.search(user_name: @admin.name, approver_name: @admin.name, status: "approved").map(&:id))
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user