Module: KeteTrackableItems::TrackableItem::ClassMethods

Defined in:
lib/kete_trackable_items/trackable_item.rb

Instance Method Summary collapse

Instance Method Details

#set_up_as_trackable_item(*args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/kete_trackable_items/trackable_item.rb', line 10

def set_up_as_trackable_item(*args)
  options = args.last.is_a?(Hash) ? args.pop : Hash.new

  # don't allow multiple calls
  return if self.included_modules.include?(KeteTrackableItems::TrackableItem::InstanceMethods)

  send :include, KeteTrackableItems::TrackableItem::InstanceMethods

  # associations to support allocating a trackable_item to a shelf_location or shelf_locations
  send :has_many, :trackable_item_shelf_locations, :as => :trackable_item, :dependent => :delete_all
  send :has_many, :shelf_locations, :through => :trackable_item_shelf_locations, :conditions => "trackable_item_shelf_locations.workflow_state = 'active'"
  # associations to support adding a trackable_item to a tracking_list
  send :has_many, :tracked_items, :as => :trackable_item, :dependent => :delete_all
  send :has_many, :tracking_lists, :through => :tracked_items

  # when a trackable_item is in 'on_loan' state, which on_loan_organization is it on loan to?
  send :belongs_to, :on_loan_organization

  self.non_versioned_columns << "on_loan_organization_id"

  cattr_accessor :described_as_in_tracking_list
  self.described_as_in_tracking_list = options[:described_as] || :title

  # set up our states and events through workflow gem
  send :include, KeteTrackableItems::WorkflowUtilities
    
  class_eval do
    shared_code_as_string = shared_tracking_workflow_specs_as_string

    specification = Proc.new {
      state :unallocated do
        event :allocate, :transitions_to => :on_shelf
      end

      eval(shared_code_as_string)

      state :to_be_refiled do
        event :refile, :transitions_to => :on_shelf
      end
    }

    workflow(&specification)

    set_up_workflow_named_scopes.call
  end

  self.non_versioned_columns << "workflow_state"
end