Module: StatusManager::StatusGroupManager

Defined in:
lib/status-manager/status_group_manager.rb

Instance Method Summary collapse

Instance Method Details

#status_group(status_attribute_name, group_status_set = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/status-manager/status_group_manager.rb', line 3

def status_group(status_attribute_name, group_status_set = {})
  status_store = self.status_store_list.get(status_attribute_name.to_sym)
  raise "undefined #{status_attribute_name}" unless status_store
  
  group_status_set.each do |group_status_name, group_statuses|
    raise "#{status_attribute_name}-#{group_status_name} is not a group, group must have statuses" if group_statuses.size < 1
    status_store.add_group_status(group_status_name, group_statuses)

    # set scope
    scope "#{status_store.attribute_name}_#{group_status_name}", -> {
      where("#{self.table_name}.#{status_store.attribute_name} in (?)", status_store.get_group_status(group_status_name).values)
    }

    # status check method
    define_method "#{status_attribute_name}_#{group_status_name}?" do 
      status_store.get_group_status(group_status_name).values.include? self.send(status_attribute_name)
    end

    define_method "#{status_attribute_name}_was_#{group_status_name}?" do
      status_store.get_group_status(group_status_name).values.include? self.send("#{status_attribute_name}_was")
    end
  end
end