Module: Intermodal::RSpec::HasParentResource::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#concerned_with_parent_resource(_parent_resource_name, options = {}, &blk) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/intermodal/rspec/models/has_parent_resource.rb', line 59

def concerned_with_parent_resource(_parent_resource_name, options = {}, &blk)
  extra_get_examples = options[:extra_get_examples]

  context "when concerned with parent resource #{_parent_resource_name}" do
    let(:parent_resource_name) { _parent_resource_name }
    let(:parent_id_name) { "#{parent_resource_name}_id" }
    let(:parent_model) { parent_resource_name.to_s.camelize.constantize }
    let(:different_parent) { parent_model.make! }
    let(:parent_with_different_account) { parent_model.make!(:account => ) }

    instance_eval(&blk) if blk

    it { should belong_to _parent_resource_name } unless options[:skip_association_examples]
    it { should validate_presence_of _parent_resource_name } unless options[:skip_validation_examples]

    [ :by_parent_id, :by_parent, "by_#{_parent_resource_name}_id", "by_#{_parent_resource_name}" ].each do |scope|
      should_respond_to_scope(scope)
    end

    implements_get_interface_for_nested_resource(&extra_get_examples)
  end
end

#implements_get_interface_for_nested_resource(&blk) ⇒ Object



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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/intermodal/rspec/models/has_parent_resource.rb', line 82

def implements_get_interface_for_nested_resource(&blk)
  implements_get_interface do

    context 'by parent' do
      it 'should find resource scoped to parent' do
        model.get(subject.id, :parent => subject.send(parent_resource_name)).should eql(subject)
      end

      it 'should find resource scoped to parent id' do
        model.get(subject.id, :parent_id => subject.send(parent_resource_name).id).should eql(subject)
      end

      it 'should find writeable resource scoped to parent id' do
        model.get(subject.id, :parent_id => subject.send(parent_resource_name).id).should_not be_readonly
      end
    end

    context 'by parent and account' do
      it 'should find resource scoped to account id and parent id' do
        model.get(subject.id, :parent_id => subject.send(parent_resource_name).id, :account_id => .id).should eql(subject)
      end

      it 'should find writeable resource scoped to account id and parent id' do
        model.get(subject.id, :parent_id => subject.send(parent_resource_name).id, :account_id => .id).should_not be_readonly
      end

      it 'should not find resource scoped to a different parent' do
        lambda { model.get(subject.id, :parent => different_parent) }.should raise_error(ActiveRecord::RecordNotFound)
      end

      it 'should not find resource scoped to a different parent id' do
        lambda { model.get(subject.id, :parent_id => different_parent.id) }.should raise_error(ActiveRecord::RecordNotFound)
      end

      it 'should not find resource scoped to a parent in a different account' do
        lambda { model.get(subject.id, :parent_id => .id, :account_id => ) }.should raise_error(ActiveRecord::RecordNotFound)
      end

      it 'should not find resource scoped to a correct parent but incorrect account' do
        lambda { model.get(subject.id, :parent_id => subject.send(parent_resource_name).id, :account_id => ) }.should raise_error(ActiveRecord::RecordNotFound)
      end
    end

    instance_eval(&blk) if blk
  end
end

#should_respond_to_scope(scope_method) ⇒ Object

NOTE: I am cheating. It assumes if the class method is defined then it is a scope



53
54
55
56
57
# File 'lib/intermodal/rspec/models/has_parent_resource.rb', line 53

def should_respond_to_scope(scope_method)
  it "should have scope #{scope_method}" do
    subject.class.should respond_to(scope_method)
  end
end