Class: Trello::CustomFieldItem

Inherits:
BasicData show all
Defined in:
lib/trello/custom_field_item.rb

Overview

A custom field item contains the value for a custom field on a particular card.

Instance Attribute Summary

Attributes inherited from BasicData

#client

Instance Method Summary collapse

Methods inherited from BasicData

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

Methods included from JsonUtils

included

Constructor Details

This class inherits a constructor from Trello::BasicData

Instance Method Details

#option_valueObject

Need to make another call to get the actual value if the custom field type == ‘list’



69
70
71
72
73
74
75
# File 'lib/trello/custom_field_item.rb', line 69

def option_value
  if option_id
    option_endpoint = "/customFields/#{custom_field_id}/options/#{option_id}"
    option = CustomFieldOption.from_response client.get(option_endpoint)
    option.value
  end
end

#removeObject

You can’t “delete” a custom field item, you can only clear the value



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

def remove
  params = { value: {} }
  client.put("/card/#{model_id}/customField/#{custom_field_id}/item", params)
end

#saveString

Saves a record.

the Trello API.

Returns:

  • (String)

    The JSON representation of the saved custom field item returned by

Raises:



47
48
49
50
51
52
53
54
# File 'lib/trello/custom_field_item.rb', line 47

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

  from_response client.post("/card/#{model_id}/customField/#{custom_field_id}/item", {
    value: value
  })
end

#typeObject

Type is saved at the CustomField level Can equally be derived from :value, with a little parsing work: 42



64
65
66
# File 'lib/trello/custom_field_item.rb', line 64

def type
  custom_field.type
end

#update!Object



32
33
34
35
36
37
38
39
# File 'lib/trello/custom_field_item.rb', line 32

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("/card/#{model_id}/customField/#{custom_field_id}/item", payload)
end

#update_fields(fields) ⇒ Object

Update the fields of a custom field item.

Supply a hash of string keyed data retrieved from the Trello API representing an item state.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/trello/custom_field_item.rb', line 19

def update_fields(fields)
  attributes[:id]               = fields['id'] || fields[:id] || attributes[:id]
  attributes[:model_id]         = fields['idModel'] || fields[:model_id] || attributes[:model_id]
  attributes[:custom_field_id]  = fields['idCustomField'] || fields[:custom_field_id] || attributes[:custom_field_id]
  attributes[:model_type]       = fields['modelType'] || fields[:model_type] || attributes[:model_type]
  # Dropdown custom field items do not have a value, they have an ID that maps to
  # a different endpoint to get the value
  attributes[:option_id]       = fields['idValue'] if fields.has_key?('idValue')
  # value format: { "text": "hello world" }
  attributes[:value]            = fields['value'] if fields.has_key?('value')
  self
end