Module: ObjectDaddy::RailsClassMethods

Defined in:
lib/object_daddy.rb

Instance Method Summary collapse

Instance Method Details

#exemplar_pathObject



188
189
190
191
192
193
194
195
# File 'lib/object_daddy.rb', line 188

def exemplar_path
  paths = ['spec', 'test'].inject([]) do |array, dir|
    if File.directory?(File.join(RAILS_ROOT, dir))
      array << File.join(RAILS_ROOT, dir, 'exemplars')
    end
    array
  end
end

#generate(args = {}) ⇒ Object

:call-seq:

generate()
generate() do |obj| ... end
generate(args)
generate(args) do |obj| ... end

Creates and tries to save an instance of this class, using any known generators. The generated instance is yielded to a block if provided.

This will not raise errors on a failed save. Use generate! if you want errors raised.



216
217
218
219
220
221
# File 'lib/object_daddy.rb', line 216

def generate(args = {})
  spawn(args) do |instance|
    instance.save
    yield instance if block_given?
  end
end

#generate!(args = {}) ⇒ Object

:call-seq:

generate()
generate() do |obj| ... end
generate(args)
generate(args) do |obj| ... end

Creates and tries to save! an instance of this class, using any known generators. The generated instance is yielded to a block if provided.

This will raise errors on a failed save. Use generate if you do not want errors raised.



234
235
236
237
238
239
# File 'lib/object_daddy.rb', line 234

def generate!(args = {})
  spawn(args) do |instance|
    instance.save!
    yield instance if block_given?
  end
end

#validates_presence_of_with_object_daddy(*attr_names) ⇒ Object



197
198
199
200
201
202
203
# File 'lib/object_daddy.rb', line 197

def validates_presence_of_with_object_daddy(*attr_names)
  @presence_validated_attributes ||= {}
  new_attr = attr_names.dup
  new_attr.pop if new_attr.last.is_a?(Hash)
  new_attr.each {|a| @presence_validated_attributes[a] = true }
  validates_presence_of_without_object_daddy(*attr_names)
end