captproton / caboo.se-sample-app-v3

Fork of an export from svn of caboo.se sample app v3.

This URL has Read+Write access

caboo.se-sample-app-v3 / lib / rspec_extensions.rb
100644 37 lines (32 sloc) 0.982 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module Spec
  module Rails
    module Matchers
      class HaveValidAssociations
        def matches?(model)
          @failed_association = nil
          @model_class = model.class
 
          model.save(false)
      
          model.class.reflect_on_all_associations.each do |assoc|
            object = model.send(assoc.name, true) rescue @failed_association = assoc.name
 
            if object.nil?
              model.send(assoc.name, :reset) # does this actually do anything
            else
              begin
                model.send(assoc.name).reload
              rescue => err
                @failed_association = "#{assoc.name} => #{err.message}"
              end
            end
          end
          !@failed_association
        end
  
        def failure_message
          "invalid association \"#{@failed_association}\" on #{@model_class}"
        end
      end
 
      def have_valid_associations
        HaveValidAssociations.new
      end
    end
  end
end