aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGareth Rees <gareth@mysociety.org>2015-05-14 12:46:48 +0100
committerGareth Rees <gareth@mysociety.org>2015-06-04 13:51:03 +0100
commit609bed69081e1f0e81662f052342d34dbe7881bb (patch)
tree5dd9e77c1814a6aaa3171f94886cb11e16127f15
parent65152bc84c070af095e298016dc6d7328a74d39f (diff)
Speed up PublicBodyHelper#type_of_authority
Previously loaded all PublicBodyHeading and associated PublicBodyCategory records. Now only loads the PublicBodyCategory records that relate to the PublicBody. user system total real old 0.320000 0.000000 0.320000 ( 0.356155) new 0.010000 0.000000 0.010000 ( 0.010077)
-rw-r--r--app/helpers/public_body_helper.rb18
-rw-r--r--spec/views/public_body/show.html.erb_spec.rb1
2 files changed, 11 insertions, 8 deletions
diff --git a/app/helpers/public_body_helper.rb b/app/helpers/public_body_helper.rb
index 54aa82d3a..e871ea1bc 100644
--- a/app/helpers/public_body_helper.rb
+++ b/app/helpers/public_body_helper.rb
@@ -40,15 +40,17 @@ module PublicBodyHelper
# Returns a String
def type_of_authority(public_body)
first = true
- types = public_body.tags.each.map do |tag|
- if PublicBodyCategory.get.by_tag.include?(tag.name)
- desc = PublicBodyCategory.get.singular_by_tag[tag.name]
- if first
- desc = desc.sub(/\S/) { |m| Unicode.upcase(m) }
- first = false
- end
- link_to(desc, list_public_bodies_path(tag.name))
+
+ categories = PublicBodyCategory.
+ where(:category_tag => public_body.tag_string.split)
+
+ types = categories.map do |category|
+ desc = category.description
+ if first
+ desc = desc.sub(/\S/) { |m| Unicode.upcase(m) }
+ first = false
end
+ link_to(desc, list_public_bodies_path(category.category_tag))
end
types.compact!
diff --git a/spec/views/public_body/show.html.erb_spec.rb b/spec/views/public_body/show.html.erb_spec.rb
index a4edcfc22..c20d8483c 100644
--- a/spec/views/public_body/show.html.erb_spec.rb
+++ b/spec/views/public_body/show.html.erb_spec.rb
@@ -18,6 +18,7 @@ describe "public_body/show" do
@pb.stub!(:special_not_requestable_reason?).and_return(false)
@pb.stub!(:has_notes?).and_return(false)
@pb.stub!(:has_tag?).and_return(false)
+ @pb.stub!(:tag_string).and_return('')
@xap = mock(ActsAsXapian::Search, :matches_estimated => 2)
@xap.stub!(:results).and_return([
{ :model => mock_event },