Closed brycesenz closed 11 years ago
All these methods only makes sense if you have a persistency backend (like a DB), so for this gem the functionality behind them does not make sense. But I will consider introduce an option to has_no_table
so it is possible to silence such DB calls. I will raise exceptions in default case and with an option like :fake_storage_success => true
I can return. Do you have a better name? or proposal?
I'm not sure that I agree - a lot of gems have an underlying assumption that these methods will exist for any model, so in many cases it makes sense to at least have the methods available to avoid unwanted errors. As I mentioned above, having these methods (and understanding that there is no persistency) allows us to use FactoryGirl methods among others.
I do think that it makes sense to have the methods available as an extra option as you suggested. Also, this might explain the "accepts_nested_attributes" errors that were described in another issue, in the event that a save! was being called on a DB-backed model that had an association with a tableless model (but that's just a hypothesis).
Thanks for input.
I expect to implement it like
class MyModel < ActiveRecord
has_no_table :fake_storage => :succeed
end
Other alternatives are:
has_no_table :fake_storage => :fail
and
has_no_table :fake_storage => :raise
The default will be :raise (as now but implemented a more clean way)
So using
has_no_table :fake_storage => :succeed
should give what you are requesting.
That makes a lot of sense. Also, so you know, the 'saves' method needs to accept parameters as I found out after a few tests. So my new implementation is:
def save(validate = true)
if (validate) && (self.valid?)
return true
elsif (validate) && !(self.valid?)
return false
else
return true
end
end
This is available in 1.1.0 with
has_no_table :database => :pretend_succes
Great! One minor question though - is that line of code a typo, or is 'success' spelled incorrectly?
Thanks. It's danish spelling, I will release a 1.1.1 right away... with english spelling (success)
Now use with
has_no_table :database => :pretend_success
Awesome, thanks! I have some similar issues in German, so no worries!
@jarl-dk - I finally got a chance to test out this feature today. I have to say, the combination of :database => :pretend_success
with FactoryGirl and Shoulda-Matchers is absolutely incredible. It's just like testing a database backed model, so all of my custom shared_examples, etc. work seamlessly. Thanks again!
Thanks for the positive feedback, very motivating...
First, thanks for the great gem; it saved me a ton of time in having to implement this myself. That said, it would be great to be able to use FactoryGirl and some of the other testing gems along with the tableless models for consistency in my test suite.
I was able to do this, but I had to add the following methods to my model to handle any save-related calls:
Would there be anything wrong with adding those to the gem itself? I imagine that I'm not the only one who would benefit from having these methods implemented.