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
describe UserSearch do | |
describe "#results" do | |
before { User.stub(scoped: MockActiveRelation.new) } | |
def results(options={}) | |
described_class.new(options).results | |
end | |
it "does not scope results by default" do | |
expect(results).to_not be_scoped |
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
class MockActiveRelation < Hash | |
def scoped? | |
any? | |
end | |
def name_matching(name) | |
new(name_scope: name) | |
end | |
def scoped_by_name?(name) |
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
describe UserSearch do | |
describe "#results" do | |
let(:user_scope) { double } | |
before do | |
User.stub(scoped: user_scope) | |
end | |
def results(options = {}) | |
described_class.new(options).results |
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
class UserSearch | |
attr_reader :name, :email | |
def initialize(options = {}) | |
@name = options[:name] | |
@email = options[:email] | |
end | |
def results | |
scope = User.scoped |
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
describe User do | |
describe ".name_matching" do | |
let!(:user) { User.create!(first_name: "Gaius", last_name: "Baltar") } | |
it "scopes to Users with matching first names" do | |
expect(User.name_matching("Gaius")).to eq([user]) | |
end | |
it "scopes to Users with matching last names" do | |
expect(User.name_matching("Baltar")).to eq([user]) |
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
class User < ActiveRecord::Base | |
attr_accessible :email, :first_name, :last_name | |
def self.name_matching(name) | |
where("lower(first_name) LIKE lower(:name) | |
OR lower(last_name) LIKE lower(:name)", name: name + "%") | |
end | |
def self.email_matching(email) | |
where("lower(email) LIKE lower(?)", email + "%") |
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
describe UserSearch do | |
describe "#results" do | |
it "returns all Users" do | |
user = User.create! | |
expect(described_class.new.results).to eq([user]) | |
end | |
context "a :name option is provided" do | |
it "returns Users with a matching first name" do | |
user = User.create!(first_name: "William") |
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
class UserSearch | |
attr_reader :name, :email | |
def initialize(options = {}) | |
@name = options[:name] | |
@email = options[:email] | |
end | |
def results | |
scope = User.scoped |
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
isFunction = (obj) -> | |
toString.call(obj) == '[object Function]' | |
Object.prototype.delegate = () -> | |
args = Array.prototype.slice.call(arguments); | |
[methods, options] = [args[0..(args.length - 2)], args[(args.length - 1)]] | |
for method in methods | |
delegation = (method, delegatedMethod) -> | |
@[method] = -> |
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
class User < ActiveRecord::Base | |
has_many :tasks | |
def self.with_flagged_comments | |
includes(:task).merge(Task.with_flagged_comments) | |
end | |
end | |
class Task < ActiveRecord::Base | |
belongs_to :user |