# encoding: UTF-8

require 'spec_helper'

describe Table do
  let(:supplier){ create(:supplier) }
  describe 'for_supplier' do
    let(:options){ Hash.new }
    subject{ Table.for_supplier(supplier, options) }
    it "should return an empty array by default" do
      subject.should be_empty
    end
    describe 'with multiple tables' do
      before do
        @table1 = create :table, supplier: supplier, number: 2
        @table2 = create :table, supplier: supplier, number: 4
        @table3 = create :table, supplier: supplier, number: 6
        @table4 = create :table, number: 4
      end
      it "returns all by default" do
        subject.should == [@table1, @table2, @table3]
      end

      it "should paginate the result" do
        options[:per_page] = 2
        subject.size.should == 2
        subject.total_count.should == 3
      end

      it "should filter by table number using from_number" do
        options[:per_page] = 1
        options[:from_number] = 3
        subject.size.should == 1
        subject.total_pages.should == 2
        subject.first.number.should == 4

      end
      it "should filter by table number using from_number and to_number" do
        options[:from_number] = 3
        options[:to_number] = 5
        subject.size.should == 1
        subject.total_pages.should == 1
        subject.first.number.should == 4

      end
    end
  end

  describe 'enrich_active_list_id' do
    before do
      @table1 = create :table, supplier: supplier, number: 2
      @list = create :list, supplier: supplier, table: @table1, state: 'active'
    end

    # False passes can be possible when list is not valid
    it 'has created a list' do
      @list.id.should be_present
    end

    it 'enriches active_list_id using a single object' do
      Table.enrich_active_list_id(@table1).active_list_id.should == @list.id
    end

    it 'enriches active_list_id using an array' do
      Table.enrich_active_list_id([@table1]).first.active_list_id.should == @list.id
    end

    it "does not enrich non active lists" do
      @list.update_attributes(state: 'closed')
      Table.enrich_active_list_id([@table1]).first.active_list_id.should be_nil
    end
  end

end
