Skip to content
This repository
Browse code

Allowing filtering of tags by type (eg. Section)

As we look at sharing lists of topics, sections, etc. between Inside
Government and Mainstream we will want to be able to quickly access a
list of tags of a given type.

GET /tags.json will still return all tags (at least until we paginate
the list)
GET /tags.json?type=section will return all section tags

We considered the fact that we use 'tag_type' in some places and 'type'
here but decided not to change that yet as there are implications for
how inheritance works that are too significant to dig into here. 'type'
would clash with rails (activerecord/mongoid) STI implementation.
  • Loading branch information...
commit 6e2e81901cdcf66a762b74a50dcb6a12deb756f5 1 parent f8e2dd9
David Heath authored June 21, 2012
3  app/controllers/tags_controller.rb
@@ -2,8 +2,7 @@ class TagsController < ApplicationController
2 2
   respond_to :json
3 3
 
4 4
   def index
5  
-    @tags = TagRepository.load_all
6  
-    # TODO: links: [next_page, prev_page]
  5
+    @tags = TagRepository.load_all(tag_type: params[:type])
7 6
     respond_with(status: 'ok', total: @tags.count, from: 0, to: @tags.count - 1,
8 7
       pagesize: @tags.count, results: @tags)
9 8
   end
11  test/functional/tags_controller_test.rb
@@ -37,4 +37,15 @@ class TagsControllerTest < ActionController::TestCase
37 37
     end
38 38
   end
39 39
 
  40
+  context "GET /tags?type=X" do
  41
+    should "return only tags of a particular type" do
  42
+      TagRepository.put(tag_id: "minister-of-silly", title: "Minister of Silly", tag_type: "role")
  43
+      get :index, format: "json", type: "section"
  44
+      parsed = JSON.parse(response.body)
  45
+      assert_equal 2, parsed["results"].count
  46
+      refute parsed["results"].any? { |result| result["id"] == "minister-of-silly" }
  47
+    end
  48
+  end
  49
+
  50
+
40 51
 end

0 notes on commit 6e2e819

Please sign in to comment.
Something went wrong with that request. Please try again.