Class: Trello::Card

Inherits:
BasicData show all
Includes:
HasActions
Defined in:
lib/trello/card.rb

Overview

A Card is a container that can house checklists and comments; it resides inside a List.

Instance Attribute Summary

Attributes inherited from BasicData

#client

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasActions

#actions

Methods inherited from BasicData

#==, client, #initialize, many, one, parse, parse_many, path_name, #refresh!, register_attributes, save

Constructor Details

This class inherits a constructor from Trello::BasicData

Class Method Details

.create(options) ⇒ Object

Create a new card and save it on Trello.



19
20
21
22
23
24
# File 'lib/trello/card.rb', line 19

def create(options)
  client.create(:card,
      'name' => options[:name],
      'idList' => options[:list_id],
      'desc'   => options[:desc])
end

.find(id, params = {}) ⇒ Object

Find a specific card by its id.



14
15
16
# File 'lib/trello/card.rb', line 14

def find(id, params = {})
  client.find(:card, id, params)
end

Instance Method Details

#add_attachment(attachment, name = '') ⇒ Object

Add an attachment to this card



190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/trello/card.rb', line 190

def add_attachment(attachment, name='')
  if attachment.is_a? File
    client.post("/cards/#{id}/attachments", {
        :file => attachment,
        :name => name
      })
  else
    client.post("/cards/#{id}/attachments", {
        :url => attachment,
        :name => name
      })
  end
end

#add_checklist(checklist) ⇒ Object

Add a checklist to this card



129
130
131
132
133
# File 'lib/trello/card.rb', line 129

def add_checklist(checklist)
  client.post("/cards/#{id}/checklists", {
    :value => checklist.id
  })
end

#add_comment(text) ⇒ Object

Add a comment with the supplied text.



124
125
126
# File 'lib/trello/card.rb', line 124

def add_comment(text)
  client.post("/cards/#{id}/actions/comments", :text => text)
end

#add_label(colour) ⇒ Object

Add a label



172
173
174
175
176
177
178
# File 'lib/trello/card.rb', line 172

def add_label(colour)
  unless %w{green yellow orange red purple blue}.include? colour
    errors.add(:label, "colour '#{colour}' does not exist")
    return Trello.logger.warn "The label colour '#{colour}' does not exist."
  end
  client.post("/cards/#{id}/labels", { :value => colour })
end

#add_member(member) ⇒ Object

Add a member to this card



154
155
156
157
158
# File 'lib/trello/card.rb', line 154

def add_member(member)
  client.post("/cards/#{id}/members", {
    :value => member.id
  })
end

#attachmentsObject

Retrieve a list of attachments



205
206
207
208
# File 'lib/trello/card.rb', line 205

def attachments
  attachments = client.get("/cards/#{id}/attachments").json_into(Attachment)
  MultiAssociation.new(self, attachments).proxy
end

#check_item_statesObject



57
58
59
60
# File 'lib/trello/card.rb', line 57

def check_item_states
  states = client.get("/cards/#{self.id}/checkItemStates").json_into(CheckItemState)
  MultiAssociation.new(self, states).proxy
end

#closeObject



109
110
111
# File 'lib/trello/card.rb', line 109

def close
  self.closed = true
end

#close!Object



113
114
115
116
# File 'lib/trello/card.rb', line 113

def close!
  close
  save
end

#closed?Boolean

Check if the card is not active anymore.

Returns:

  • (Boolean)


105
106
107
# File 'lib/trello/card.rb', line 105

def closed?
  closed
end

#deleteObject

Delete this card



100
101
102
# File 'lib/trello/card.rb', line 100

def delete
  client.delete("/cards/#{id}")
end

#labelsObject

Retrieve a list of labels



166
167
168
169
# File 'lib/trello/card.rb', line 166

def labels
  labels = client.get("/cards/#{id}/labels").json_into(Label)
  MultiAssociation.new(self, labels).proxy
end

#membersObject

