Last active
December 19, 2015 08:09
-
-
Save rudyjahchan/5923483 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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 |
This file contains hidden or 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 Image < ActiveRecord::Base | |
| end | |
| class Assignment < ActiveRecord::Base | |
| belongs_to :image | |
| belongs_to :gallery | |
| end | |
| class Gallery < ActiveRecord::Base | |
| has_many :assignments | |
| end |
This file contains hidden or 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
| 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 |
This file contains hidden or 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 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 |
This file contains hidden or 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
| 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 |
This file contains hidden or 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
| 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 |
This file contains hidden or 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 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 |
This file contains hidden or 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
| 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 |
This file contains hidden or 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 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 |
This file contains hidden or 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
| shared_context 'subject class' do | |
| let(:model) { subject.class } | |
| let(:factory) { model.to_s.underscore.to_sym } | |
| end |
This file contains hidden or 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
| 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 |
This file contains hidden or 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 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