This repository has been archived by the owner on Feb 18, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Merge pull request #8 from Averethel/filters
+ Filters
- Loading branch information
Showing
11 changed files
with
265 additions
and
19 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
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
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,32 @@ | ||
class IssueFilterService | ||
def initialize(filters) | ||
@filters = filters | ||
end | ||
|
||
def filter(issues) | ||
issues = filter_by_title(issues) if @filters[:title] | ||
issues = filter_by_description(issues) if @filters[:description] | ||
issues = filter_by_priority(issues) if @filters[:priority] | ||
issues = filter_by_status(issues) if @filters[:status] | ||
issues.where(@filters.except(:tite, :description, :priority, :status)) | ||
end | ||
|
||
private | ||
def filter_by_title(issues) | ||
issues.where('title ilike ?', "%#{@filters[:title]}%") | ||
end | ||
|
||
def filter_by_description(issues) | ||
issues.where('description ilike ?', "%#{@filters[:description]}%") | ||
end | ||
|
||
def filter_by_priority(issues) | ||
values = @filters[:priority].map{ |s| Issue.priorities[s] }.compact | ||
issues.where(priority: values) | ||
end | ||
|
||
def filter_by_status(issues) | ||
values = @filters[:status].map{ |s| Issue.statuses[s] }.compact | ||
issues.where(status: values) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class UserFilterService | ||
def initialize(filters) | ||
@filters = filters | ||
end | ||
|
||
def filter(users) | ||
users = filter_by_username(users) if @filters[:username] | ||
users.where(@filters.except(:username)) | ||
end | ||
|
||
private | ||
def filter_by_username(users) | ||
users.where('username ilike ?', "%#{@filters[:username]}%") | ||
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,65 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe IssueFilterService, type: :model do | ||
subject { described_class.new(filters).filter(Issue.all) } | ||
let!(:issue1) { FactoryGirl.create(:issue, priority: 'major') } | ||
let!(:issue2) { FactoryGirl.create(:issue, title: 'different', description: 'different', priority: 'minor') } | ||
|
||
context '#filter' do | ||
context 'by id' do | ||
let(:filters) do | ||
{ | ||
id: [issue1.id] | ||
} | ||
end | ||
|
||
it 'includes only issues with matching id' do | ||
expect(subject).to eq([issue1]) | ||
end | ||
end | ||
|
||
context 'by title' do | ||
let(:filters) do | ||
{ title: issue1.title } | ||
end | ||
|
||
it 'includes only issues with matching title' do | ||
expect(subject).to eq([issue1]) | ||
end | ||
end | ||
|
||
context 'by description' do | ||
let(:filters) do | ||
{ description: issue1.description } | ||
end | ||
|
||
it 'includes only issues with matching description' do | ||
expect(subject).to eq([issue1]) | ||
end | ||
end | ||
|
||
context 'by priority' do | ||
let(:filters) do | ||
{ priority: [issue1.priority] } | ||
end | ||
|
||
it 'includes only issues with matching priority' do | ||
expect(subject).to eq([issue1]) | ||
end | ||
end | ||
|
||
context 'by status' do | ||
before do | ||
issue1.fixed! | ||
end | ||
|
||
let(:filters) do | ||
{ status: ['fixed'] } | ||
end | ||
|
||
it 'includes only issues with matching status' do | ||
expect(subject).to eq([issue1]) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe UserFilterService, type: :model do | ||
subject { described_class.new(filters).filter(User.all) } | ||
let!(:user1) { FactoryGirl.create(:user) } | ||
let!(:user2) { FactoryGirl.create(:user, username: 'other') } | ||
|
||
context '#filter' do | ||
context 'by id' do | ||
let(:filters) do | ||
{ | ||
id: [user1.id] | ||
} | ||
end | ||
|
||
it 'includes only issues with matching id' do | ||
expect(subject).to eq([user1]) | ||
end | ||
end | ||
|
||
context 'by username' do | ||
let(:filters) do | ||
{ username: user1.username } | ||
end | ||
|
||
it 'includes only issues with matching username' do | ||
expect(subject).to eq([user1]) | ||
end | ||
end | ||
end | ||
end |