On Oct 20, 2009, at 1:28 PM, dd wrote:

> So there is no constant that returns any single state. I solved it  
> by writing a little initializer file:
>
>   class Hobo::Lifecycles::DeclarationDSL
>
>     def state_with_constants *args, &block
>       state_without_constants *args, &block
>       args.extract_options!
>       args.each { |name| @lifecycle.class_eval "#{name.to_s.upcase}  
> = '#{name}'" }
>     end
>
>     alias_method_chain :state, :constants
>   end
>
> So now I can use any defined state a la HoboFields, to including it  
> in queries without any possible typo
>
> MyClass::Lifecycle::ANYSTATE
>
> Since the relavant part is just a simple line of code which make it  
> also consistent with HoboFields
>
> @lifecycle.class_eval "#{name.to_s.upcase} = '#{name}'"
>
> you might consider to include it in your code.
>

I'm not clear on what the use case for this is. I can't think of many  
cases where you'd actually need to specify a lifecycle state  
explicitly like this; most of them are better covered by features  
already present.

- Finding records in a specific state - use an automatic scope:
Model.whatever_the_state_name_is.find(:all)

- Figuring out if a particular record is in a state:
@record.lifecycle.state_name_state?
[that could probably use some polish]

- Changing a record to a new state: this should really be a  
transition. Barring importing data, if you're whacking state values  
into a lifecycle something's missing from your model.

Not trying to attack anybody, but what's the need for those constants?

Also note that things are allowed in :symbols and state fields that  
don't make valid constants; the code above will die messily when that  
happens. I'll grant that *I* can't think of a good reason to name a  
state :01_foo=bar, or to do silly things with two states named :foo  
and :f0o, but somebody might...

--Matt Jones


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Hobo 
Users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/hobousers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to