Module: CommonLib::ActiveSupportExtension::Associations::ClassMethods

Defined in:
lib/common_lib/active_support_extension/associations.rb

Overview

def self.included

Instance Method Summary collapse

Instance Method Details

#assert_should_belong_to(*associations) ⇒ Object



47
48
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
# File 'lib/common_lib/active_support_extension/associations.rb', line 47

def assert_should_belong_to(*associations)
  options = associations.extract_options!
  model = options[:model] || model_name_without_test
  associations.each do |assoc|
    class_name = ( assoc = assoc.to_s ).camelize
    title = "#{brand}should belong to #{assoc}" 
    if !options[:class_name].blank?
      title << " ( #{options[:class_name]} )"
      class_name = options[:class_name].to_s
    end
    test title do
      object = create_object
      assert_nil object.send(assoc)
      object.send("#{assoc}=",send("create_#{class_name.underscore}"))
      assert_not_nil object.send(assoc)
      assert object.send(assoc).is_a?(class_name.constantize
        ) unless options[:polymorphic]
# Paperclip attachments don't get deleted on rollback.
# So we much destroy the object, and therefore the attachment, by hand.
# Only seems to matter with ...
#   BirthDatumTest#assert_should_belong_to( :birth_datum_update )
#   but does't seem to cause a problem elsewhere.
object.send(assoc).destroy
object.destroy
    end
  end
end

#assert_should_habtm(*associations) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/common_lib/active_support_extension/associations.rb', line 129

def assert_should_habtm(*associations)
  options = associations.extract_options!
  model = options[:model] || model_name_without_test
  associations.each do |assoc|
    assoc = assoc.to_s
    test "#{brand}should habtm #{assoc}" do
      object = create_object
      assert_equal 0, object.send(assoc).length
      object.send(assoc) << send("create_#{assoc.singularize}")
      assert_equal 1, object.reload.send(assoc).length
      if object.respond_to?("#{assoc}_count")
        assert_equal 1, object.reload.send("#{assoc}_count")
      end
      object.send(assoc) << send("create_#{assoc.singularize}")
      assert_equal 2, object.reload.send(assoc).length
      if object.respond_to?("#{assoc}_count")
        assert_equal 2, object.reload.send("#{assoc}_count")
      end
    end
  end
end

#assert_should_have_many_(*associations) ⇒ Object



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/common_lib/active_support_extension/associations.rb', line 91

def assert_should_have_many_(*associations)
  options = associations.extract_options!
  model = options[:model] || model_name_without_test
  associations.each do |assoc|
    class_name = ( assoc = assoc.to_s ).camelize
    title = "#{brand}should have many #{assoc}"
    if !options[:class_name].blank?
      title << " ( #{options[:class_name]} )"
      class_name = options[:class_name].to_s
    end
    if !options[:as].blank?
      title << " ( as #{options[:as]} )"
    end
    test title do
      object = create_object
      assert_equal 0, object.send(assoc).length
      command = ["create_#{class_name.singularize.underscore}"]
      if !options[:foreign_key].blank?
        command.push( options[:foreign_key].to_sym => object.id )
      elsif !options[:as].blank?
        command.push( options[:as].to_sym => object )
      else
        command.push( model.underscore => object )
      end
      send *command
      assert_equal 1, object.reload.send(assoc).length
      if object.respond_to?("#{assoc}_count")
        assert_equal 1, object.reload.send("#{assoc}_count")
      end
      send *command
      assert_equal 2, object.reload.send(assoc).length
      if object.respond_to?("#{assoc}_count")
        assert_equal 2, object.reload.send("#{assoc}_count")
      end
    end
  end
end

#assert_should_have_one(*associations) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/common_lib/active_support_extension/associations.rb', line 75

def assert_should_have_one(*associations)
  options = associations.extract_options!
  model = options[:model] || model_name_without_test
  associations.each do |assoc|
    assoc = assoc.to_s
    test "#{brand}should have one #{assoc}" do
      object = create_object
      assert_nil object.reload.send(assoc)
      send("create_#{assoc}", model.underscore => object )
      assert_not_nil object.reload.send(assoc)
      object.send(assoc).destroy
      assert_nil object.reload.send(assoc)
    end
  end
end

#assert_should_initially_belong_to(*associations) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/common_lib/active_support_extension/associations.rb', line 22

def assert_should_initially_belong_to(*associations)
  options = associations.extract_options!
  model = options[:model] || model_name_without_test
  associations.each do |assoc|
    class_name = ( assoc = assoc.to_s ).camelize
    title = "#{brand}should initially belong to #{assoc}"
    if !options[:class_name].blank?
      title << " ( #{options[:class_name]} )"
      class_name = options[:class_name].to_s
    end
    test title do
      object = create_object
      assert_not_nil object.send(assoc)
      if object.send(assoc).respond_to?(
        "#{model.underscore.pluralize}_count")
        assert_equal 1, object.reload.send(assoc).send(
          "#{model.underscore.pluralize}_count")
      end
      if !options[:class_name].blank?
        assert object.send(assoc).is_a?(class_name.constantize)
      end
    end
  end
end