Skip to content
This repository has been archived by the owner on Jun 20, 2018. It is now read-only.

Commit

Permalink
Merge pull request #16 from TPei/filters_on_junior_story_model
Browse files Browse the repository at this point in the history
Filters on junior story model
  • Loading branch information
berlintam authored Dec 11, 2016
2 parents ea0b3fe + 84979cc commit 0eeadf3
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
/log/*
!/log/.keep
/tmp
*.swp
55 changes: 50 additions & 5 deletions app/controllers/junior_stories_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
class JuniorStoriesController < ApplicationController

def index
@junior_stories = JuniorStory.all
@junior_stories = JuniorStory.
filter(allowed_params(params))

@fields = fields
@csv_params = allowed_params(params).merge({ format: 'csv' })

@filtered = params_set?

respond_to do |format|
format.html
format.csv { send_data @junior_stories.to_csv, type: 'application/csv' }
format.csv { send_data @junior_stories.to_csv, type: 'application/csv', filename: 'junior_stories.csv' }
end
end

Expand All @@ -17,7 +23,7 @@ def show
end

def create
@junior_story = JuniorStory.new(junior_story_params)
@junior_story = JuniorStory.new(allowed_params(required_params))
if @junior_story.save
redirect_to junior_stories_path
else
Expand All @@ -26,11 +32,50 @@ def create
end

private
def junior_story_params
params.require(:junior_story).permit(:job, :happy_in_job, :happy_info,
def required_params
params.require(:junior_story)
end

def allowed_params(params)
params.permit(:job, :happy_in_job, :happy_info,
:gender, :city, :country, :days_per_week, :salary, :currency, :technology, :focus,
:age, :years_working_in_total, :years_working_at_job, :education, :first_job,
:remote, :tech_team_size, :company_size, :company_age, :person_of_colour, :other,
:publishing_consent, :freelancer, :person_with_disability)
end

# TODO: shouldn't hard code this, but I like having demo search terms in the list
def fields
{
job: "junior developer",
happy_in_job: "yes",
happy_info: "It's ok",
gender: "female",
city: "Berlin",
country: "DE",
days_per_week: "5",
salary: "15000",
currency: "€",
technology: "RoR",
focus: "backend",
age: 30,
years_working_total: "less than 1 year",
years_working_at_job: "less than 1 year",
education: "self taught",
first_job: "no",
remote: "no",
tech_team_size: "5 - 10 people",
company_size: "less than 10 people",
company_age: 5,
person_of_colour: false,
other: "Thanks",
freelancer: "f"
}
end

def params_set?
values = allowed_params(params).values
values.reject { |value| value.blank? || value.empty? || value.nil? }
!values.empty?
end
end
15 changes: 15 additions & 0 deletions app/models/concerns/filterable.rb
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
1 change: 1 addition & 0 deletions app/models/junior_story.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'csv'

class JuniorStory < ApplicationRecord
include Filterable

validates :salary, presence: true
validates :currency, presence: true
Expand Down
38 changes: 37 additions & 1 deletion app/views/junior_stories/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,43 @@
<h1>Junior Stories:</h1>
</div>

<%= link_to "Download as CSV", junior_stories_path(format: "csv"), class: 'btn btn-info' %>
<%= link_to "Download as CSV", junior_stories_path(@csv_params), class: 'btn btn-info' %>
<!-- Button trigger modal -->
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#myModal">
Filter Stories
</button>
<% if @filtered %>
<%= link_to "Reset filters", junior_stories_path, class: 'btn btn-info' %>
<% end %>
<!-- Filter Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<%= form_tag({controller: "junior_stories", action: "index" }, { method: "get", class: "form-horizontal" }) do %>
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="myModalLabel">Filter for Fields</h4>
</div>
<div class="modal-body">
<% @fields.each do |name, demo_value| %>
<div class="form-group">
<div class="col-sm-offset-1"></div>
<label class="col-sm-4 control-label"><%= name %></label>
<div class="col-sm-6">
<input type="text" class="form-control" name="<%= name %>" placeholder="<%= demo_value %>">
</div>
</div>
<% end %>
</div>

<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Filter</button>
</div>
</div>
<% end %>
</div>
</div>

<div class='junior-stories'>
<% @junior_stories.each do |j| %>
Expand Down
81 changes: 81 additions & 0 deletions spec/controllers/junior_story_controller_spec.rb
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
23 changes: 0 additions & 23 deletions spec/controllers/junior_stroy_controller_spec.rb

This file was deleted.

0 comments on commit 0eeadf3

Please sign in to comment.