Permalink
Browse files

Implement artefact search

  • Loading branch information...
1 parent ba11beb commit 34844d262fae346de4670c2a960bbcfdb5d488a8 @jamiecobbett jamiecobbett committed Mar 20, 2013
@@ -19,6 +19,10 @@ def index
else
@artefacts = Artefact
end
+ if params[:filter].present?
+ search = /#{Regexp.escape(params[:filter])}/i
+ @artefacts = @artefacts.any_of({name: search}, {description: search}, {slug: search}, {kind: search}, {owning_app: search})
+ end
@artefacts = @artefacts.order_by([[sort_column, sort_direction]])
@artefacts = @artefacts.page(params[:page]).per(ITEMS_PER_PAGE)
respond_with @artefacts, @tag_collection
@@ -3,7 +3,12 @@
<div class="span12">
<div class="page-header">
- <h1>Artefacts</h1>
+ <h1>
+ Artefacts
+ <% if params[:filter].present? %>
+ filtered for "<%= params[:filter] %>"
+ <% end %>
+ </h1>
</div>
<div class="row-fluid">
@@ -13,9 +18,13 @@
<li class="nav-header">Filter by Section</li>
<%= parent_section_tab_list(:current => @section) %>
</ul>
-
</div>
-
+ <%= form_tag "", method: :get do |f| %>
+ <%= hidden_field_tag :section, params[:section] %>
+ <label for="filter" class="visuallyhidden">Filter results</label>
+ <%= text_field_tag "filter", params[:filter], class: "span12", type: "search" %>
+ <input class="btn btn-primary" type="submit" value="Filter">
+ <% end %>
</div>
<div class="span10">
@@ -28,6 +28,15 @@ class ArtefactsControllerTest < ActionController::TestCase
assert_select "tbody tr td", /crime/i
assert_select "tbody tr td", artefact1.name
end
+
+ should "filter by 'filter' parameter" do
+ artefact1 = FactoryGirl.create(:artefact, name: "Cheese")
+ artefact2 = FactoryGirl.create(:artefact, name: "Chalk")
+
+ get :index, filter: "cheese"
+ assert_select "tbody tr", count: 1
+ assert_select "tbody tr td", artefact1.name
+ end
end
context "GET new" do

0 comments on commit 34844d2

Please sign in to comment.