Module: DcidevApproval::InstanceMethods

Defined in:
lib/dcidev_approval.rb

Instance Method Summary collapse

Instance Method Details

#approval(params) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



107
108
109
110
111
112
113
114
115
# File 'lib/dcidev_approval.rb', line 107

def approval(params)
    case params.status
    when 'approved'
        approve_changes
    when 'rejected'
        delete_changes
    end
    yield self
end

#approve_changesObject



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dcidev_approval.rb', line 75

def approve_changes
    if change_status.nil? && %w[waiting rejected].include?(status) && !update(status: :approved, data_changes: nil, change_status: nil)
        raise errors.full_messages.join(', ')
    end
    
    case change_status
    when 'pending_update'
        raise errors.full_messages.join(', ') unless update_by_params(data_changes, false)
        raise errors.full_messages.join(', ') unless update(status: :approved, data_changes: nil, change_status: nil)
    when 'pending_delete'
        destroy
    end
end

#approved?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/dcidev_approval.rb', line 38

def approved?
    status == 'approved' || change_status.nil?
end

#changes_present?(changes) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
20
# File 'lib/dcidev_approval.rb', line 10

def changes_present?(changes)
    present = false
    changes.each do |k, v|
        if eval("self.#{k}") != v
            present = true
            break
        end
    rescue StandardError => _e
    end
    present
end

#created_byObject



58
59
60
61
62
63
64
# File 'lib/dcidev_approval.rb', line 58

def created_by
    log = audit_trails(:asc).where(activity_type: :create).limit(1).try(:first)
    {
        created_by: log.present? && log.try(:agent).try(:name).present? ? log.try(:agent).try(:name).to_s + " (#{log.try(:agent_role).try(:name)})" : 'System',
        created_at: self.try(:created_at) || log.try(:created_at)
    }
end

#delete_changesObject



89
90
91
# File 'lib/dcidev_approval.rb', line 89

def delete_changes
    raise errors.full_messages.join(', ') unless update(data_changes: nil, change_status: nil, status: status == 'waiting' ? :rejected : :approved)
end

#delete_data(agent, bypass = true) {|true| ... } ⇒ Object

Yields:

  • (true)


117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/dcidev_approval.rb', line 117

def delete_data(agent, bypass = true)
    raise 'data still waiting for approval' if waiting_approval?
    
    if bypass || agent.is_admin?
        ActiveRecord::Base.transaction do
            raise errors.full_messages.join(', ') unless destroy
        end
    else
        raise errors.full_messages.join(', ') unless update(change_status: :pending_delete)
    end
    yield true
end

#edit_data(params, agent, bypass = true) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/dcidev_approval.rb', line 93

def edit_data(params, agent, bypass = true)
    raise 'data still waiting for approval' if waiting_approval?
    
    if bypass
        raise errors.full_messages.join(', ') unless update_by_params(params, false)
    elsif changes_present?(params)
        ActiveRecord::Base.transaction do
            data = agent.is_admin? || status == 'waiting' ? params :{ change_status: :pending_update, data_changes: agent.is_admin? ? nil : params }
            raise errors.full_messages.join(', ') unless update_by_params(data, false)
        end
    end
    yield self
end

#last_approved_byObject



66
67
68
69
70
71
72
73
# File 'lib/dcidev_approval.rb', line 66

def last_approved_by
    last_approve = audit_trails.where(approval: true).limit(1).try(:first)
    last_entry = audit_trails.limit(1).try(:first)
    {
        approved_by: last_approve.try(:id) == last_entry.try(:id) ? last_approve.try(:agent).try(:name).to_s + " (#{last_approve.try(:agent_role).try(:name)})" : nil,
        approved_at: last_approve.try(:id) == last_entry.try(:id) ? last_approve.try(:created_at) : nil
    }
end

#last_modified_byObject



50
51
52
53
54
55
56
# File 'lib/dcidev_approval.rb', line 50

def last_modified_by
    log = audit_trails.where(activity_type: %w[update delete]).limit(1).try(:first)
    {
        modified_by: log.present? ? log.try(:agent).try(:name).to_s + " (#{log.try(:agent).try(:roles).try(:first).try(:name)})" : nil,
        modified_at: log.present? ? log.try(:created_at) || try(:updated_at) || try(:created_at) : nil
    }
end

#pending_delete?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/dcidev_approval.rb', line 34

def pending_delete?
    change_status == 'pending_delete'
end

#pending_insert?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/dcidev_approval.rb', line 26

def pending_insert?
    change_status.nil? && %w[waiting rejected].include?(status)
end

#pending_update?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/dcidev_approval.rb', line 30

def pending_update?
    change_status == 'pending_update'
end

#rejected?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/dcidev_approval.rb', line 42

def rejected?
    status == 'rejected'
end

#waiting?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/dcidev_approval.rb', line 46

def waiting?
    status == 'waiting'
end

#waiting_approval?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/dcidev_approval.rb', line 22

def waiting_approval?
    %w[pending_update pending_delete].include?(change_status) || status == 'waiting'
end