Sha256: e7ce48a701107a4decc326f9a4c26ed54f48550e8f0a24df9d9812cd0ce84996

Contents?: true

Size: 1.97 KB

Versions: 23

Compression:

Stored size: 1.97 KB

Contents

require 'spec_helper'

class Musician # Mock AR model
  attr_accessor :id, :name, :rating, :errors, :birthplace_id
  def initialize(params={}); params.each{|k,v| self.send("#{k}=",v) if self.respond_to?(k)}; end
  def attributes; {:id => self.id, :name => self.name, :rating => self.rating}; end
end

class Birthplace # Mock AR model
  attr_accessor :id, :city, :state
  def initialize(params={}); params.each{|k,v| self.send("#{k}=",v) if self.respond_to?(k)}; end
  def attributes; {:id => self.id, :city => self.city, :state => self.state}; end
end

class Song # Mock AR model
  attr_accessor :id, :title, :rating
  def initialize(params={}); params.each{|k,v| self.send("#{k}=",v) if self.respond_to?(k)}; end
  def attributes; {:id => self.id, :title => self.title}; end
end

module MyApi

  class Birthplace
    include Faceted::Presenter
    presents :birthplace
    field :city
    field :state
  end

  class Musician
    include Faceted::Presenter
    presents :musician
    field :name
    field :rating
    field :birthplace_id
  end

  class Song
    include Faceted::Presenter
    presents :song
    field :title
    field :rating
  end

  class Band
    include Faceted::Collector
    collects :musicians, :find_by => :birthplace_id
    collects :songs
  end

  describe Band do

    it 'creates an accessor method for its collected objects' do
      Band.new.respond_to?(:musicians).should be_true
      Band.new.respond_to?(:songs).should be_true
    end

    describe 'with associated objects' do

      it 'initializes the associated objects in the correct namespace' do
        band = MyApi::Band.new(:birthplace_id => 1)
        band.stub(:id) { 3 }
        MyApi::Musician.stub(:where) { [MyApi::Musician.new] }
        MyApi::Song.stub(:where) { [MyApi::Song.new] }
        MyApi::Song.should_receive(:where).with('band_id' => 3)
        band.musicians.first.class.name.should == "MyApi::Musician"
        band.songs.first.class.name.should == "MyApi::Song"

      end

    end

  end

end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
faceted-1.6.2 spec/collector_spec.rb
faceted-1.6.1 spec/collector_spec.rb
faceted-1.6.0 spec/collector_spec.rb
faceted-1.5.1 spec/collector_spec.rb
faceted-1.5.0 spec/collector_spec.rb
faceted-1.4.0 spec/collector_spec.rb
faceted-1.3.3 spec/collector_spec.rb
faceted-1.3.2 spec/collector_spec.rb
faceted-1.3.1 spec/collector_spec.rb
faceted-1.3.0 spec/collector_spec.rb
faceted-1.2.3 spec/collector_spec.rb
faceted-1.2.2 spec/collector_spec.rb
faceted-1.2.1 spec/collector_spec.rb
faceted-1.2.0 spec/collector_spec.rb
faceted-1.1.1 spec/collector_spec.rb
faceted-1.1.0 spec/collector_spec.rb
faceted-1.0.8 spec/collector_spec.rb
faceted-1.0.7 spec/collector_spec.rb
faceted-1.0.6 spec/collector_spec.rb
faceted-1.0.5 spec/collector_spec.rb