Module: Collavre::Comment::Approvable

Extended by:
ActiveSupport::Concern
Included in:
Collavre::Comment
Defined in:
app/models/collavre/comment/approvable.rb

Instance Method Summary collapse

Instance Method Details

#approval_status(user) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/collavre/comment/approvable.rb', line 10

def approval_status(user)
  return :not_allowed unless user

  if action.blank?
    return :not_allowed unless approver_id == user&.id
    return :missing_action
  end

  begin
    payload = JSON.parse(action)
  rescue JSON::ParserError
    return :invalid_action_format
  end
  return :invalid_action_format unless payload.is_a?(Hash)

  actions = Array(payload["actions"])
  actions = [ payload ] if actions.empty?

  requires_admin = actions.any? do |item|
    next false unless item.is_a?(Hash)
    action_type = item["action"] || item["type"]
    action_type == "approve_tool"
  end

  if requires_admin && SystemSetting.mcp_tool_approval_required?
    return user.system_admin? ? :ok : :admin_required
  end

  return :missing_approver if approver_id.blank?
  return :not_allowed unless approver_id == user&.id

  :ok
end

#can_be_approved_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'app/models/collavre/comment/approvable.rb', line 6

def can_be_approved_by?(user)
  approval_status(user) == :ok
end

#parsed_action_tool_nameObject



44
45
46
47
# File 'app/models/collavre/comment/approvable.rb', line 44

def parsed_action_tool_name
  parsed = JSON.parse(action) rescue nil
  parsed&.dig("tool_name") || "unknown"
end