Returns a list of members who are assigned to this card.



67
68
69
70
71
72
# File 'lib/trello/card.rb', line 67

def members
  members = member_ids.map do |member_id|
    client.get("/members/#{member_id}").json_into(Member)
  end
  MultiAssociation.new(self, members).proxy
end

#move_to_board(new_board, new_list = nil) ⇒ Object

Move this card to the given board (and optional list on this board)



145
146
147
148
149
150
151
# File 'lib/trello/card.rb', line 145

def move_to_board(new_board, new_list = nil)
  unless board_id == new_board.id
    payload = { :value => new_board.id }
    payload[:idList] = new_list.id if new_list
    client.put("/cards/#{id}/idBoard", payload)
  end
end

#move_to_list(list) ⇒ Object

Move this card to the given list



136
137
138
139
140
141
142
# File 'lib/trello/card.rb', line 136

def move_to_list(list)
  unless list_id == list.id
    client.put("/cards/#{id}/idList", {
      :value => list.id
    })
  end
end

#remove_attachment(attachment) ⇒ Object

Remove an attachment from this card



211
212
213
# File 'lib/trello/card.rb', line 211

def remove_attachment(attachment)
  client.delete("/cards/#{id}/attachments/#{attachment.id}")
end

#remove_label(colour) ⇒ Object

Remove a label



181
182
183
184
185
186
187
# File 'lib/trello/card.rb', line 181

def remove_label(colour)
  unless %w{green yellow orange red purple blue}.include? colour
    errors.add(:label, "colour '#{colour}' does not exist")
    return Trello.logger.warn "The label colour '#{colour}' does not exist." unless %w{green yellow orange red purple blue}.include? colour
  end
  client.delete("/cards/#{id}/labels/#{colour}")
end

#remove_member(member) ⇒ Object

Remove a member from this card



161
162
163
# File 'lib/trello/card.rb', line 161

def remove_member(member)
  client.delete("/cards/#{id}/members/#{member.id}")
end

#request_prefixObject

:nodoc:



216
217
218
# File 'lib/trello/card.rb', line 216

def request_prefix
  "/cards/#{id}"
end

#saveObject

Saves a record.



75
76
77
78
79
80
81
82
83
84
# File 'lib/trello/card.rb', line 75

def save
  # If we have an id, just update our fields.
  return update! if id

  client.post("/cards", {
    :name   => name,
    :desc   => desc,
    :idList => list_id
  }).json_into(self)
end

#update!Object

Update an existing record. Warning, this updates all fields using values already in memory. If an external resource has updated these fields, you should refresh! this object before making your changes, and before updating the record.



90
91
92
93
94
95
96
97
# File 'lib/trello/card.rb', line 90

def update!
  @previously_changed = changes
  # extract only new values to build payload
  payload = Hash[changes.map { |key, values| [key.to_sym, values[1]] }]
  @changed_attributes.clear

  client.put("/cards/#{id}", payload)
end

#update_fields(fields) ⇒ Object

Update the fields of a card.

Supply a hash of string keyed data retrieved from the Trello API representing a card.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/trello/card.rb', line 31

def update_fields(fields)
  attributes[:id]                 = fields['id']
  attributes[:short_id]           = fields['idShort']
  attributes[:name]               = fields['name']
  attributes[:desc]               = fields['desc']
  attributes[:due]                = Time.iso8601(fields['due']) rescue nil
  attributes[:closed]             = fields['closed']
  attributes[:url]                = fields['url']
  attributes[:board_id]           = fields['idBoard']
  attributes[:member_ids]         = fields['idMembers']
  attributes[:list_id]            = fields['idList']
  attributes[:pos]                = fields['pos']
  attributes[:last_activity_date] = Time.iso8601(fields['dateLastActivity']) rescue nil
  self
end

#valid?Boolean

Is the record valid?

Returns:

  • (Boolean)


119
120
121
# File 'lib/trello/card.rb', line 119

def valid?
  name && list_id
end