module Approvable::ActsAsApprovable::LocalInstanceMethods

Public Instance Methods

apply_changes() click to toggle source
# File lib/approvable/acts_as_approvable.rb, line 54
def apply_changes
  self.assign_attributes_without_change_request requested_changes
  self
end
approve_changes() click to toggle source
# File lib/approvable/acts_as_approvable.rb, line 73
def approve_changes
  transaction do
    current_change_request.approve! if current_change_request
    apply_changes.save
    reload
  end
end
assign_attributes_with_change_request(new_attributes) click to toggle source
# File lib/approvable/acts_as_approvable.rb, line 86
def assign_attributes_with_change_request new_attributes
  ignored_params = ignored_attributes(new_attributes)
  approvable_params = approvable_attributes(new_attributes)

  if approvable_params.any?
    current_change_request || build_current_change_request(requested_changes: {})       
    current_change_request.requested_changes = requested_changes.merge approvable_params
  end

  assign_attributes_without_change_request ignored_params
end
change_status() click to toggle source

use current_change_request here so that we dont get pending from a newly built change_request

# File lib/approvable/acts_as_approvable.rb, line 46
def change_status
  current_change_request ? current_change_request.state : 'approved'
end
change_status_notes() click to toggle source
# File lib/approvable/acts_as_approvable.rb, line 50
def change_status_notes
  current_change_request ? current_change_request.notes : {}
end
reject_changes(options = {}) click to toggle source
# File lib/approvable/acts_as_approvable.rb, line 81
def reject_changes options = {}
  current_change_request.reject! :rejected, options if current_change_request
  reload
end
requested_changes() click to toggle source
# File lib/approvable/acts_as_approvable.rb, line 41
def requested_changes
  current_change_request ? current_change_request.requested_changes.with_indifferent_access : {}
end
submit_changes() click to toggle source
# File lib/approvable/acts_as_approvable.rb, line 59
def submit_changes
  transaction do
    current_change_request.submit! if current_change_request
    reload
  end 
end
unsubmit_changes() click to toggle source
# File lib/approvable/acts_as_approvable.rb, line 66
def unsubmit_changes
  transaction do
    current_change_request.unsubmit! if current_change_request
    reload
  end
end

Private Instance Methods

approvable_attributes(new_attributes) click to toggle source
# File lib/approvable/acts_as_approvable.rb, line 104
def approvable_attributes(new_attributes)
  process_nested_hash(new_attributes, self.class.filter_attrs, filter_type == :only )
end
auto_approve?() click to toggle source

process_nested_hash h, [:first_name, {address: :street}], true process_nested_hash h, [:first_name, {address: :street}], false

# File lib/approvable/acts_as_approvable.rb, line 138
def auto_approve?
  Approvable.auto_approve == true
end
force_approve!() click to toggle source
# File lib/approvable/acts_as_approvable.rb, line 142
def force_approve!
  current_change_request.update_column :state, 'approved' if current_change_request
end
ignored_attributes(new_attributes) click to toggle source
# File lib/approvable/acts_as_approvable.rb, line 100
def ignored_attributes(new_attributes)
  process_nested_hash(new_attributes, self.class.filter_attrs, filter_type == :except)
end
process_nested_hash(attributes, keys, should_match) click to toggle source

h = {“first_name”=>“Leif”, “last_name”=>“Gensert”, “address”=>{“street”=>“Preysinstraße”, “city”=>“München”}}

# File lib/approvable/acts_as_approvable.rb, line 109
def process_nested_hash(attributes, keys, should_match)
  attributes = attributes.dup.stringify_keys!
  hash = {}
  [*keys].each do |key|
    if key.is_a? Hash
      key.each do |k,v| 
        hash[k.to_s] = process_nested_hash(attributes[k.to_s], v, should_match) if attributes[k.to_s]
      end
    elsif key.is_a? Array
      key.each do|k|
        process_nested_hash(attributes[k.to_s], k, should_match) if attributes[k.to_s]
      end
    else
      value = attributes.delete(key.to_s)  
      hash[key.to_s] = value if value
    end
  end
  
  if should_match
    return hash
  else
    return attributes
  end
  
end