/bulk_update_requests: add search form.

This commit is contained in:
evazion
2017-05-14 12:47:57 -05:00
parent f510a0d9d2
commit 614bf44086
5 changed files with 67 additions and 3 deletions

View File

@@ -46,7 +46,7 @@ class BulkUpdateRequestsController < ApplicationController
end
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)
end

View File

@@ -19,15 +19,58 @@ class BulkUpdateRequest < ActiveRecord::Base
before_validation :normalize_text
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
def search(params)
def search(params = {})
q = where("true")
return q if params.blank?
if params[:id].present?
q = q.where("id in (?)", params[:id].split(",").map(&:to_i))
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
end
end

View 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 %>

View File

@@ -2,6 +2,7 @@
<div id="a-index">
<h1>Bulk Update Requests</h1>
<%= render "search" %>
<%= render "listing", :bulk_update_requests => @bulk_update_requests %>
<%= numbered_paginator(@bulk_update_requests) %>

View File

@@ -106,5 +106,18 @@ class BulkUpdateRequestTest < ActiveSupport::TestCase
assert_match(/\[REJECTED\]/, @topic.title)
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