Class: Wf::Workitem

Inherits:
ApplicationRecord show all
Defined in:
app/models/wf/workitem.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.doing(wf_current_user) ⇒ Object



59
60
61
# File 'app/models/wf/workitem.rb', line 59

def self.doing(wf_current_user)
  where(holding_user: wf_current_user).where(state: %i[started enabled])
end

.done(wf_current_user) ⇒ Object



63
64
65
# File 'app/models/wf/workitem.rb', line 63

def self.done(wf_current_user)
  where(holding_user: wf_current_user).where(state: [:finished])
end

.todo(wf_current_user) ⇒ Object



51
52
53
54
55
56
57
# File 'app/models/wf/workitem.rb', line 51

def self.todo(wf_current_user)
  current_party_ids = [
    wf_current_user,
    Wf.org_classes.map { |org, _org_class| wf_current_user&.public_send(org) }
  ].flatten.map { |x| x&.party&.id }.compact
  Wf::Workitem.where(forked: false).joins(:workitem_assignments).where(Wf::WorkitemAssignment.table_name => { party_id: current_party_ids })
end

Instance Method Details

#finished_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# File 'app/models/wf/workitem.rb', line 108

def finished_by?(user)
  real? && started? && owned_by?(user) && holding_user == user
end

#for_mini_racerObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/models/wf/workitem.rb', line 67

def for_mini_racer
  attr = attributes
  attr = attr.merge(holding_user: holding_user&.attributes || {})
  attr = attr.merge(form: entries.to_a.first&.for_mini_racer || {})
  children_attrs = if forked?
    []
  else
    children.includes(:holding_user, entries: :field_values).map(&:for_mini_racer)
  end
  attr = attr.merge(children: children_attrs)
  attr
end

#nameObject



84
85
86
# File 'app/models/wf/workitem.rb', line 84

def name
  "Workitem -> #{id}"
end

#owned_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
118
119
120
# File 'app/models/wf/workitem.rb', line 112

def owned_by?(user)
  Wf::Party.joins(workitem_assignments: { workitem: %i[transition case] })
           .where(Wf::Transition.table_name => { trigger_type: Wf::Transition.trigger_types[:user] })
           .where(Wf::Case.table_name => { state: Wf::Case.states[:active] })
           .where(Wf::Workitem.table_name => { state: Wf::Workitem.states.values_at(:started, :enabled) })
           .where(Wf::Workitem.table_name => { id: id }).map do |party|
    party.partable.users.to_a
  end.flatten.include?(user)
end

#parent?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'app/models/wf/workitem.rb', line 80

def parent?
  !forked
end

#pass_guard?(arc, has_passed = false) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
# File 'app/models/wf/workitem.rb', line 88

def pass_guard?(arc, has_passed = false)
  if arc.guards_count == 0
    !has_passed
  else
    entry = entries.where(user: holding_user).first
    arc.guards.all? { |guard| guard.pass?(entry, self) }
  end
end

#real?Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
# File 'app/models/wf/workitem.rb', line 97

def real?
  return false if transition.multiple_instance? && parent_id.nil?
  return false if transition.sub_workflow_id.present?

  true
end

#started_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'app/models/wf/workitem.rb', line 104

def started_by?(user)
  real? && enabled? && owned_by?(user)
end