From ef6471a1bbe693535c13401ec5ab4d20db07d8d7 Mon Sep 17 00:00:00 2001 From: TPei Date: Sun, 6 Mar 2016 13:51:44 +0100 Subject: [PATCH 01/20] adds swap files to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 050c9d9..d548e5f 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ /log/* !/log/.keep /tmp +*.swp From d755bc3c2113b6f556c84f30c46501b1130a016b Mon Sep 17 00:00:00 2001 From: TPei Date: Sun, 6 Mar 2016 14:27:32 +0100 Subject: [PATCH 02/20] adds Filterable model to junior_stories_controller, allows for url filter params like so: the_url.tld/junior_stories?gender=female --- app/controllers/junior_stories_controller.rb | 2 +- app/models/concerns/filterable.rb | 14 ++++++++++++++ app/models/junior_story.rb | 1 + 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 app/models/concerns/filterable.rb diff --git a/app/controllers/junior_stories_controller.rb b/app/controllers/junior_stories_controller.rb index 8d63aaa..7f785c2 100644 --- a/app/controllers/junior_stories_controller.rb +++ b/app/controllers/junior_stories_controller.rb @@ -1,7 +1,7 @@ class JuniorStoriesController < ApplicationController def index - @junior_stories = JuniorStory.can_publish + @junior_stories = JuniorStory.filter(params.except(:controller, :action)).can_publish end def new diff --git a/app/models/concerns/filterable.rb b/app/models/concerns/filterable.rb new file mode 100644 index 0000000..3f306fe --- /dev/null +++ b/app/models/concerns/filterable.rb @@ -0,0 +1,14 @@ +module Filterable + extend ActiveSupport::Concern + + module ClassMethods + def filter(params) + params.except(:controller, :action) + results = self.all + params.each do |key, value| + results = results.where(key => value) if value.present? + end + results + end + end +end diff --git a/app/models/junior_story.rb b/app/models/junior_story.rb index 8bdfc6c..f151adc 100644 --- a/app/models/junior_story.rb +++ b/app/models/junior_story.rb @@ -1,4 +1,5 @@ class JuniorStory < ActiveRecord::Base + include Filterable validates :salary, presence: true validates :currency, presence: true From ea3ee1956f7d7dbaceee9544d710fb1c42c6e9ad Mon Sep 17 00:00:00 2001 From: TPei Date: Sun, 6 Mar 2016 14:37:45 +0100 Subject: [PATCH 03/20] can_publish does not exist anymore, apparently^^ --- app/controllers/junior_stories_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/junior_stories_controller.rb b/app/controllers/junior_stories_controller.rb index 52d8667..e467c28 100644 --- a/app/controllers/junior_stories_controller.rb +++ b/app/controllers/junior_stories_controller.rb @@ -1,7 +1,7 @@ class JuniorStoriesController < ApplicationController def index - @junior_stories = JuniorStory.filter(params.except(:controller, :action)).can_publish + @junior_stories = JuniorStory.filter(params.except(:controller, :action)) respond_to do |format| format.html From 27c156be662645cb074a612568d0071b50ca1188 Mon Sep 17 00:00:00 2001 From: TPei Date: Sun, 6 Mar 2016 14:39:57 +0100 Subject: [PATCH 04/20] needs to exclude format param as well --- app/controllers/junior_stories_controller.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/junior_stories_controller.rb b/app/controllers/junior_stories_controller.rb index e467c28..7859884 100644 --- a/app/controllers/junior_stories_controller.rb +++ b/app/controllers/junior_stories_controller.rb @@ -1,7 +1,8 @@ class JuniorStoriesController < ApplicationController def index - @junior_stories = JuniorStory.filter(params.except(:controller, :action)) + @junior_stories = JuniorStory. + filter(params.except(:controller, :action, :format)) respond_to do |format| format.html From 5db541ba34d16ad62c043d7249c5efa545a1c381 Mon Sep 17 00:00:00 2001 From: TPei Date: Mon, 7 Mar 2016 07:56:13 +0100 Subject: [PATCH 05/20] adds tests for paramter filtering --- .../junior_stroy_controller_spec.rb | 56 ++++++++++++++----- 1 file changed, 43 insertions(+), 13 deletions(-) diff --git a/spec/controllers/junior_stroy_controller_spec.rb b/spec/controllers/junior_stroy_controller_spec.rb index b9c3f84..707d5d0 100644 --- a/spec/controllers/junior_stroy_controller_spec.rb +++ b/spec/controllers/junior_stroy_controller_spec.rb @@ -2,22 +2,52 @@ describe JuniorStoriesController do describe 'GET index' do - it 'assigns @junior_stories' do - story = build :junior_story - story.save - get :index - expect(assigns(:junior_stories)).to eq([story]) - end + 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 'renders the index template' do - get :index - expect(response).to render_template('index') + 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 - 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' + context 'with 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 end end From daadee6709498690fbedec8ccf1af54808b177fb Mon Sep 17 00:00:00 2001 From: TPei Date: Mon, 7 Mar 2016 08:03:09 +0100 Subject: [PATCH 06/20] adds error handling to Filterable, adds specs for illegal filter params --- app/models/concerns/filterable.rb | 4 ++- .../junior_stroy_controller_spec.rb | 32 +++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/app/models/concerns/filterable.rb b/app/models/concerns/filterable.rb index fe87898..7e2d371 100644 --- a/app/models/concerns/filterable.rb +++ b/app/models/concerns/filterable.rb @@ -5,7 +5,9 @@ module ClassMethods def filter(params) results = self.all params.each do |key, value| - results = results.where(key => value) if value.present? + if results.column_names.include? key + results = results.where(key => value) if value.present? + end end results end diff --git a/spec/controllers/junior_stroy_controller_spec.rb b/spec/controllers/junior_stroy_controller_spec.rb index 707d5d0..2c335f3 100644 --- a/spec/controllers/junior_stroy_controller_spec.rb +++ b/spec/controllers/junior_stroy_controller_spec.rb @@ -22,7 +22,7 @@ end end - context 'with filter params' do + context 'with legal filter params' do it 'returns only the filter appropriate junior_story' do female_story = build :junior_story female_story.gender = "female" @@ -33,7 +33,7 @@ male_story.save - get :index, {"gender" => "female"} + get :index, { "gender" => "female" } expect(assigns(:junior_stories)).to eq([female_story]) end @@ -49,5 +49,33 @@ expect(response.content_type).to include 'application/csv' end end + + context 'with illegal 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, { "makes_no_sense" => "random" } + expect(assigns(:junior_stories)).to eq([female_story, male_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 From 55d087df3ede959dd857502d7c66c5d564e180ce Mon Sep 17 00:00:00 2001 From: TPei Date: Mon, 7 Mar 2016 08:05:14 +0100 Subject: [PATCH 07/20] updates strings to use single quotes --- .../junior_stroy_controller_spec.rb | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/spec/controllers/junior_stroy_controller_spec.rb b/spec/controllers/junior_stroy_controller_spec.rb index 2c335f3..668695e 100644 --- a/spec/controllers/junior_stroy_controller_spec.rb +++ b/spec/controllers/junior_stroy_controller_spec.rb @@ -25,26 +25,26 @@ 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.gender = 'female' female_story.save male_story = build :junior_story - male_story.gender = "male" + male_story.gender = 'male' male_story.save - get :index, { "gender" => "female" } + 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, { '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" } + get :index, { 'gender' => 'female', 'format' => 'csv' } expect(response).not_to render_template('index') expect(response.content_type).to include 'application/csv' end @@ -53,26 +53,26 @@ context 'with illegal filter params' do it 'returns only the filter appropriate junior_story' do female_story = build :junior_story - female_story.gender = "female" + female_story.gender = 'female' female_story.save male_story = build :junior_story - male_story.gender = "male" + male_story.gender = 'male' male_story.save - get :index, { "makes_no_sense" => "random" } + get :index, { 'makes_no_sense' => 'random' } expect(assigns(:junior_stories)).to eq([female_story, male_story]) end it 'renders the index template' do - get :index, { "makes_no_sense" => "random" } + 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" } + get :index, { 'makes_no_sense' => 'random', 'format' => 'csv' } expect(response).not_to render_template('index') expect(response.content_type).to include 'application/csv' end From 5f37082ffa2bfb0f7c11937b21be46487f4528eb Mon Sep 17 00:00:00 2001 From: TPei Date: Mon, 7 Mar 2016 08:18:23 +0100 Subject: [PATCH 08/20] spec for a combination of legal and illegal filter params --- spec/controllers/junior_stroy_controller_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/controllers/junior_stroy_controller_spec.rb b/spec/controllers/junior_stroy_controller_spec.rb index 668695e..1cb67a9 100644 --- a/spec/controllers/junior_stroy_controller_spec.rb +++ b/spec/controllers/junior_stroy_controller_spec.rb @@ -51,7 +51,7 @@ end context 'with illegal filter params' do - it 'returns only the filter appropriate junior_story' do + it 'ignores illegal filter params but respects legal filter params' do female_story = build :junior_story female_story.gender = 'female' female_story.save @@ -61,8 +61,8 @@ male_story.save - get :index, { 'makes_no_sense' => 'random' } - expect(assigns(:junior_stories)).to eq([female_story, male_story]) + get :index, { 'makes_no_sense' => 'random', 'gender' => 'female' } + expect(assigns(:junior_stories)).to eq([female_story]) end it 'renders the index template' do From 16fbfe1eb96900d5d1145b34de522aa5d5fe6b2f Mon Sep 17 00:00:00 2001 From: TPei Date: Mon, 7 Mar 2016 10:59:16 +0100 Subject: [PATCH 09/20] adds filter modal with all fields listed to search through --- app/controllers/junior_stories_controller.rb | 2 ++ app/views/junior_stories/index.html.erb | 35 ++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/app/controllers/junior_stories_controller.rb b/app/controllers/junior_stories_controller.rb index 7859884..864fba3 100644 --- a/app/controllers/junior_stories_controller.rb +++ b/app/controllers/junior_stories_controller.rb @@ -4,6 +4,8 @@ def index @junior_stories = JuniorStory. filter(params.except(:controller, :action, :format)) + @fields = JuniorStory.attribute_names + respond_to do |format| format.html format.csv { send_data @junior_stories.to_csv, type: 'application/csv' } diff --git a/app/views/junior_stories/index.html.erb b/app/views/junior_stories/index.html.erb index d984e9d..5fb77dd 100644 --- a/app/views/junior_stories/index.html.erb +++ b/app/views/junior_stories/index.html.erb @@ -5,6 +5,41 @@ <%= link_to "Download as CSV", junior_stories_path(format: "csv"), class: 'btn btn-info' %> + + + + +
<% @junior_stories.each do |j| %> From 75411253982ab3199d7bc8dd572a9b55b69b1cef Mon Sep 17 00:00:00 2001 From: TPei Date: Mon, 7 Mar 2016 11:02:26 +0100 Subject: [PATCH 10/20] ommits a couple fields from filter list --- app/controllers/junior_stories_controller.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/controllers/junior_stories_controller.rb b/app/controllers/junior_stories_controller.rb index 864fba3..605c78c 100644 --- a/app/controllers/junior_stories_controller.rb +++ b/app/controllers/junior_stories_controller.rb @@ -4,7 +4,9 @@ def index @junior_stories = JuniorStory. filter(params.except(:controller, :action, :format)) - @fields = JuniorStory.attribute_names + @fields = JuniorStory.attribute_names - ['publishing_consent', + 'updated_at', + 'created_at'] respond_to do |format| format.html From 429689ae9dd2c94449c7e7feda0a67476efa840a Mon Sep 17 00:00:00 2001 From: TPei Date: Mon, 7 Mar 2016 14:29:15 +0100 Subject: [PATCH 11/20] like this layout better --- app/views/junior_stories/index.html.erb | 69 ++++++++++++------------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/app/views/junior_stories/index.html.erb b/app/views/junior_stories/index.html.erb index 5fb77dd..cda2fca 100644 --- a/app/views/junior_stories/index.html.erb +++ b/app/views/junior_stories/index.html.erb @@ -19,20 +19,19 @@
+