This repository has been archived by the owner on Jun 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from TPei/filters_on_junior_story_model
Filters on junior story model
- Loading branch information
Showing
7 changed files
with
185 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ | |
/log/* | ||
!/log/.keep | ||
/tmp | ||
*.swp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Filterable | ||
extend ActiveSupport::Concern | ||
|
||
module ClassMethods | ||
def filter(params) | ||
results = self.all | ||
params.each do |key, value| | ||
if results.column_names.include? key | ||
results = results.where(key => value) if value.present? | ||
end | ||
end | ||
results | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
require 'rails_helper' | ||
|
||
describe JuniorStoriesController do | ||
describe 'GET index' do | ||
context 'without filter params' do | ||
it 'assigns @junior_stories' do | ||
story = build :junior_story | ||
story.save | ||
get :index | ||
expect(assigns(:junior_stories)).to eq([story]) | ||
end | ||
|
||
it 'renders the index template' do | ||
get :index | ||
expect(response).to render_template('index') | ||
end | ||
|
||
it 'does not render index, but instead responds with csv' do | ||
get :index, format: 'csv' | ||
expect(response).not_to render_template('index') | ||
expect(response.content_type).to include 'application/csv' | ||
end | ||
end | ||
|
||
context 'with legal filter params' do | ||
it 'returns only the filter appropriate junior_story' do | ||
female_story = build :junior_story | ||
female_story.gender = 'female' | ||
female_story.save | ||
|
||
male_story = build :junior_story | ||
male_story.gender = 'male' | ||
male_story.save | ||
|
||
|
||
get :index, { 'gender' => 'female' } | ||
expect(assigns(:junior_stories)).to eq([female_story]) | ||
end | ||
|
||
it 'renders the index template' do | ||
get :index, { 'gender' => 'female' } | ||
get :index | ||
expect(response).to render_template('index') | ||
end | ||
|
||
it 'does not render index, but instead responds with csv' do | ||
get :index, { 'gender' => 'female', 'format' => 'csv' } | ||
expect(response).not_to render_template('index') | ||
expect(response.content_type).to include 'application/csv' | ||
end | ||
end | ||
|
||
context 'with illegal filter params' do | ||
it 'ignores illegal filter params but respects legal filter params' do | ||
female_story = build :junior_story | ||
female_story.gender = 'female' | ||
female_story.save | ||
|
||
male_story = build :junior_story | ||
male_story.gender = 'male' | ||
male_story.save | ||
|
||
|
||
get :index, { 'makes_no_sense' => 'random', 'gender' => 'female' } | ||
expect(assigns(:junior_stories)).to eq([female_story]) | ||
end | ||
|
||
it 'renders the index template' do | ||
get :index, { 'makes_no_sense' => 'random' } | ||
get :index | ||
expect(response).to render_template('index') | ||
end | ||
|
||
it 'does not render index, but instead responds with csv' do | ||
get :index, { 'makes_no_sense' => 'random', 'format' => 'csv' } | ||
expect(response).not_to render_template('index') | ||
expect(response.content_type).to include 'application/csv' | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.