softace / activerecord-tableless

Bringing together the different tableless model implementations into a single gem/plugin
Other
113 stars 41 forks source link

Adding in save, update_attributes methods #3

Closed brycesenz closed 11 years ago

brycesenz commented 11 years ago

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:

def save
  if self.valid?
    return true
  else
    return false
  end
end

def save!
  if self.valid?
    return self
  else
    raise StandardError, self.errors
  end
end

def reload
  return self
end

def update_attributes(attributes = {})
  if attributes
    attributes.each do |name, value|
      self.send("#{name}=", value)
      if self.valid?
        return true
      else
        return false
      end
    end
  end
end

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.

jarl-dk commented 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?

brycesenz commented 11 years ago

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).

jarl-dk commented 11 years ago

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.

brycesenz commented 11 years ago

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
jarl-dk commented 11 years ago

This is available in 1.1.0 with

 has_no_table :database => :pretend_succes
brycesenz commented 11 years ago

Great! One minor question though - is that line of code a typo, or is 'success' spelled incorrectly?

jarl-dk commented 11 years ago

Thanks. It's danish spelling, I will release a 1.1.1 right away... with english spelling (success)

jarl-dk commented 11 years ago

Now use with

 has_no_table :database => :pretend_success
brycesenz commented 11 years ago

Awesome, thanks! I have some similar issues in German, so no worries!

brycesenz commented 11 years ago

@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!

jarl-dk commented 11 years ago

Thanks for the positive feedback, very motivating...