Casecommons / with_model

Dynamically build an Active Record model (with table) within a test context
http://www.casebook.net
MIT License
164 stars 18 forks source link

Callbacks? #23

Closed mockdeep closed 8 years ago

mockdeep commented 8 years ago

I'm trying to write a dummy model to test that callbacks are used properly, but with_model throws an error:

NoMethodError:
  undefined method `_run_before_save_callbacks' for #<Dummy id: nil, created_at: nil, updated_at: nil>
# /home/fletch/.rvm/gems/ruby-2.1.7/gems/activemodel-4.2.4/lib/active_model/attribute_methods.rb:433:in `method_missing'
# /home/fletch/.rvm/gems/ruby-2.1.7/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:81:in `run_callbacks'

I don't know if it's digging too deep into re-implementing the underlying details of ActiveRecord, but it would be nice to be able to use with_model to test callbacks as well.

mockdeep commented 8 years ago

Oops, I'm conflating a couple of things. That error is a known issue with active record.

But the other piece still stands, since I can't add before_save callbacks into with_model.

andrew-cc commented 8 years ago

Please provide minimal failing example code.

On Wednesday, September 30, 2015, Robert Fletcher notifications@github.com wrote:

Oops, I'm conflating a couple of things. That error is a known issue https://github.com/rails/rails/issues/718 with active record.

But the other piece still stands, since I can't add before_save callbacks into with_model.

— Reply to this email directly or view it on GitHub https://github.com/Casecommons/with_model/issues/23#issuecomment-144511533 .

(Sent via iPhone)

mockdeep commented 8 years ago
with_model :Dummy do
  after_save -> { puts 'whatevs' }
  table(&:timestamps)
end

results in:

undefined method `after_save' for #<WithModel::Model::DSL:0x0000000a6a7728> (NoMethodError)
amarshall commented 8 years ago

Did you mean to do:

with_model :Dummy do
  model do
    after_save -> { puts 'whatevs' }
  end
  table(&:timestamps)
end

Also the error with the code you give is different than your original error (though you did mention that that particular error was the result of an ActiveRecord bug, not the issue with with_model).

mockdeep commented 8 years ago

Ah, you are correct. My mistake.