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.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasActions

#actions

Methods inherited from BasicData

#==, #initialize, many, one, #refresh!, register_attributes

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
# File 'lib/trello/card.rb', line 19

def create(options)
  new('name'   => options[:name],
      'idList' => options[:list_id],
      'desc'   => options[:description]).save
end

.find(id) ⇒ Object

Find a specific card by its id.



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

def find(id)
  super(:cards, id)
end

Instance Method Details

#add_checklist(checklist) ⇒ Object

Add a checklist to this card



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

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

#add_comment(text) ⇒ Object

Add a comment with the supplied text.



102
103
104
# File 'lib/trello/card.rb', line 102

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

#add_label(colour) ⇒ Object

Add a label



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

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



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

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

#labelsObject

Retrieve a list of labels



140
141
142
143
# File 'lib/trello/card.rb', line 140

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.



58
59
60
61
62
63
# File 'lib/trello/card.rb', line 58

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_list(list) ⇒ Object

Move this card to the given list



133
134
135
136
137
# File 'lib/trello/card.rb', line 133

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

#remove_all_membersObject

Removes all members from this card



126
127
128
129
130
# File 'lib/trello/card.rb', line 126

def remove_all_members
  members.each do |member|
    Client.delete("/cards/#{id}/members/#{member.id}")
  end
end

#remove_label(colour) ⇒ Object

Remove a label



155
156
157
158
159
160
161
# File 'lib/trello/card.rb', line 155

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



121
122
123
# File 'lib/trello/card.rb', line 121

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

#request_prefixObject

:nodoc:



164
165
166
# File 'lib/trello/card.rb', line 164

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

#saveObject

Saves a record.



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

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

  Client.post("/cards", {
    :name   => name,
    :desc   => description,
    :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.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/trello/card.rb', line 81

def update!
  @previously_changed = changes
  @changed_attributes.clear

  Client.put("/cards/#{id}", {
    :name      => name,
    :desc      => description,
    :due       => due && due.utc.iso8601,
    :closed    => closed,
    :idList    => list_id,
    :idBoard   => board_id,
    :idMembers => member_ids
  })
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.



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

def update_fields(fields)
  attributes[:id]          = fields['id']
  attributes[:short_id]    = fields['idShort']
  attributes[:name]        = fields['name']
  attributes[:description] = 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']
  self
end

#valid?Boolean

Is the record valid?

Returns:

  • (Boolean)


97
98
99
# File 'lib/trello/card.rb', line 97

def valid?
  name && list_id
end