diff options
author | Louise Crow <louise.crow@gmail.com> | 2014-09-24 17:44:43 +0100 |
---|---|---|
committer | Louise Crow <louise.crow@gmail.com> | 2014-09-24 17:44:43 +0100 |
commit | fd82802023fec28c0e467c0ba2820489d35ddc9c (patch) | |
tree | 93c4ebb80c547b1837d5479782d418607c89a9f0 | |
parent | 7b7f5dfa21f669b75552e946bedc598174fd71db (diff) |
fixup! Replace existing PublicBodyCategories functionality with db models PublicBodyCategory and PublicBodyHeading
-rw-r--r-- | app/models/public_body_category.rb | 53 | ||||
-rw-r--r-- | app/models/public_body_category/category_collection.rb | 54 | ||||
-rw-r--r-- | spec/controllers/public_body_controller_spec.rb | 19 | ||||
-rw-r--r-- | spec/models/public_body_category/category_collection_spec.rb | 81 | ||||
-rw-r--r-- | spec/models/public_body_category_spec.rb | 72 | ||||
-rw-r--r-- | spec/models/public_body_heading_spec.rb | 6 | ||||
-rw-r--r-- | spec/spec_helper.rb | 8 |
7 files changed, 155 insertions, 138 deletions
diff --git a/app/models/public_body_category.rb b/app/models/public_body_category.rb index 74b239463..b019673c4 100644 --- a/app/models/public_body_category.rb +++ b/app/models/public_body_category.rb @@ -167,57 +167,4 @@ class PublicBodyCategory < ActiveRecord::Base end end -# replicate original file-based PublicBodyCategories functionality -class CategoryCollection - include Enumerable - extend Forwardable - def_delegators :@categories, :each, :<< - - def initialize - @categories = [] - end - - def with_headings - @categories - end - - def with_description - @categories.select() { |a| a.instance_of?(Array) } - end - - def tags - tags = with_description.map() { |a| a[0] } - end - - def by_tag - Hash[*with_description.map() { |a| a[0..1] }.flatten] - end - - def singular_by_tag - Hash[*with_description.map() { |a| [a[0],a[2]] }.flatten] - end - - def by_heading - output = {} - heading = nil - @categories.each do |row| - if row.is_a?(Array) - output[heading] << row[0] - else - heading = row - output[heading] = [] - end - end - output - end - def headings - output = [] - @categories.each do |row| - unless row.is_a?(Array) - output << row - end - end - output - end -end diff --git a/app/models/public_body_category/category_collection.rb b/app/models/public_body_category/category_collection.rb new file mode 100644 index 000000000..8286e2710 --- /dev/null +++ b/app/models/public_body_category/category_collection.rb @@ -0,0 +1,54 @@ +# replicate original file-based PublicBodyCategories functionality +class PublicBodyCategory::CategoryCollection + include Enumerable + extend Forwardable + def_delegators :@categories, :each, :<< + + def initialize + @categories = [] + end + + def with_headings + @categories + end + + def with_description + @categories.select() { |a| a.instance_of?(Array) } + end + + def tags + tags = with_description.map() { |a| a[0] } + end + + def by_tag + Hash[*with_description.map() { |a| a[0..1] }.flatten] + end + + def singular_by_tag + Hash[*with_description.map() { |a| [a[0],a[2]] }.flatten] + end + + def by_heading + output = {} + heading = nil + @categories.each do |row| + if row.is_a?(Array) + output[heading] << row[0] + else + heading = row + output[heading] = [] + end + end + output + end + + def headings + output = [] + @categories.each do |row| + unless row.is_a?(Array) + output << row + end + end + output + end +end diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb index 6ff1a7215..fc7143522 100644 --- a/spec/controllers/public_body_controller_spec.rb +++ b/spec/controllers/public_body_controller_spec.rb @@ -209,16 +209,19 @@ describe PublicBodyController, "when listing bodies" do end end - it "should list a tagged thing on the appropriate list page, and others on the other page, and all still on the all page" do - load_test_categories - - public_bodies(:humpadink_public_body).tag_string = "foo local_council" - - get :list, :tag => "local_council" + it "should list a tagged thing on the appropriate list page, and others on the other page, + and all still on the all page" do + category = FactoryGirl.create(:public_body_category) + heading = FactoryGirl.create(:public_body_heading) + PublicBodyCategoryLink.create(:public_body_heading_id => heading.id, + :public_body_category_id => category.id) + public_bodies(:humpadink_public_body).tag_string = category.category_tag + + get :list, :tag => category.category_tag response.should render_template('list') assigns[:public_bodies].should == [ public_bodies(:humpadink_public_body) ] - assigns[:tag].should == "local_council" - assigns[:description].should == "in the category ‘Local councils’" + assigns[:tag].should == category.category_tag + assigns[:description].should == "in the category ‘#{category.title}’" get :list, :tag => "other" response.should render_template('list') diff --git a/spec/models/public_body_category/category_collection_spec.rb b/spec/models/public_body_category/category_collection_spec.rb new file mode 100644 index 000000000..1fbcbe739 --- /dev/null +++ b/spec/models/public_body_category/category_collection_spec.rb @@ -0,0 +1,81 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +describe PublicBodyCategory::CategoryCollection do + context "requesting data" do + + before do + data = [ "Local and regional", + [ "local_council", "Local councils", "a local council" ], + "Miscellaneous", + [ "other", "Miscellaneous", "miscellaneous" ] ] + @categories = PublicBodyCategory::CategoryCollection.new + data.each { |item| @categories << item } + end + + describe 'when asked for headings' do + + it 'should return a list of headings' do + @categories.headings().should == ['Local and regional', 'Miscellaneous'] + end + + end + + describe 'when asked for categories with headings' do + it 'should return a list of headings as plain strings, each followed by n tag specifications as + lists in the form: + ["tag_to_use_as_category", "Sub category title", "Instance description"]' do + expected_categories = ["Local and regional", ["local_council", + "Local councils", + "a local council"], + "Miscellaneous", ["other", + "Miscellaneous", + "miscellaneous"]] + @categories.with_headings().should == expected_categories + end + end + + + + describe 'when asked for tags by headings' do + it 'should return a hash of tags keyed by heading' do + @categories.by_heading().should == {'Local and regional' => ['local_council'], + 'Miscellaneous' => ['other']} + end + end + + describe 'when asked for categories with description' do + it 'should return a list of tag specifications as lists in the form: + ["tag_to_use_as_category", "Sub category title", "Instance description"]' do + expected_categories = [ + ["local_council", "Local councils", "a local council"], + ["other", "Miscellaneous", "miscellaneous"] + ] + @categories.with_description().should == expected_categories + end + end + + describe 'when asked for tags' do + it 'should return a list of tags' do + @categories.tags().should == ["local_council", "other"] + end + end + + describe 'when asked for categories by tag' do + it 'should return a hash of categories keyed by tag' do + @categories.by_tag().should == { + "local_council" => "Local councils", + "other" => "Miscellaneous" + } + end + end + + describe 'when asked for singular_by_tag' do + it 'should return a hash of category descriptions keyed by tag' do + @categories.singular_by_tag().should == { + "local_council" => "a local council", + "other" => "miscellaneous" + } + end + end + end +end diff --git a/spec/models/public_body_category_spec.rb b/spec/models/public_body_category_spec.rb index d3c91e4f8..b7ef63ac3 100644 --- a/spec/models/public_body_category_spec.rb +++ b/spec/models/public_body_category_spec.rb @@ -44,76 +44,12 @@ describe PublicBodyCategory do end context "requesting data" do - before do - load_test_categories - end - - describe 'when asked for categories with headings' do - it 'should return a list of headings as plain strings, each followed by n tag specifications as - lists in the form: - ["tag_to_use_as_category", "Sub category title", "Instance description"]' do - expected_categories = ["Local and regional", ["local_council", - "Local councils", - "a local council"], - "Miscellaneous", ["other", - "Miscellaneous", - "miscellaneous"]] - PublicBodyCategory::get().with_headings().should == expected_categories - end - end - - describe 'when asked for headings' do - it 'should return a list of headings' do - PublicBodyCategory::get().headings().should == ['Local and regional', 'Miscellaneous'] - end - it 'should call load_categories if categories are not already loaded' do - PublicBodyCategory.stub!(:count).and_return(0) - PublicBodyCategory.should_receive(:load_categories) - PublicBodyCategory::get() - end + it 'should call load_categories if categories are not already loaded' do + PublicBodyCategory.stub!(:count).and_return(0) + PublicBodyCategory.should_receive(:load_categories) + PublicBodyCategory::get() end - describe 'when asked for tags by headings' do - it 'should return a hash of tags keyed by heading' do - PublicBodyCategory::get().by_heading().should == {'Local and regional' => ['local_council'], - 'Miscellaneous' => ['other']} - end - end - - describe 'when asked for categories with description' do - it 'should return a list of tag specifications as lists in the form: - ["tag_to_use_as_category", "Sub category title", "Instance description"]' do - expected_categories = [ - ["local_council", "Local councils", "a local council"], - ["other", "Miscellaneous", "miscellaneous"] - ] - PublicBodyCategory::get().with_description().should == expected_categories - end - end - - describe 'when asked for tags' do - it 'should return a list of tags' do - PublicBodyCategory::get().tags().should == ["local_council", "other"] - end - end - - describe 'when asked for categories by tag' do - it 'should return a hash of categories keyed by tag' do - PublicBodyCategory::get().by_tag().should == { - "local_council" => "Local councils", - "other" => "Miscellaneous" - } - end - end - - describe 'when asked for singular_by_tag' do - it 'should return a hash of category descriptions keyed by tag' do - PublicBodyCategory::get().singular_by_tag().should == { - "local_council" => "a local council", - "other" => "miscellaneous" - } - end - end end end diff --git a/spec/models/public_body_heading_spec.rb b/spec/models/public_body_heading_spec.rb index dd0517072..91fd35be8 100644 --- a/spec/models/public_body_heading_spec.rb +++ b/spec/models/public_body_heading_spec.rb @@ -15,7 +15,11 @@ describe PublicBodyHeading do context 'when loading the data' do before do - load_test_categories + PublicBodyCategories.add(:en, [ + "Local and regional", + [ "local_council", "Local councils", "a local council" ], + "Miscellaneous", + [ "other", "Miscellaneous", "miscellaneous" ],]) end it 'should use the display_order field to preserve the original data order' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0e3fe35c7..74a4891c2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -201,14 +201,6 @@ Spork.prefork do I18n.default_locale = original_default_locale end - def load_test_categories - PublicBodyCategories.add(:en, [ - "Local and regional", - [ "local_council", "Local councils", "a local council" ], - "Miscellaneous", - [ "other", "Miscellaneous", "miscellaneous" ],]) - end - def basic_auth_login(request, username = nil, password = nil) username = AlaveteliConfiguration::admin_username if username.nil? password = AlaveteliConfiguration::admin_password if password.nil? |