Permalink
Browse files

Partner filter with all tests passing

  • Loading branch information...
1 parent 050e86b commit 4d1891cf6aa9991fbcd99f0d5aba403667ac6979 @decioferreira decioferreira committed Apr 13, 2012
Showing with 102 additions and 23 deletions.
  1. +29 −20 app/assets/javascripts/backbone/models/indicator.js.coffee
  2. +4 −0 spec/factories.rb
  3. +69 −3 spec/requests/indicators_spec.rb
@@ -24,34 +24,43 @@ class BIPIndicatorsPage.Collections.IndicatorsCollection extends Backbone.Collec
target: null
partner: null
+ resetTabFilters: ->
+ @filters.headline = null
+ @filters.focalArea = null
+ @filters.goal = null
+ @filters.target = null
+
+ applyFilter: ->
+ _.each @models, (indicator) =>
+ indicator.set
+ 'show': (
+ (
+ @filters.headline? && indicator.get('headline_id') == @filters.headline.get('id') ||
+ @filters.focalArea? && _.pluck(indicator.get('focal_areas'), 'id').indexOf(@filters.focalArea.get('id')) != -1 ||
+ @filters.target? && _.pluck(indicator.get('targets'), 'id').indexOf(@filters.target.get('id')) != -1
+ ) &&
+ (!@filters.partner? || _.pluck(indicator.get('partners'), 'id').indexOf(@filters.partner.get('id')) != -1)
+ )
+
filterByHeadline: (headline) ->
- if headline?
- _.each @models, (indicator) ->
- indicator.set({'show': (indicator.get('headline_id') == headline.get('id') )})
- else
- _.each @models, (indicator) ->
- indicator.set({'show': false })
+ @resetTabFilters()
+ @filters.headline = headline
+ @applyFilter()
filterByFocalArea: (focalArea) ->
- if focalArea?
- _.each @models, (indicator) ->
- indicator.set({'show': (_.pluck(indicator.get('focal_areas'), 'id').indexOf(focalArea.get('id')) != -1 ) })
- else
- _.each @models, (indicator) ->
- indicator.set({'show': false })
+ @resetTabFilters()
+ @filters.focalArea = focalArea
+ @applyFilter()
filterByGoal: (goal) ->
_.each @models, (indicator) ->
indicator.set({'show': (_.intersection(_.pluck(indicator.get('targets'), 'id'), _.pluck(goal.get('targets'), 'id')).length > 0 ) })
filterByTarget: (target) ->
- if target?
- _.each @models, (indicator) ->
- indicator.set({'show': (_.pluck(indicator.get('targets'), 'id').indexOf(target.get('id')) != -1 ) })
- else
- _.each @models, (indicator) ->
- indicator.set({'show': false })
+ @resetTabFilters()
+ @filters.target = target
+ @applyFilter()
filterByPartner: (partner) ->
- _.each @models, (indicator) ->
- indicator.set({'show': (_.pluck(indicator.get('partners'), 'id').indexOf(partner.get('id')) != -1 ) })
+ @filters.partner = partner
+ @applyFilter()
View
@@ -15,6 +15,10 @@
factory :indicator do
end
+ # Partner
+ factory :partner do
+ end
+
# Target
factory :target do
end
@@ -75,7 +75,7 @@
FactoryGirl.create(:indicator, title: 'This one should appear', targets: [target_1])
FactoryGirl.create(:indicator, title: 'This one should also appear', targets: [target_1])
- FactoryGirl.create(:indicator, title: 'This one should not', targets: [target_2])
+ FactoryGirl.create(:indicator, title: 'This one should not appear', targets: [target_2])
visit '/'
page.execute_script('$("div.keyword:contains(\'ABC\')").click()')
@@ -93,7 +93,7 @@
FactoryGirl.create(:indicator, title: 'This one should appear', focal_areas: [focal_area_1])
FactoryGirl.create(:indicator, title: 'This one should also appear', focal_areas: [focal_area_1])
- FactoryGirl.create(:indicator, title: 'This one should not', focal_areas: [focal_area_2])
+ FactoryGirl.create(:indicator, title: 'This one should not appear', focal_areas: [focal_area_2])
visit '/'
click_link('Graphic')
@@ -112,7 +112,7 @@
FactoryGirl.create(:indicator, title: 'This one should appear', headline: headline_1)
FactoryGirl.create(:indicator, title: 'This one should also appear', headline: headline_1)
- FactoryGirl.create(:indicator, title: 'This one should not', headline: headline_2)
+ FactoryGirl.create(:indicator, title: 'This one should not appear', headline: headline_2)
visit '/'
click_link('Headlines')
@@ -122,5 +122,71 @@
page.find('#indicators-container').should_not have_content('This one should not appear')
end
end
+
+ describe 'filters' do
+ describe 'partners select' do
+ it 'shows indicators associated with the selected target and partner', js: true do
+ goal = FactoryGirl.create(:goal, code: 'ABBA')
+
+ target_1 = FactoryGirl.create(:target, keyword: 'ABC', goal: goal)
+ target_2 = FactoryGirl.create(:target, keyword: 'XYZ', goal: goal)
+
+ partner_1 = FactoryGirl.create(:partner, name: 'Batman')
+ partner_2 = FactoryGirl.create(:partner, name: 'Robin')
+
+ FactoryGirl.create(:indicator, title: 'This one should appear', targets: [target_1], partners: [partner_1])
+ FactoryGirl.create(:indicator, title: 'This one should not appear', targets: [target_1], partners: [partner_2])
+ FactoryGirl.create(:indicator, title: 'This last one should not appear either', targets: [target_2], partners: [partner_1, partner_2])
+
+ visit '/'
+ page.execute_script('$("div.keyword:contains(\'ABC\')").click()')
+ select('Batman', from: 'Partner')
+ page.find('#indicators-container').should have_content('This one should appear')
+ page.find('#indicators-container').should_not have_content('This one should not appear')
+ page.find('#indicators-container').should_not have_content('This last one should not appear either')
+ end
+
+ it 'shows indicators associated with the selected focal_area and partner', js: true, :driver => :selenium do
+ focal_area_1 = FactoryGirl.create(:focal_area, name: 'A')
+ focal_area_2 = FactoryGirl.create(:focal_area, name: 'B')
+
+ partner_1 = FactoryGirl.create(:partner, name: 'Batman')
+ partner_2 = FactoryGirl.create(:partner, name: 'Robin')
+
+ FactoryGirl.create(:indicator, title: 'This one should appear', focal_areas: [focal_area_1], partners: [partner_1])
+ FactoryGirl.create(:indicator, title: 'This one should not appear', focal_areas: [focal_area_1], partners: [partner_2])
+ FactoryGirl.create(:indicator, title: 'This last one should not appear either', focal_areas: [focal_area_2], partners: [partner_1, partner_2])
+
+ visit '/'
+ click_link('Graphic')
+ # Workaround to test clicking on the image map
+ page.execute_script('window.router.filterByFocalArea("A");return false')
+ select('Batman', from: 'Partner')
+ page.find('#indicators-container').should have_content('This one should appear')
+ page.find('#indicators-container').should_not have_content('This one should not appear')
+ page.find('#indicators-container').should_not have_content('This last one should not appear either')
+ end
+
+ it 'shows indicators associated with the selected headline and partner', js: true, :driver => :selenium do
+ headline_1 = FactoryGirl.create(:headline, title: 'The first headline ever')
+ headline_2 = FactoryGirl.create(:headline, title: 'The last headline ever')
+
+ partner_1 = FactoryGirl.create(:partner, name: 'Batman')
+ partner_2 = FactoryGirl.create(:partner, name: 'Robin')
+
+ FactoryGirl.create(:indicator, title: 'This one should appear', headline: headline_1, partners: [partner_1])
+ FactoryGirl.create(:indicator, title: 'This one should not appear', headline: headline_1, partners: [partner_2])
+ FactoryGirl.create(:indicator, title: 'This last one should not appear either', headline: headline_2, partners: [partner_1, partner_2])
+
+ visit '/'
+ click_link('Headlines')
+ page.execute_script('$(".title:contains(\'The first headline ever\')").siblings("button").click()')
+ select('Batman', from: 'Partner')
+ page.find('#indicators-container').should have_content('This one should appear')
+ page.find('#indicators-container').should_not have_content('This one should not appear')
+ page.find('#indicators-container').should_not have_content('This last one should not appear either')
+ end
+ end
+ end
end
end

0 comments on commit 4d1891c

Please sign in to comment.