Module: TestDummy::ClassMethods
- Defined in:
- lib/test_dummy.rb
Instance Method Summary collapse
-
#build_dummy(with_attributes = nil, tags = nil) {|model| ... } ⇒ Object
Builds a dummy model with some parameters set as supplied.
-
#can_dummy?(*names) ⇒ Boolean
Returns true if all the supplied attribute names have defined dummy methods, or false otherwise.
-
#create_dummy(*args, &block) ⇒ Object
Builds a dummy model with some parameters set as supplied.
-
#create_dummy!(*args, &block) ⇒ Object
Builds a dummy model with some parameters set as supplied.
-
#dummy(*names, &block) ⇒ Object
Declares how to fake one or more attributes.
-
#dummy_attribute(name, with_attributes = nil) ⇒ Object
Produces dummy data for a single attribute.
-
#dummy_attributes(with_attributes = nil, tags = nil) ⇒ Object
Produces a complete set of dummy attributes.
-
#execute_dummy_operation(model, with_attributes = nil, tags = nil) ⇒ Object
This performs the dummy operation on a model with an optional set of parameters.
Instance Method Details
#build_dummy(with_attributes = nil, tags = nil) {|model| ... } ⇒ Object
Builds a dummy model with some parameters set as supplied. The new model is provided to the optional block for manipulation before the dummy operation is completed. Returns a dummy model which has not been saved.
255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/test_dummy.rb', line 255 def build_dummy(with_attributes = nil, = nil) load_dummy_declaration! build_scope = (method(:scoped).arity == 1) ? scoped(nil).scope(:create) : scoped.scope_for_create model = new(TestDummy::Support.combine_attributes(build_scope, with_attributes)) yield(model) if (block_given?) self.execute_dummy_operation(model, with_attributes, ) model end |
#can_dummy?(*names) ⇒ Boolean
Returns true if all the supplied attribute names have defined dummy methods, or false otherwise.
243 244 245 246 247 248 249 |
# File 'lib/test_dummy.rb', line 243 def can_dummy?(*names) @test_dummy ||= { } names.flatten.reject do |name| @test_dummy.key?(name) end.empty? end |
#create_dummy(*args, &block) ⇒ Object
Builds a dummy model with some parameters set as supplied. The new model is provided to the optional block for manipulation before the dummy operation is completed and the model is saved. Returns a dummy model. The model may not have been saved if there was a validation failure, or if it was blocked by a callback.
274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/test_dummy.rb', line 274 def create_dummy(*args, &block) if (args.last.is_a?(Hash)) with_attributes = args.pop end model = build_dummy(with_attributes, args, &block) model.save model end |
#create_dummy!(*args, &block) ⇒ Object
Builds a dummy model with some parameters set as supplied. The new model is provided to the optional block for manipulation before the dummy operation is completed and the model is saved. Returns a dummy model. Will throw ActiveRecord::RecordInvalid if there was al20 validation failure, or ActiveRecord::RecordNotSaved if the save was blocked by a callback.
292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/test_dummy.rb', line 292 def create_dummy!(*args, &block) if (args.last.is_a?(Hash)) with_attributes = args.pop end model = build_dummy(with_attributes, args, &block) model.save! model end |
#dummy(*names, &block) ⇒ Object
Declares how to fake one or more attributes. Accepts a block that can receive up to two parameters, the first the instance of the model being created, the second the parameters supplied to create it. The first and second parameters may be nil.
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/test_dummy.rb', line 105 def dummy(*names, &block) = nil case (names.last) when Hash = names.pop end @test_dummy ||= { } @test_dummy_order ||= [ ] @test_dummy_tags ||= { } names.flatten.each do |name| name = name.to_sym from = nil = nil if () if ([:only]) = [ [:only] ].flatten.compact if (.any?) set = @test_dummy_tags[name] ||= { } set[:only] = end end if ([:except]) = [ [:except] ].flatten.compact if (.any?) set = @test_dummy_tags[name] ||= { } set[:except] = end end if ([:with]) if (block) raise TestDummy::Exception, "Cannot use block and :with option at the same time." end block = case (with = [:with]) when Proc with when String, Symbol lambda { send(with) } else lambda { with } end end # The :inherit directive is used to pass arguments through to the # create_dummy call on the association's class. if (inherit = [:inherit]) = Hash[ inherit.collect do |attribute, spec| [ attribute.to_sym, case (spec) when Array spec.collect(&:to_sym) when String spec.split('.').collect(&:to_sym) when Proc spec end ] end ] = lambda do |target, model, with_attributes| .each do |attribute, spec| target[attribute] ||= case (spec) when Array spec.inject(model) do |_model, _method| _model ? _model.send(_method) : nil end when Proc proc.call(model, with_attributes) end end end end if (from = [:from]) if (block) raise TestDummy::Exception, "Cannot use block, :with, or :from option at the same time." end case (from) when Array # Already in correct form when Hash from = from.to_a when String from = from.split('.') else raise TestDummy::Exception, "Argument to :from must be a String, Array or Hash." end end reflection_class, foreign_key = TestDummy::Support.reflection_properties(self, name) if (reflection_class and foreign_key) block = lambda do |model, with_attributes| unless ((with_attributes and (with_attributes.key?(name) or with_attributes.key?(foreign_key))) or model.send(name).present?) object = from && from.inject(model) do |_model, _method| _model ? _model.send(_method) : nil end object ||= reflection_class.create_dummy(with_attributes) do |target| if () .call(target, model, with_attributes) end end model.send(:"#{name}=", object) end end end end # For associations, delay creation of block until first call # to allow for additional relationships to be defined after # the to_dummy call. Leave placeholder (true) instead. @test_dummy[name] = block || true @test_dummy_order << name end end |
#dummy_attribute(name, with_attributes = nil) ⇒ Object
Produces dummy data for a single attribute.
305 306 307 308 309 |
# File 'lib/test_dummy.rb', line 305 def dummy_attribute(name, with_attributes = nil) with_attributes = TestDummy.combine_attributes(scoped.scope_for_create, with_attributes) dummy_method_call(nil, with_attributes, dummy_method(name)) end |
#dummy_attributes(with_attributes = nil, tags = nil) ⇒ Object
Produces a complete set of dummy attributes. These can be used to create a model.
97 98 99 |
# File 'lib/test_dummy.rb', line 97 def dummy_attributes @test_dummy ||= { } end |
#execute_dummy_operation(model, with_attributes = nil, tags = nil) ⇒ Object
This performs the dummy operation on a model with an optional set of parameters.
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
# File 'lib/test_dummy.rb', line 339 def execute_dummy_operation(model, with_attributes = nil, = nil) load_dummy_declaration! return model unless (@test_dummy_order) @test_dummy_order.each do |name| if (tag_conditions = @test_dummy_tags[name]) if ( = tag_conditions[:only]) next if (! or ( & ).empty?) end if ( = tag_conditions[:except]) next if ( and ( & ).any?) end end if (respond_to?(:reflect_on_association) and reflection = reflect_on_association(name)) foreign_key = (reflection.respond_to?(:foreign_key) ? reflection.foreign_key : reflection.primary_key_name).to_sym unless ((with_attributes and (with_attributes.key?(name.to_sym) or with_attributes.key?(foreign_key.to_sym))) or model.send(name).present?) model.send(:"#{name}=", dummy_method_call(model, with_attributes, dummy_method(name))) end elsif (respond_to?(:association_reflection) and reflection = association_reflection(name)) key = reflection[:key] || :"#{name.to_s.underscore}_id" unless ((with_attributes and (with_attributes.key?(name.to_sym) or with_attributes.key?(key.to_sym))) or model.send(name).present?) model.send(:"#{name}=", dummy_method_call(model, with_attributes, dummy_method(name))) end else unless (with_attributes and (with_attributes.key?(name.to_sym) or with_attributes.key?(name.to_s))) model.send(:"#{name}=", dummy_method_call(model, with_attributes, dummy_method(name))) end end end model end |