Module: ActsAsApprovable::Ownership::ClassMethods

Defined in:
lib/acts_as_approvable/ownership.rb

Overview

Class methods for approval ownership.

Instance Method Summary collapse

Instance Method Details

#assigned_ownersObject

A list of owners that have assigned approvals.

This method can be overriden by the configured #owner_source.



114
115
116
# File 'lib/acts_as_approvable/ownership.rb', line 114

def assigned_owners
  with_owner_source(:assigned_owners) { all(:select => 'DISTINCT(owner_id)', :conditions => 'owner_id IS NOT NULL', :include => :owner).map(&:owner) }
end

#available_ownersObject

A list of records that can be assigned to an approval.

This method can be overriden by the configured #owner_source.



95
96
97
# File 'lib/acts_as_approvable/ownership.rb', line 95

def available_owners
  with_owner_source(:available_owners) { owner_class.all }
end

#option_for_owner(owner) ⇒ Array

Helper method that takes an owner record and returns an array for Rails’ ‘#options_for_select`.

This method can be overriden by the configured #owner_source.

Returns:

  • (Array)

    a 2-index array with a display string and value.



136
137
138
# File 'lib/acts_as_approvable/ownership.rb', line 136

def option_for_owner(owner)
  with_owner_source(:option_for_owner, owner) { [owner.to_str, owner.id] }
end

#options_for_assigned_owners(with_prompt = false) ⇒ Array

Build an array from #assigned_owners usable by Rails’ ‘#options_for_select`. Each element in the array is built with #option_for_owner.

Returns:

  • (Array)


123
124
125
126
127
# File 'lib/acts_as_approvable/ownership.rb', line 123

def options_for_assigned_owners(with_prompt = false)
  owners = assigned_owners.map { |owner| option_for_owner(owner) }
  owners.unshift(['All Users', nil]) if with_prompt
  owners
end

#options_for_available_owners(with_prompt = false) ⇒ Array

Build an array from #available_owners usable by Rails’ ‘#options_for_select`. Each element in the array is built with #option_for_owner.

Returns:

  • (Array)


104
105
106
107
108
# File 'lib/acts_as_approvable/ownership.rb', line 104

def options_for_available_owners(with_prompt = false)
  owners = available_owners.map { |owner| option_for_owner(owner) }
  owners.unshift(['(none)', nil]) if with_prompt
  owners
end

#owner_classObject

Get the model that represents an owner.



68
69
70
# File 'lib/acts_as_approvable/ownership.rb', line 68

def owner_class
  ActsAsApprovable.owner_class
end

#owner_sourceObject

Source class used to override Owner retrieval methods



76
77
78
# File 'lib/acts_as_approvable/ownership.rb', line 76

def owner_source
  ActsAsApprovable.owner_source
end

#with_owner_source(method, *args) ⇒ Object

Attempt to run a method on the configured #owner_source class. If it does not exist yield to the given block.



83
84
85
86
87
88
89
# File 'lib/acts_as_approvable/ownership.rb', line 83

def with_owner_source(method, *args)
  if owner_source && owner_source.singleton_class.method_defined?(method)
    owner_source.send(method, *args)
  else
    yield
  end
end