Skip to content

Instantly share code, notes, and snippets.

@rudyjahchan
Last active December 19, 2015 08:09
Show Gist options
  • Select an option

  • Save rudyjahchan/5923483 to your computer and use it in GitHub Desktop.

Select an option

Save rudyjahchan/5923483 to your computer and use it in GitHub Desktop.
require 'spec_helper'
describe Image do
it_behaves_like 'it references creators and places'
it_behaves_like 'it supports tagging'
it_behaves_like 'it has a hash bag'
end
class Image < ActiveRecord::Base
end
class Assignment < ActiveRecord::Base
belongs_to :image
belongs_to :gallery
end
class Gallery < ActiveRecord::Base
has_many :assignments
end
require 'spec_helper'
describe Image do
describe '#creators' do
# specs to test the creators relationship
end
describe '#places' do
# specs to test the places relationship
end
describe '.by_tags' do
let!(:images) do
[
create(:image, tags: ['foo']),
create(:image, tags: ['bar', 'baz', 'foo']),
create(:image, tags: ['foo', 'baz']),
create(:image, tags: ['bar']),
create(:image, tags: ['baz', 'foo'])
]
end
context 'given a list of tags' do
let(:images_by_tags) { Image.by_tags [ 'foo', 'baz'] }
it 'retrieves only images that have ALL tags' do
expect(images_by_tags).to have(3).images
expect(images_by_tags).to include(images[1], images[2], images[4])
expect(images_by_tags).to_not include(images[0], images[3])
end
end
end
describe '.has_key_value' do
let!(:images) do
[
create(:image, hash_bag: { foo: 'bar', sna: 'foo' }),
create(:image, hash_bag: { foo: 'baz' }),
create(:image, hash_bag: { foo: 'bar' }),
create(:image, hash_bag: { sna: 'foo' }),
create(:image, hash_bag: { baz: 'luhrmann', foo: 'bar', sna: 'foo' })
]
end
context 'given a key and value' do
context 'with no wild card operators' do
let(:images_with_key_value) { Image.has_key_value :foo, 'bar' }
it 'returns all images who have that exact value for the key' do
expect(images_with_key_value).to have(3).items
expect(images_with_key_value).to include(images[0], images[2], images[4])
expect(images_with_key_value).to_not include(images[1], images[3])
end
end
context 'with wild card operators' do
let(:images_with_key_value) { Image.has_key_value :foo, 'ba%' }
it 'returns all images who have a matching value for the key' do
expect(images_with_key_value).to have(4).items
expect(images_with_key_value).to include(images[0], images[1], images[2], images[4])
expect(images_with_key_value).to_not include(images[3])
end
end
end
end
end
class Image < ActiveRecord::Base
has_many :creators
has_many :places
attr_accessible :tags, :hash_bag
serialize :hash_bag, ActiveRecord::Coders::Hstore
scope :by_tags, lambda { |*tags| where('tags @> ARRAY[?]', tags.flatten) }
scope :has_key_value, lambda { |key, value| where('hash_bag -> ? LIKE ?', key, value) }
# ... code specific to other Image functionality ...
end
shared_example 'a name' do
it 'has an example' do
expect(true).to be_false
end
it 'has another example' do
expect(1 + 1).to eql(2)
end
end
describe 'my specs' do
it 'has my own example' do
expect(null).to be
end
it_behaves_like 'a name'
end
shared_example 'with parameter' do |param1, param2|
it 'use parameters' do
expect(param1).to eql(param2)
end
end
describe 'passing parameters' do
it_behaves_like 'with parameters', :rudy, 'rudy'
end
shared_examples 'it references creators and places' do |model, factory|
describe '#creators' do
# specs to test the creators relationship
end
describe '#places' do
# specs to test the places relationship
end
end
shared_examples 'it supports tagging' do |model, factory|
describe '.by_tags' do
let!(:models) do
[
create(factory, tags: ['foo']),
create(factory, tags: ['bar', 'baz', 'foo']),
create(factory, tags: ['foo', 'baz']),
create(factory, tags: ['bar']),
create(factory, tags: ['baz', 'foo'])
]
end
context 'given a list of tags' do
let(:models_by_tags) { model.by_tags [ 'foo', 'baz'] }
it "retrieves only #{model.to_s.pluralize} that have ALL tags" do
expect(models_by_tags).to have(3).models
expect(models_by_tags).to include(models[1], models[2], models[4])
expect(models_by_tags).to_not include(models[0], models[3])
end
end
end
end
shared_examples 'it has a hash bag' do |model, factory|
describe '.has_key_value' do
let!(:models) do
[
create(factory, hash_bag: { foo: 'bar', sna: 'foo' }),
create(factory, hash_bag: { foo: 'baz' }),
create(factory, hash_bag: { foo: 'bar' }),
create(factory, hash_bag: { sna: 'foo' }),
create(factory, hash_bag: { baz: 'luhrmann', foo: 'bar', sna: 'foo' })
]
end
context 'given a key and value' do
context 'with no wild card operators' do
let(:models_with_key_value) { model.has_key_value :foo, 'bar' }
it 'returns all items who have that exact value for the key' do
expect(models_with_key_value).to have(3).items
expect(models_with_key_value).to include(models[0], models[2], models[4])
expect(models_with_key_value).to_not include(models[1], models[3])
end
end
context 'with wild card operators' do
let(:models_with_key_value) { model.has_key_value :foo, 'ba%' }
it 'returns all items who have a matching value for the key' do
expect(models_with_key_value).to have(4).items
expect(models_with_key_value).to include(models[0], models[1], models[2], models[4])
expect(models_with_key_value).to_not include(models[3])
end
end
end
end
end
require 'spec_helper'
describe Image do
it_behaves_like 'it references creators and places', Image, :image
it_behaves_like 'it supports tagging', Image, :image
it_behaves_like 'it has a hash bag', Image, :image
end
describe Gallery do
it_behaves_like 'it supports tagging', Gallery, :gallery
it_behaves_like 'it has a hash bag', Gallery, :gallery
# ... additional gallery specific specs
end
describe Assignment do
it_behaves_like 'it has a hash bag', Assignment, :assignment
# ... additional assignment specific specs
end
module References
extend ActiveSupport::Concern
included do
has_many :creators
has_many :places
end
end
module Tags
extend ActiveSupport::Concern
included do
attr_accessible :tags
scope :by_tags, lambda { |*tags| where('tags @> ARRAY[?]', tags.flatten) }
end
end
module HashBag
extend ActiveSupport::Concern
included do
attr_accessible :hash_bag
serialize :hash_bag, ActiveRecord::Coders::Hstore
scope :has_key_value, lambda { |key, value| where('hash_bag -> ? LIKE ?', key, value) }
end
end
class Image < ActiveRecord::Base
include References
include Tags
include HashBag
end
class Assignment < ActiveRecord::Base
include HashBag
belongs_to :image
belongs_to :gallery
end
class Gallery < ActiveRecord::Base
include Tags
include HashBag
has_many :assignments
end
shared_context 'subject class' do
let(:model) { subject.class }
let(:factory) { model.to_s.underscore.to_sym }
end
shared_examples 'it references creators and places'
include_context 'subject class'
describe '#creators' do
# specs to test the creators relationship
end
describe '#places' do
# specs to test the places relationship
end
end
shared_examples 'it supports tagging'
include_context 'subject class'
describe '.by_tags' do
let!(:models) do
[
create(factory, tags: ['foo']),
create(factory, tags: ['bar', 'baz', 'foo']),
create(factory, tags: ['foo', 'baz']),
create(factory, tags: ['bar']),
create(factory, tags: ['baz', 'foo'])
]
end
context 'given a list of tags' do
let(:models_by_tags) { model.by_tags [ 'foo', 'baz'] }
it "retrieves only #{model.to_s.pluralize} that have ALL tags" do
expect(models_by_tags).to have(3).models
expect(models_by_tags).to include(models[1], models[2], models[4])
expect(models_by_tags).to_not include(models[0], models[3])
end
end
end
end
shared_examples 'it has a hash bag'
include_context 'subject class'
describe '.has_key_value' do
let!(:models) do
[
create(factory, hash_bag: { foo: 'bar', sna: 'foo' }),
create(factory, hash_bag: { foo: 'baz' }),
create(factory, hash_bag: { foo: 'bar' }),
create(factory, hash_bag: { sna: 'foo' }),
create(factory, hash_bag: { baz: 'luhrmann', foo: 'bar', sna: 'foo' })
]
end
context 'given a key and value' do
context 'with no wild card operators' do
let(:models_with_key_value) { model.has_key_value :foo, 'bar' }
it 'returns all items who have that exact value for the key' do
expect(models_with_key_value).to have(3).items
expect(models_with_key_value).to include(models[0], models[2], models[4])
expect(models_with_key_value).to_not include(models[1], models[3])
end
end
context 'with wild card operators' do
let(:models_with_key_value) { model.has_key_value :foo, 'ba%' }
it 'returns all items who have a matching value for the key' do
expect(models_with_key_value).to have(4).items
expect(models_with_key_value).to include(models[0], models[1], models[2], models[4])
expect(models_with_key_value).to_not include(models[3])
end
end
end
end
end
describe Image do
it_behaves_like 'it references creators and places'
it_behaves_like 'it supports tagging'
it_behaves_like 'it has a hash bag'
end
describe Gallery do
it_behaves_like 'it supports tagging'
it_behaves_like 'it has a hash bag'
# ... additional gallery specific specs
end
describe Assignment do
it_behaves_like 'it has a hash bag'
# ... additional assignment specific specs
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment