I have never tried using STI for lifecycles, but after looking at your lifecyles, it might be simpler to just replace the tech_status lifecyle with a "enum_string" only editable by techinicians.

This is just a suggestion without known all the internals of the app :).

Regards,
Ignacio

El 24/03/13 09:01, Bernard Rault escribió:
I am using Hobo 1.3.3 & Rails 3.0.19


I have a model device.rb for which I needed to track

  *

    a tech_status, and

  *

    an admin_status

using lifecycles.


As per Bryan’s ‘You can't have more than one Lifecycle in a model, …. you
can always create a child model to hold the second Lifecycle.’


I have tried to tackle this issue using the STI approach, and defined

  *

    the tech_status lifecycle in device.rb, and

  *

    the admin_status lifecycle in device_son.rb, as in the following code :


I am using Hobo 1.3.3 & Rails 3.0.19


I have a model device.rb for which I needed to track

  *

    a tech_status, and

  *

    an admin_status

using lifecycles.


As per Bryan’s ‘You can't have more than one Lifecycle in a model, …. you
can always create a child model to hold the second Lifecycle.’


I have tried to tackle this issue using the STI approach, and defined

  *

    the tech_status lifecycle in device.rb, and

  *

    the admin_status lifecycle in device_son.rb, as in the following code :


/class Device < ActiveRecord::Base/

/hobo_model # Don't put anything above this/

/../

/# :tech_status lifecycle/

/lifecycle :state_field => :tech_status do/

/state :for_workshop, :default => true/

/state :all_ok,/

/:scrapped,/

/:deactivated/

/transition :complete, /

/{ :for_workshop => :all_ok },/

/:available_to => :technicians do/

/# Create new tech_status_change object ; this tracks tech_status history/

/@rtsc = self.tech_status_changes.new( :new_tech_status => :all_ok,
:operator => acting_user )/

/# Save object;/

/@rtsc.save/

/end/

/transition :deactivate, /

/{ :for_workshop => :deactivated },/

/:available_to => :technicians do/

/@rtsc = self.tech_status_changes.new( :new_tech_status => :deactivated,
:operator => acting_user )/

/@rtsc.save/

/end/

/transition :scrap, { :for_workshop => :scrapped },/

/:available_to => :technicians do/

/@rtsc = self.tech_status_changes.new( :new_tech_status => :scrapped,
:operator => acting_user )/

/@rtsc.save/

/end/

/# The following :receive transitions get called programatically;/

/# For documented reasons, we lose access to acting_user in contained
models/

/# acting_user is only set on the model during a create or a /

/# save; it's available in the various validation methods, permission /

/# methods and callbacks, but not typically set outside that context/

/transition :receive, { :scrapped => :for_workshop },/

/:available_to => :all do/

/@rtsc = self.tech_status_changes.new( :new_tech_status =>
:for_workshop, :operator => acting_user )/

/@rtsc.save/

/end/

/transition :receive,/

/{ :deactivated => :for_workshop },/

/:available_to => :all do/

/@rtsc = self.tech_status_changes.new( :new_tech_status =>
:for_workshop, :operator => acting_user )/

/@rtsc.save/

/end/

/transition :receive, { :all_ok => :for_workshop },/

/:available_to => :all do/

/@rtsc = self.tech_status_changes.new( :new_tech_status =>
:for_workshop, :operator => acting_user )/

/@rtsc.save/

/end/

/end/

…

and,


/class DeviceSon < Device /

/# This is a STI Model definition/

/# --- Lifecycles --- #/


/# :admin_status lifecycle/

/lifecycle :state_field => :admin_status do/

/state :undefined, :default => true/

/state :in_stock, # Device is in stock/

/:internal_asset, # Device capitalised for internal use/

/:sold, # Device sold to regular customer/

/:demo, # Device 'demo'ed from stock; device is issued in this state/

/:available, # Device available for loan or rent; device not issued in
this state/

/:rented, # Device rented; device is issued in this state/

/:loaned # Device loaned; device is issued in this state/

/# New device => stock; callable by :accountants/

/transition :to_stock, /

/{ :undefined => :in_stock },/

/:available_to => :all do/

/@dasc = self.admin_status_changes.new( :new_admin_status => :in_stock,
:operator => acting_user )/

/@dasc.save/

/end/

/# Stock => capitalisation for internal use; callable by :accountants/

/transition :capitalise_internal, /

/{ :in_stock => :internal_asset },/

/:available_to => :all do/

/@dasc = self.admin_status_changes.new( :new_admin_status =>
:internal_asset, :operator => acting_user )/

/@dasc.save/

/end/

/# Stock => capitalisation for rental/loan use; callable by :accountants/

/transition :capitalise_for_rental, /

/{ :in_stock => :available },/

/:available_to => :all do/

/@dasc = self.admin_status_changes.new( :new_admin_status => :available,
:operator => acting_user )/

/@dasc.save/

/end/

/# Trade-in sold device for internal use; callable by :accountants/

/transition :trade_in_internal, /

/{ :sold => :internal_asset },/

/:available_to => :all do/

/@dasc = self.admin_status_changes.new( :new_admin_status =>
:internal_asset, :operator => acting_user )/

/@dasc.save/

/end/

/# Trade-in sold device for rental/loan use; callable by :accountants/

/transition :trade_in_for_rental, /

/{ :sold => :available },/

/:available_to => :all do/

/@dasc = self.admin_status_changes.new( :new_admin_status => :available,
:operator => acting_user )/

/@dasc.save/

/end/

/# Sell device from stock; callable by :accountants/

/transition :sell, /

/{ :in_stock => :sold },/

/:available_to => :all do/

/@dasc = self.admin_status_changes.new( :new_admin_status => :sold,
:operator => acting_user )/

/@dasc.save/

/end/

/# Issue device from stock for demonstration purposes; callable by
:coordinators/

/transition :issue_demo, /

/{ :in_stock => :demo },/

/:available_to => :all do/

/@dasc = self.admin_status_changes.new( :new_admin_status => :demo,
:operator => acting_user )/

/@dasc.save/

/end/

/# Issue device for rental; callable by :coordinators/

/transition :issue_rent, /

/{ :available => :rented },/

/:available_to => :all do/

/@dasc = self.admin_status_changes.new( :new_admin_status => :rented,
:operator => acting_user )/

/@dasc.save/

/end/

/# Issue device for loan; callable by :coordinators/

/transition :issue_loan, /

/{ :available => :loaned },/

/:available_to => :all do/

/@dasc = self.admin_status_changes.new( :new_admin_status => :loaned,
:operator => acting_user )/

/@dasc.save/

/end/

/# Receive device from loan; callable by :coordinators/

/transition :return, /

/{ :loaned => :available },/

/:available_to => :all do/

/@dasc = self.admin_status_changes.new( :new_admin_status => :available,
:operator => acting_user )/

/@dasc.save/

/end/

/# Receive device from rent; callable by :coordinators/

/transition :return, /

/{ :rented => :available },/

/:available_to => :all do/

/@dasc = self.admin_status_changes.new( :new_admin_status => :available,
:operator => acting_user )/

/@dasc.save/

/end/

/# Return demo device back to stock; callable by :coordinators/

/transition :return, /

/{ :demo => :in_stock },/

/:available_to => :all do/

/@dasc = self.admin_status_changes.new( :new_admin_status => :in_stock,
:operator => acting_user )/

/@dasc.save/

/end/

/end/

…


The above migrates properly, the lifecycle state field is created in the
parent model but I cannot call a transition in the admin_status lifecycle !!

In fact, in the console(using an instance variable my_dev_son, of class
DevSon) :


my_dev_son.lifecycle.class # => Device::Lifecycle,


I was expecting DeviceSon::Lifecycle.


I got this to work using a rather inelegant :has_one => :device_son &
belongs_to approach, but this uses two tables of course; it just does
not feel right !!!

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



--
Ignacio Huerta Arteche
http://www.ihuerta.net
Teléfono: 0034 645 70 77 35
Email realizado con software libre

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


Reply via email to