I have a Project model, which has a status property. Status can either
be 0, 1, 2, or 3 - and complies with the array Project.statuses = [
:hidden, :available, :in_progress, :complete ]. This works well in all
of my application, tested and all. (Project.statuses is only used
superficially at this point, and I added Project#complete?,
Project#in_progress?, etc. - automatically - this works too)
Now, in my unit tests, I often had to set the status of the project
manually. Now, I could do this in many different ways, but I’ve set my
eyes on this one:
some_project.status = :complete
To get this to work, what I did was (in project.rb)
if (new_status.is_a? Symbol) || (new_status.is_a? String)
new_status = Project.status_id_for new_status
Project.status_id_for being a proper lookup (with some aliases, for
example :assigned is same as :in_progress). This works perfectly within
the testing code, and from the console.
However, this code breaks updating using the controller - the
mostly-scaffold code calls @project.update_attributes(params[:project])
(params[:project][‘status’] is set by the form to the proper numeric
value), but that, for some reason, sets @project.status to nil (which is
invalid, and I test for with validates_presence_of :status).
Now, removing my special status= method brings things back to normal,
and I could obviously do without this, but… I mean, come on, that’s
just beautiful. Any chance of this working without an ugly hack?