Module: TestUnitExt::EasyRecordInsertion
- Included in:
- TestUnitExt
- Defined in:
- lib/test_unit_ext/easy_record_insertion.rb
Overview
Details in the introduction article.
Instance Method Summary collapse
-
#insert!(model, attributes = {}, options = {}) ⇒ Object
Usage examples:.
Instance Method Details
#insert!(model, attributes = {}, options = {}) ⇒ Object
Usage examples:
insert! Host, :ip_address => '192.168.0.1'
insert! Host, {:hostname => 'foo'}, :trigger_validation => true
insert! Host, {}, :inhibit_callbacks => false
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/test_unit_ext/easy_record_insertion.rb', line 9 def insert!(model, attributes={}, ={}) attributes = attributes.stringify_keys trigger_validation = .fetch(:trigger_validation, false) inhibit_callbacks = .fetch(:inhibit_callbacks, !trigger_validation) begin record = model.new { |record| record.send(:attributes=, attributes, false) } if inhibit_callbacks def record.callback(*args) end end if trigger_validation record.valid? end record.save(false) rescue ActiveRecord::StatementInvalid if $!. =~ /Column '(.+?)' cannot be null/ unless attributes.key?($1) attributes[$1] = record.column_for_attribute($1).number? ? 0 : "-" retry end end raise end return record end |