Sha256: ff34f5870f64ff6621785b097f8c5e7a6d315ee25859979428f9b6b6d692d10a

Contents?: true

Size: 1.84 KB

Versions: 7

Compression:

Stored size: 1.84 KB

Contents

require 'spec_helper'

describe ActiveFedora::Associations::HasManyAssociation do
  before do
    class Book < ActiveFedora::Base
    end
    class Page < ActiveFedora::Base
    end
  end

  after do
    Object.send(:remove_const, :Book)
    Object.send(:remove_const, :Page)
  end
  let(:book) { Book.new('subject-a') }
  let(:page) { Page.new('object-b') }

  describe "setting the foreign key" do
    before do
      allow(book).to receive(:new_record?).and_return(false)
      allow(page).to receive(:save).and_return(true)
      allow(ActiveFedora::SolrService).to receive(:query).and_return([])
    end

    let(:reflection) { Book.create_reflection(:has_many, 'pages', { predicate: ActiveFedora::RDF::Fcrepo::RelsExt.isPartOf }, Book) }
    let(:association) { ActiveFedora::Associations::HasManyAssociation.new(book, reflection) }

    it "should set the book_id attribute" do
      expect(association).to receive(:callback).twice
      expect(page).to receive(:[]=).with('book_id', book.id)
      association.concat page
    end
  end

  describe "Finding a polymorphic inverse relation" do

    before do
      # :books must come first, so that we can test that is being passed over in favor of :contents
      Page.has_many :books, predicate: ActiveFedora::RDF::Fcrepo::RelsExt.isPartOf, class_name: 'ActiveFedora::Base'
      Page.has_and_belongs_to_many :contents, predicate: ActiveFedora::RDF::Fcrepo::RelsExt.isPartOf, class_name: 'ActiveFedora::Base'
    end
    let(:book_reflection) { Book.create_reflection(:has_many, 'pages', { predicate: ActiveFedora::RDF::Fcrepo::RelsExt.isPartOf }, Book) }
    let(:association) { ActiveFedora::Associations::HasManyAssociation.new(book, book_reflection) }

    subject { association.send(:find_polymorphic_inverse, page) }

    it "should find the HABTM reflection" do
      expect(subject.name).to eq :contents
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
active-fedora-9.0.6 spec/unit/has_many_association_spec.rb
active-fedora-9.0.5 spec/unit/has_many_association_spec.rb
active-fedora-9.0.4 spec/unit/has_many_association_spec.rb
active-fedora-9.0.3 spec/unit/has_many_association_spec.rb
active-fedora-9.0.2 spec/unit/has_many_association_spec.rb
active-fedora-9.0.1 spec/unit/has_many_association_spec.rb
active-fedora-9.0.0 spec/unit/has_many_association_spec.rb