Module: Intermodal::RSpec::Accountability::ClassMethods

Defined in:
lib/intermodal/rspec/models/accountability.rb

Instance Method Summary collapse

Instance Method Details

#concerned_with_accountability(&blk) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/intermodal/rspec/models/accountability.rb', line 35

def concerned_with_accountability(&blk)
  instance_eval(&blk) if blk

  context 'when concerned with accountability' do
    let(:model) { subject.class }
    it { should belong_to :account }
    it { should validate_presence_of :account_id }
    it { model.should respond_to :by_account_id }
    it { model.should respond_to :by_account }

    implements_get_interface
  end
end

#implements_get_interface(&blk) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/intermodal/rspec/models/accountability.rb', line 49

def implements_get_interface(&blk)
  describe '.get' do
    let(:model) { subject.class }
    let(:different_account) { Account.make! }

    it { model.should respond_to :get }

    context ':all' do
      context 'when unscoped to account' do
        let(:collection) { model.get(:all) }
        it 'should find all resources' do
          collection.should include(resource)
          collection.size.should eql(1)
        end
      end

      context 'when scoped to account' do
        let(:collection) { model.get(:all, account: ) }
        it 'should find all resources' do
          collection.should include(resource)
          collection.size.should eql(1)
        end
      end
    end

    context 'by id' do
      it 'should find resource by id' do
        model.get(resource.id).should eql(resource)
      end

      it 'should return a writeable resource' do
        model.get(resource.id).should_not be_readonly
      end
    end

    context 'by account' do
      it 'should find resource scoped to account' do
        model.get(resource.id, account: ).should eql(resource)
      end

      it 'should find resource scoped to account id' do
        model.get(resource.id, account_id: .id).should eql(resource)
      end

      it 'should find a writeable resource scoped to account id' do
        model.get(resource.id, account_id: .id).should_not be_readonly
      end

      it 'should not find resource scoped to a different account' do
        expect { model.get(resource.id, account: ) }.to raise_error(ActiveRecord::RecordNotFound)
      end

      it 'should not find resource scoped to a different account id ' do
        expect { model.get(resource.id, account_id: .id) }.to raise_error(ActiveRecord::RecordNotFound)
      end
    end

    instance_eval(&blk) if blk
  end
end