Class: ActiveProject::Resources::Issue

Inherits:
PersistableResource show all
Defined in:
lib/active_project/resources/issue.rb

Overview

Whether it’s a Jira ticket, Trello card, GitHub Issue, or Basecamp Todo

Instance Attribute Summary

Attributes inherited from BaseResource

#adapter, #attributes, #raw_data

Instance Method Summary collapse

Methods inherited from PersistableResource

#persisted?

Methods inherited from BaseResource

def_members, #inspect, members, #method_missing, #respond_to_missing?, #to_h

Constructor Details

#initialize(adapter, attributes = {}) ⇒ Issue

Returns a new instance of Issue.



11
12
13
14
# File 'lib/active_project/resources/issue.rb', line 11

def initialize(adapter, attributes = {})
  super
  @initial_title = attributes[:title]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ActiveProject::Resources::BaseResource

Instance Method Details

#commentsObject

Lazy association proxy for comments.



77
78
79
# File 'lib/active_project/resources/issue.rb', line 77

def comments
  AssociationProxy.new(owner: self, adapter: adapter, association_name: :comments)
end

#deleteObject Also known as: destroy

Delete remote record.



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

def delete
  raise "project_id missing – can't destroy" unless project_id
  raise "id missing – record not persisted"  if id.nil?

  adapter_delete_issue(id)
  freeze
  true
end

#saveObject

Persist the record, creating it if it does not yet exist.



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
43
44
45
46
# File 'lib/active_project/resources/issue.rb', line 17

def save
  unless project_id
    raise ActiveProject::NotImplementedError,
          "#save not supported on transient records"
  end

  attrs = to_h.slice(:title, :description, :status, :assignees,
                     :reporter, :due_on, :priority)

  if @adapter.is_a?(ActiveProject::Adapters::JiraAdapter)
    if id.nil? # first persist ⇒ create_issue
      attrs.delete(:title)                  # remove :title entirely
      attrs[:summary] = @initial_title      # use the ORIGINAL title
    elsif attrs.key?(:title) # later saves ⇒ update_issue
      attrs[:summary] = attrs.delete(:title)
    end
  end

  attrs.delete(:status) unless @adapter.status_known?(project_id, attrs[:status])

  fresh =
    if id.nil?
      adapter.create_issue(project_id, attrs)
    else
      adapter_update_issue(id, attrs)
    end

  copy_from(fresh)
  true
end

#update(attributes = {}) ⇒ Object

Update attributes and persist them.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/active_project/resources/issue.rb', line 49

def update(attributes = {})
  unless project_id && id
    raise ActiveProject::NotImplementedError,
          "#update not supported on transient records"
  end
  unless attributes.is_a?(Hash)
    raise ActiveProject::NotImplementedError,
          "attributes must be a Hash"
  end

  ident = key || id
  adapter_update_issue(ident, attributes)
  copy_from(adapter_find_issue(ident))
  true
end