Module: TestDummy::ClassMethods

Defined in:
lib/test_dummy.rb

Instance Method Summary collapse

Instance Method Details

#build_dummy(create_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.

Yields:

  • (model)


142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/test_dummy.rb', line 142

def build_dummy(create_attributes = nil, tags = nil)
  build_scope = where(nil)

  create_attributes = TestDummy::Support.combine_attributes(build_scope, create_attributes)

  model = new(create_attributes)

  yield(model) if (block_given?)

  self.dummy_definition.apply!(model, create_attributes, tags)

  model
end

#can_dummy?(*fields) ⇒ Boolean

Returns true if all the supplied attribute fields have defined dummy methods, or false otherwise.

Returns:

  • (Boolean)


134
135
136
# File 'lib/test_dummy.rb', line 134

def can_dummy?(*fields)
  @test_dummy and @test_dummy.can_dummy?(*fields) or false
end

#create_dummy(*tags, &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.



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/test_dummy.rb', line 161

def create_dummy(*tags, &block)
  if (tags.last.is_a?(Hash))
    create_attributes = tags.pop
  end

  model = build_dummy(create_attributes, tags, &block)

  model.save

  self.dummy_definition.apply_after_save!(model, create_attributes, tags)
  
  model
end

#create_dummy!(*tags, &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.



181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/test_dummy.rb', line 181

def create_dummy!(*tags, &block)
  if (tags.last.is_a?(Hash))
    create_attributes = tags.pop
  end

  model = build_dummy(create_attributes, tags, &block)
  
  model.save!
  
  self.dummy_definition.apply_after_save!(model, create_attributes, tags)

  model
end

#dummy(*fields) ⇒ 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.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/test_dummy.rb', line 116

def dummy(*fields)
  options =
    case (fields.last)
    when Hash
       fields.pop
     else
      { }
    end

  if (block_given?)
    options = options.merge(:block => Proc.new)
  end

  self.dummy_definition.define_operation(self, fields, options)
end

#dummy_definitionObject

Returns a Hash which describes the dummy configuration for this Model class.



104
105
106
107
108
109
110
# File 'lib/test_dummy.rb', line 104

def dummy_definition
  @dummy_definition ||= TestDummy::Definition.new

  TestDummy::Loader.load!(self)

  @dummy_definition
end