Module: ActsAsStatused::CanCan

Defined in:
app/models/concerns/acts_as_statused.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_statused(klass, only: nil, except: nil) ⇒ Object

The idea here is you can go forward but you can’t go back.



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
58
59
60
61
62
63
64
65
# File 'app/models/concerns/acts_as_statused.rb', line 29

def acts_as_statused(klass, only: nil, except: nil)
  raise "klass does not implement acts_as_statused" unless klass.acts_as_statused?

  statuses = klass.const_get(:STATUSES)
  instance = klass.new

  only = Array(only).compact
  except = Array(except).compact

  statuses.each_with_index do |status, index|
    action = status_active_verb(status, instance)

    next if action.blank?
    next if only.present? && !only.include?(action)
    next if except.present? && except.include?(action)

    if index == 0
      can(action, klass) and next
    end

    if status == :approved && statuses.include?(:declined)
      if (position = statuses.index { |status| (status == :approved || status == :declined) }) > 0
        can(action, klass) { |obj| obj.public_send("#{statuses[position-1]}?") || obj.declined? }
        next
      end
    end

    if status == :declined && statuses.include?(:approved)
      if (position = statuses.index { |status| (status == :approved || status == :declined) }) > 0
        can(action, klass) { |obj| obj.public_send("#{statuses[position-1]}?") }
        next
      end
    end

    can(action, klass) { |obj| obj.public_send("#{statuses[index-1]}?") }
  end
end