summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2014-02-21 23:43:42 -0700
committermo khan <mo@mokhan.ca>2014-02-21 23:43:42 -0700
commit39fc3e184c1c185ffc9fc1f17bd19c5a8d9d0106 (patch)
tree47d42ae9531bae3d10cbea6a4b7eca5e7430c5f1
parent18e3102f4e64d206c72efb28bbe7021b1ba09a16 (diff)
move search filter method from controller down to model.
-rw-r--r--app/controllers/v1/company_licenses_controller.rb8
-rw-r--r--app/models/company.rb6
-rw-r--r--spec/controllers/v1/company_licenses_controller_spec.rb5
3 files changed, 10 insertions, 9 deletions
diff --git a/app/controllers/v1/company_licenses_controller.rb b/app/controllers/v1/company_licenses_controller.rb
index 234345c..3b4850f 100644
--- a/app/controllers/v1/company_licenses_controller.rb
+++ b/app/controllers/v1/company_licenses_controller.rb
@@ -2,7 +2,7 @@ class V1::CompanyLicensesController < ApplicationController
before_filter :load_company
def index
- @active_licenses = filter_using(@company, search_filters)
+ @active_licenses = @company.filter_licenses_using(search_filters)
render json: @active_licenses
end
@@ -12,12 +12,6 @@ class V1::CompanyLicensesController < ApplicationController
{ status: LicenseStatus.find_match(params[:status]), township: params[:township] }
end
- def filter_using(scope, filters)
- result = scope
- filters.each { |key, value| result = result.public_send(key, value) if value.present? }
- result
- end
-
def load_company
@company = Company.find(params[:company_id])
end
diff --git a/app/models/company.rb b/app/models/company.rb
index 7691bf5..630610f 100644
--- a/app/models/company.rb
+++ b/app/models/company.rb
@@ -4,4 +4,10 @@ class Company < ActiveRecord::Base
def status(status)
licenses.status(status)
end
+
+ def filter_licenses_using(search_filters)
+ result = self.licenses
+ search_filters.each { |key, value| result = result.public_send(key, value) if value.present? }
+ result
+ end
end
diff --git a/spec/controllers/v1/company_licenses_controller_spec.rb b/spec/controllers/v1/company_licenses_controller_spec.rb
index 198b4f3..e811179 100644
--- a/spec/controllers/v1/company_licenses_controller_spec.rb
+++ b/spec/controllers/v1/company_licenses_controller_spec.rb
@@ -2,18 +2,18 @@ require "spec_helper"
describe V1::CompanyLicensesController do
describe :index do
- let(:company) { Object.new }
+ let(:company) { Company.new }
let(:company_id) { SecureRandom.uuid }
let(:active_licenses) { ["active"] }
let(:active_licenses_in_township) { ["active township"] }
before :each do
Company.stub(:find).with(company_id).and_return(company)
- company.stub(:status).with(LicenseStatus::ACTIVE).and_return(active_licenses)
active_licenses.stub(:township).with("123").and_return(active_licenses_in_township)
end
it "returns the active licenses" do
+ company.stub(:filter_licenses_using).with({status: LicenseStatus::ACTIVE, township: nil}).and_return(active_licenses)
xhr :get, :index, company_id: company_id
assigns(:active_licenses).should == active_licenses
end
@@ -24,6 +24,7 @@ describe V1::CompanyLicensesController do
end
it "returns the active licenses for a given township" do
+ company.stub(:filter_licenses_using).with({status: LicenseStatus::ACTIVE, township: "123"}).and_return(active_licenses_in_township)
xhr :get, :index, company_id: company_id, township: "123"
assigns(:active_licenses).should == active_licenses_in_township
end