Skip to content

Instantly share code, notes, and snippets.

@nicolaslazartekaqui
Last active August 29, 2015 14:02
Show Gist options
  • Save nicolaslazartekaqui/dfac568ae15f08063991 to your computer and use it in GitHub Desktop.
Save nicolaslazartekaqui/dfac568ae15f08063991 to your computer and use it in GitHub Desktop.
# app/models/bar.rb
class Bar < ActiveRecord::Base
include Searchable
end
# app/models/foo.rb
class Foo < ActiveRecord::Base
include Searchable
end
# app/models/concern/searchable.rb
module Searchable
extend ActiveSupport::Concern
included do
scope :term, ->(term) do
where { name.like("%#{term}%") }
end
end
end
# spec/concerns/searchable_spec.rb
require 'spec_helper'
describe Searchable do
it "where name like a term" do
@foo_1 = Foo.create(name: 'Foo')
@foo_2 = Foo.create(name: 'Boo')
expect(Foo.term('F')).to include @foo_1
expect(Foo.term('F')).to_not include @foo_2
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment