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

Commit

Permalink
Extract search logic from controller
Browse files Browse the repository at this point in the history
  • Loading branch information
biow0lf committed May 25, 2015
1 parent 4047788 commit e63d1a5
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
8 changes: 4 additions & 4 deletions app/controllers/srpms_controller.rb
Expand Up @@ -81,8 +81,8 @@ def bugs

names = @srpm.packages.map { |package| package.name }.flatten.sort.uniq

@bugs = Bug.where(component: names, bug_status: ['NEW', 'ASSIGNED', 'VERIFIED', 'REOPENED']).order('bug_id DESC')
@allbugs = Bug.where(component: names).order('bug_id DESC')
@bugs = Bug.opened_bugs_for(names)
@allbugs = Bug.all_bugs_for(names)
end

def allbugs
Expand All @@ -92,8 +92,8 @@ def allbugs

names = @srpm.packages.map { |package| package.name }.flatten.sort.uniq

@bugs = Bug.where(component: names, bug_status: ['NEW', 'ASSIGNED', 'VERIFIED', 'REOPENED']).order('bug_id DESC')
@allbugs = Bug.where(component: names).order('bug_id DESC')
@bugs = Bug.opened_bugs_for(names)
@allbugs = Bug.all_bugs_for(names)
end

def repocop
Expand Down
10 changes: 10 additions & 0 deletions app/models/bug.rb
@@ -1,2 +1,12 @@
class Bug < ActiveRecord::Base
validates :bug_id, presence: true

def self.opened_bugs_for(components)
bug_statuses = %w(NEW ASSIGNED VERIFIED REOPENED)
where(component: components, bug_status: bug_statuses).order('bug_id DESC')
end

def self.all_bugs_for(components)
where(component: components).order('bug_id DESC')
end
end
47 changes: 43 additions & 4 deletions spec/models/bug_spec.rb
@@ -1,9 +1,48 @@
require 'rails_helper'

describe Bug do
context 'DB Indexes' do
it { is_expected.to have_db_index :assigned_to }
it { is_expected.to have_db_index :bug_status }
it { is_expected.to have_db_index :product }
describe 'Validations' do
specify { should validate_presence_of :bug_id }
end

describe 'DB Indexes' do
specify { should have_db_index :assigned_to }
specify { should have_db_index :bug_status }
specify { should have_db_index :product }
end

describe '.opened_bugs_for' do
before do
#
# bug_statuses = %w(NEW ASSIGNED VERIFIED REOPENED)
# Bug.where(component: components, bug_status: bug_statuses).order('bug_id DESC')
#
bug_statuses = %w(NEW ASSIGNED VERIFIED REOPENED)
components = ['anything']

expect(described_class).to receive(:where).with(component: components, bug_status: bug_statuses) do
double.tap do |a|
expect(a).to receive(:order).with('bug_id DESC')
end
end
end

specify { expect { described_class.opened_bugs_for(['anything']) }.not_to raise_error }
end

describe '.all_bugs_for' do
before do
#
# Bug.where(component: components).order('bug_id DESC')
#
components = ['anything']
expect(described_class).to receive(:where).with(component: components) do
double.tap do |a|
expect(a).to receive(:order).with('bug_id DESC')
end
end
end

specify { expect { described_class.all_bugs_for(['anything']) }.not_to raise_error }
end
end

0 comments on commit e63d1a5

Please sign in to comment.