Skip to content

Instantly share code, notes, and snippets.

View BenEddy's full-sized avatar
💭
🍂

Ben Eddy BenEddy

💭
🍂
  • Seattle
View GitHub Profile
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
class MockActiveRelation < Hash
def scoped?
any?
end
def name_matching(name)
new(name_scope: name)
end
def scoped_by_name?(name)
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
class UserSearch
attr_reader :name, :email
def initialize(options = {})
@name = options[:name]
@email = options[:email]
end
def results
scope = User.scoped
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])
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 + "%")
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")
class UserSearch
attr_reader :name, :email
def initialize(options = {})
@name = options[:name]
@email = options[:email]
end
def results
scope = User.scoped
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] = ->
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