Module: RailsExtension::ActiveSupportExtension::Associations::ClassMethods

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

Overview

def self.included

Instance Method Summary collapse

Instance Method Details

#assert_requires_valid_associations(*associations) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/rails_extension/active_support_extension/associations.rb', line 168

def assert_requires_valid_associations(*associations)
#				options = associations.extract_options!
#				model = options[:model] || st_model_name
#	
#				associations.each do |assoc|
#					as = assoc = assoc.to_s
#					as = options[:as] if !options[:as].blank?
#	
#					test "#{brand}should require foreign key #{as}_id" do
#						assert_difference("#{model}.count",0) do
#							object = create_object("#{as}_id".to_sym => nil)
#							assert object.errors.on("#{as}_id".to_sym)
#						end
#					end
#	
#	#				test "#{brand}should require valid foreign key #{as}_id" do
#	#					assert_difference("#{model}.count",0) do
#	#						object = create_object("#{as}_id".to_sym => 0)
#	#						assert object.errors.on("#{as}_id".to_sym)
#	#					end
#	#				end
#	
#					title = "#{brand}should require valid association #{assoc}"
#					title << " as #{options[:as]}" if !options[:as].blank?
#					test title do
#						assert_difference("#{model}.count",0) { 
#							object = create_object(
#								assoc.to_sym => Factory.build(assoc.to_sym))
#	#							as.to_sym => Factory.build(assoc.to_sym))
#							assert object.errors.on("#{as}_id".to_sym)
#						}    
#					end 
#	
#				end

end

#assert_should_belong_to(*associations) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rails_extension/active_support_extension/associations.rb', line 52

def assert_should_belong_to(*associations)
	options = associations.extract_options!
	model = options[:model] || st_model_name
	associations.each do |assoc|
		class_name = ( assoc = assoc.to_s ).camelize
		title = "#{brand}should belong to #{assoc}" 
#				if !options[:as].blank?
#					title << " as #{options[:as]}"
#					as = options[:as]
#				end
		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]
		end
	end
end

#assert_should_habtm(*associations) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rails_extension/active_support_extension/associations.rb', line 146

def assert_should_habtm(*associations)
	options = associations.extract_options!
	model = options[:model] || st_model_name
	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



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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/rails_extension/active_support_extension/associations.rb', line 99

def assert_should_have_many_(*associations)
	options = associations.extract_options!
	model = options[:model] || st_model_name
#			foreign_key = if !options[:foreign_key].blank?
#				options[:foreign_key].to_sym
#			else
#				"#{model.underscore}_id".to_sym
#			end
	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("create_#{class_name.singularize.underscore}", foreign_key => object.id)
#					send("create_#{class_name.singularize.underscore}", model.underscore => object )
			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("create_#{class_name.singularize.underscore}", foreign_key => object.id)
#					send("create_#{class_name.singularize.underscore}", model.underscore => object )
			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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rails_extension/active_support_extension/associations.rb', line 77

def assert_should_have_one(*associations)
	options = associations.extract_options!
	model = options[:model] || st_model_name
#			foreign_key = if !options[:foreign_key].blank?
#				options[:foreign_key].to_sym
#			else
#				"#{model.underscore}_id".to_sym
#			end
	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}", foreign_key => object.id)
			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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rails_extension/active_support_extension/associations.rb', line 27

def assert_should_initially_belong_to(*associations)
	options = associations.extract_options!
	model = options[:model] || st_model_name
	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