Class: Trello::CustomField

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

Overview

A Custom Field can be activated on a board. Values are stored at the card level.

Constant Summary collapse

SYMBOL_TO_STRING =
{
  id:          'id',
  name:        'name',
  model_id:    'idModel',
  model_type:  'modelType',
  field_group: 'fieldGroup',
  type:        'type',
  pos:         'pos'
}

Instance Attribute Summary collapse

Attributes inherited from BasicData

#client

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BasicData

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

Methods included from JsonUtils

included

Constructor Details

#initialize(fields = {}) ⇒ CustomField

Returns a new instance of CustomField.



67
68
69
# File 'lib/trello/custom_field.rb', line 67

def initialize(fields = {})
  initialize_fields(fields)
end

Instance Attribute Details

#field_groupString

Returns:



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/trello/custom_field.rb', line 20

class CustomField < BasicData
  register_attributes :id, :model_id, :model_type, :field_group, :name, :pos, :type
  validates_presence_of :id, :model_id, :model_type, :name, :type, :pos

  SYMBOL_TO_STRING = {
    id:          'id',
    name:        'name',
    model_id:    'idModel',
    model_type:  'modelType',
    field_group: 'fieldGroup',
    type:        'type',
    pos:         'pos'
  }

  class << self
    # Find a custom field by its id.
    def find(id, params = {})
      client.find('customFields', id, params)
    end

    # Create a new custom field and save it on Trello.
    def create(options)
      client.create('customFields',
        'name'       => options[:name],
        'idModel'    => options[:model_id],
        'modelType'  => options[:model_type],
        'fieldGroup' => options[:field_group],
        'type'       => options[:type],
        'pos'        => options[:pos]
      )
    end
  end

  # References Board where this custom field is located
  # Currently, model_type will always be "board" at the customFields endpoint
  one :board, path: :boards, using: :model_id

  # If type == 'list'
  many :custom_field_options, path: 'options'

  def update_fields(fields)
    send('name_will_change!') if fields_has_key?(fields, :name)
    send('pos_will_change!') if fields_has_key?(fields, :pos)

    initialize_fields(fields)
  end

  def initialize(fields = {})
    initialize_fields(fields)
  end

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

    from_response client.post("/customFields", {
      name:       name,
      idModel:    model_id,
      modelType:  model_type,
      type:       type,
      pos:        pos,
      fieldGroup: field_group
    })
  end

  # Update an existing custom field.
  def update!
    @previously_changed = changes
    # extract only new values to build payload
    payload = Hash[changes.map { |key, values| [SYMBOL_TO_STRING[key.to_sym].to_sym, values[1]] }]
    @changed_attributes.clear if @changed_attributes.respond_to?(:clear)
    changes_applied if respond_to?(:changes_applied)

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

  # Delete this custom field
  # Also deletes all associated values across all cards
  def delete
    client.delete("/customFields/#{id}")
  end

  # If type == 'list', create a new option and add to this Custom Field
  def create_new_option(value)
    payload = { value: value }
    client.post("/customFields/#{id}/options", payload)
  end

  # Will also clear it from individual cards that have this option selected
  def delete_option(option_id)
    client.delete("/customFields/#{id}/options/#{option_id}")
  end

  private

  def fields_has_key?(fields, key)
    fields.key?(SYMBOL_TO_STRING[key]) || fields.key?(key)
  end

  def initialize_fields(fields)
    attributes[:id]          = fields[SYMBOL_TO_STRING[:id]] || fields[:id] || attributes[:id]
    attributes[:name]        = fields[SYMBOL_TO_STRING[:name]] || fields[:name] || attributes[:name]
    attributes[:model_id]    = fields[SYMBOL_TO_STRING[:model_id]] || fields[:model_id] || attributes[:model_id]
    attributes[:model_type]  = fields[SYMBOL_TO_STRING[:model_type]] || fields[:model_type] || attributes[:model_type]
    attributes[:field_group] = fields[SYMBOL_TO_STRING[:field_group]] || fields[:field_group] || attributes[:field_group]
    attributes[:type]        = fields[SYMBOL_TO_STRING[:type]] || fields[:type] || attributes[:type]
    attributes[:pos]         = fields[SYMBOL_TO_STRING[:pos]] || fields[:pos] || attributes[:pos]
    self
  end
end

#idString

Returns:



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/trello/custom_field.rb', line 20

class CustomField < BasicData
  register_attributes :id, :model_id, :model_type, :field_group, :name, :pos, :type
  validates_presence_of :id, :model_id, :model_type, :name, :type, :pos

  SYMBOL_TO_STRING = {
    id:          'id',
    name:        'name',
    model_id:    'idModel',
    model_type:  'modelType',
    field_group: 'fieldGroup',
    type:        'type',
    pos:         'pos'
  }

  class << self
    # Find a custom field by its id.
    def find(id, params = {})
      client.find('customFields', id, params)
    end

    # Create a new custom field and save it on Trello.
    def create(options)
      client.create('customFields',
        'name'       => options[:name],
        'idModel'    => options[:model_id],
        'modelType'  => options[:model_type],
        'fieldGroup' => options[:field_group],
        'type'       => options[:type],
        'pos'        => options[:pos]
      )
    end
  end

  # References Board where this custom field is located
  # Currently, model_type will always be "board" at the customFields endpoint
  one :board, path: :boards, using: :model_id

  # If type == 'list'
  many :custom_field_options, path: 'options'

  def update_fields(fields)
    send('name_will_change!') if fields_has_key?(fields, :name)
    send('pos_will_change!') if fields_has_key?(fields, :pos)

    initialize_fields(fields)
  end

  def initialize(fields = {})
    initialize_fields(fields)
  end

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

    from_response client.post("/customFields", {
      name:       name,
      idModel:    model_id,
      modelType:  model_type,
      type:       type,
      pos:        pos,
      fieldGroup: field_group
    })
  end

  # Update an existing custom field.
  def update!
    @previously_changed = changes
    # extract only new values to build payload
    payload = Hash[changes.map { |key, values| [SYMBOL_TO_STRING[key.to_sym].to_sym, values[1]] }]
    @changed_attributes.clear if @changed_attributes.respond_to?(:clear)
    changes_applied if respond_to?(:changes_applied)

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

  # Delete this custom field
  # Also deletes all associated values across all cards
  def delete
    client.delete("/customFields/#{id}")
  end

  # If type == 'list', create a new option and add to this Custom Field
  def create_new_option(value)
    payload = { value: value }
    client.post("/customFields/#{id}/options", payload)
  end

  # Will also clear it from individual cards that have this option selected
  def delete_option(option_id)
    client.delete("/customFields/#{id}/options/#{option_id}")
  end

  private

  def fields_has_key?(fields, key)
    fields.key?(SYMBOL_TO_STRING[key]) || fields.key?(key)
  end

  def initialize_fields(fields)
    attributes[:id]          = fields[SYMBOL_TO_STRING[:id]] || fields[:id] || attributes[:id]
    attributes[:name]        = fields[SYMBOL_TO_STRING[:name]] || fields[:name] || attributes[:name]
    attributes[:model_id]    = fields[SYMBOL_TO_STRING[:model_id]] || fields[:model_id] || attributes[:model_id]
    attributes[:model_type]  = fields[SYMBOL_TO_STRING[:model_type]] || fields[:model_type] || attributes[:model_type]
    attributes[:field_group] = fields[SYMBOL_TO_STRING[:field_group]] || fields[:field_group] || attributes[:field_group]
    attributes[:type]        = fields[SYMBOL_TO_STRING[:type]] || fields[:type] || attributes[:type]
    attributes[:pos]         = fields[SYMBOL_TO_STRING[:pos]] || fields[:pos] || attributes[:pos]
    self
  end
end

#model_idString

Returns:



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/trello/custom_field.rb', line 20

class CustomField < BasicData
  register_attributes :id, :model_id, :model_type, :field_group, :name, :pos, :type
  validates_presence_of :id, :model_id, :model_type, :name, :type, :pos

  SYMBOL_TO_STRING = {
    id:          'id',
    name:        'name',
    model_id:    'idModel',
    model_type:  'modelType',
    field_group: 'fieldGroup',
    type:        'type',
    pos:         'pos'
  }

  class << self
    # Find a custom field by its id.
    def find(id, params = {})
      client.find('customFields', id, params)
    end

    # Create a new custom field and save it on Trello.
    def create(options)
      client.create('customFields',
        'name'       => options[:name],
        'idModel'    => options[:model_id],
        'modelType'  => options[:model_type],
        'fieldGroup' => options[:field_group],
        'type'       => options[:type],
        'pos'        => options[:pos]
      )
    end
  end

  # References Board where this custom field is located
  # Currently, model_type will always be "board" at the customFields endpoint
  one :board, path: :boards, using: :model_id

  # If type == 'list'
  many :custom_field_options, path: 'options'

  def update_fields(fields)
    send('name_will_change!') if fields_has_key?(fields, :name)
    send('pos_will_change!') if fields_has_key?(fields, :pos)

    initialize_fields(fields)
  end

  def initialize(fields = {})
    initialize_fields(fields)
  end

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

    from_response client.post("/customFields", {
      name:       name,
      idModel:    model_id,
      modelType:  model_type,
      type:       type,
      pos:        pos,
      fieldGroup: field_group
    })
  end

  # Update an existing custom field.
  def update!
    @previously_changed = changes
    # extract only new values to build payload
    payload = Hash[changes.map { |key, values| [SYMBOL_TO_STRING[key.to_sym].to_sym, values[1]] }]
    @changed_attributes.clear if @changed_attributes.respond_to?(:clear)
    changes_applied if respond_to?(:changes_applied)

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

  # Delete this custom field
  # Also deletes all associated values across all cards
  def delete
    client.delete("/customFields/#{id}")
  end

  # If type == 'list', create a new option and add to this Custom Field
  def create_new_option(value)
    payload = { value: value }
    client.post("/customFields/#{id}/options", payload)
  end

  # Will also clear it from individual cards that have this option selected
  def delete_option(option_id)
    client.delete("/customFields/#{id}/options/#{option_id}")
  end

  private

  def fields_has_key?(fields, key)
    fields.key?(SYMBOL_TO_STRING[key]) || fields.key?(key)
  end

  def initialize_fields(fields)
    attributes[:id]          = fields[SYMBOL_TO_STRING[:id]] || fields[:id] || attributes[:id]
    attributes[:name]        = fields[SYMBOL_TO_STRING[:name]] || fields[:name] || attributes[:name]
    attributes[:model_id]    = fields[SYMBOL_TO_STRING[:model_id]] || fields[:model_id] || attributes[:model_id]
    attributes[:model_type]  = fields[SYMBOL_TO_STRING[:model_type]] || fields[:model_type] || attributes[:model_type]
    attributes[:field_group] = fields[SYMBOL_TO_STRING[:field_group]] || fields[:field_group] || attributes[:field_group]
    attributes[:type]        = fields[SYMBOL_TO_STRING[:type]] || fields[:type] || attributes[:type]
    attributes[:pos]         = fields[SYMBOL_TO_STRING[:pos]] || fields[:pos] || attributes[:pos]
    self
  end
end

#model_typeString

Returns:



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/trello/custom_field.rb', line 20

class CustomField < BasicData
  register_attributes :id, :model_id, :model_type, :field_group, :name, :pos, :type
  validates_presence_of :id, :model_id, :model_type, :name, :type, :pos

  SYMBOL_TO_STRING = {
    id:          'id',
    name:        'name',
    model_id:    'idModel',
    model_type:  'modelType',
    field_group: 'fieldGroup',
    type:        'type',
    pos:         'pos'
  }

  class << self
    # Find a custom field by its id.
    def find(id, params = {})
      client.find('customFields', id, params)
    end

    # Create a new custom field and save it on Trello.
    def create(options)
      client.create('customFields',
        'name'       => options[:name],
        'idModel'    => options[:model_id],
        'modelType'  => options[:model_type],
        'fieldGroup' => options[:field_group],
        'type'       => options[:type],
        'pos'        => options[:pos]
      )
    end
  end

  # References Board where this custom field is located
  # Currently, model_type will always be "board" at the customFields endpoint
  one :board, path: :boards, using: :model_id

  # If type == 'list'
  many :custom_field_options, path: 'options'

  def update_fields(fields)
    send('name_will_change!') if fields_has_key?(fields, :name)
    send('pos_will_change!') if fields_has_key?(fields, :pos)

    initialize_fields(fields)
  end

  def initialize(fields = {})
    initialize_fields(fields)
  end

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

    from_response client.post("/customFields", {
      name:       name,
      idModel:    model_id,
      modelType:  model_type,
      type:       type,
      pos:        pos,
      fieldGroup: field_group
    })
  end

  # Update an existing custom field.
  def update!
    @previously_changed = changes
    # extract only new values to build payload
    payload = Hash[changes.map { |key, values| [SYMBOL_TO_STRING[key.to_sym].to_sym, values[1]] }]
    @changed_attributes.clear if @changed_attributes.respond_to?(:clear)
    changes_applied if respond_to?(:changes_applied)

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

  # Delete this custom field
  # Also deletes all associated values across all cards
  def delete
    client.delete("/customFields/#{id}")
  end

  # If type == 'list', create a new option and add to this Custom Field
  def create_new_option(value)
    payload = { value: value }
    client.post("/customFields/#{id}/options", payload)
  end

  # Will also clear it from individual cards that have this option selected
  def delete_option(option_id)
    client.delete("/customFields/#{id}/options/#{option_id}")
  end

  private

  def fields_has_key?(fields, key)
    fields.key?(SYMBOL_TO_STRING[key]) || fields.key?(key)
  end

  def initialize_fields(fields)
    attributes[:id]          = fields[SYMBOL_TO_STRING[:id]] || fields[:id] || attributes[:id]
    attributes[:name]        = fields[SYMBOL_TO_STRING[:name]] || fields[:name] || attributes[:name]
    attributes[:model_id]    = fields[SYMBOL_TO_STRING[:model_id]] || fields[:model_id] || attributes[:model_id]
    attributes[:model_type]  = fields[SYMBOL_TO_STRING[:model_type]] || fields[:model_type] || attributes[:model_type]
    attributes[:field_group] = fields[SYMBOL_TO_STRING[:field_group]] || fields[:field_group] || attributes[:field_group]
    attributes[:type]        = fields[SYMBOL_TO_STRING[:type]] || fields[:type] || attributes[:type]
    attributes[:pos]         = fields[SYMBOL_TO_STRING[:pos]] || fields[:pos] || attributes[:pos]
    self
  end
end

#nameString

Returns:



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/trello/custom_field.rb', line 20

class CustomField < BasicData
  register_attributes :id, :model_id, :model_type, :field_group, :name, :pos, :type
  validates_presence_of :id, :model_id, :model_type, :name, :type, :pos

  SYMBOL_TO_STRING = {
    id:          'id',
    name:        'name',
    model_id:    'idModel',
    model_type:  'modelType',
    field_group: 'fieldGroup',
    type:        'type',
    pos:         'pos'
  }

  class << self
    # Find a custom field by its id.
    def find(id, params = {})
      client.find('customFields', id, params)
    end

    # Create a new custom field and save it on Trello.
    def create(options)
      client.create('customFields',
        'name'       => options[:name],
        'idModel'    => options[:model_id],
        'modelType'  => options[:model_type],
        'fieldGroup' => options[:field_group],
        'type'       => options[:type],
        'pos'        => options[:pos]
      )
    end
  end

  # References Board where this custom field is located
  # Currently, model_type will always be "board" at the customFields endpoint
  one :board, path: :boards, using: :model_id

  # If type == 'list'
  many :custom_field_options, path: 'options'

  def update_fields(fields)
    send('name_will_change!') if fields_has_key?(fields, :name)
    send('pos_will_change!') if fields_has_key?(fields, :pos)

    initialize_fields(fields)
  end

  def initialize(fields = {})
    initialize_fields(fields)
  end

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

    from_response client.post("/customFields", {
      name:       name,
      idModel:    model_id,
      modelType:  model_type,
      type:       type,
      pos:        pos,
      fieldGroup: field_group
    })
  end

  # Update an existing custom field.
  def update!
    @previously_changed = changes
    # extract only new values to build payload
    payload = Hash[changes.map { |key, values| [SYMBOL_TO_STRING[key.to_sym].to_sym, values[1]] }]
    @changed_attributes.clear if @changed_attributes.respond_to?(:clear)
    changes_applied if respond_to?(:changes_applied)

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

  # Delete this custom field
  # Also deletes all associated values across all cards
  def delete
    client.delete("/customFields/#{id}")
  end

  # If type == 'list', create a new option and add to this Custom Field
  def create_new_option(value)
    payload = { value: value }
    client.post("/customFields/#{id}/options", payload)
  end

  # Will also clear it from individual cards that have this option selected
  def delete_option(option_id)
    client.delete("/customFields/#{id}/options/#{option_id}")
  end

  private

  def fields_has_key?(fields, key)
    fields.key?(SYMBOL_TO_STRING[key]) || fields.key?(key)
  end

  def initialize_fields(fields)
    attributes[:id]          = fields[SYMBOL_TO_STRING[:id]] || fields[:id] || attributes[:id]
    attributes[:name]        = fields[SYMBOL_TO_STRING[:name]] || fields[:name] || attributes[:name]
    attributes[:model_id]    = fields[SYMBOL_TO_STRING[:model_id]] || fields[:model_id] || attributes[:model_id]
    attributes[:model_type]  = fields[SYMBOL_TO_STRING[:model_type]] || fields[:model_type] || attributes[:model_type]
    attributes[:field_group] = fields[SYMBOL_TO_STRING[:field_group]] || fields[:field_group] || attributes[:field_group]
    attributes[:type]        = fields[SYMBOL_TO_STRING[:type]] || fields[:type] || attributes[:type]
    attributes[:pos]         = fields[SYMBOL_TO_STRING[:pos]] || fields[:pos] || attributes[:pos]
    self
  end
end

#optionsArray<Hash>

Returns:



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/trello/custom_field.rb', line 20

class CustomField < BasicData
  register_attributes :id, :model_id, :model_type, :field_group, :name, :pos, :type
  validates_presence_of :id, :model_id, :model_type, :name, :type, :pos

  SYMBOL_TO_STRING = {
    id:          'id',
    name:        'name',
    model_id:    'idModel',
    model_type:  'modelType',
    field_group: 'fieldGroup',
    type:        'type',
    pos:         'pos'
  }

  class << self
    # Find a custom field by its id.
    def find(id, params = {})
      client.find('customFields', id, params)
    end

    # Create a new custom field and save it on Trello.
    def create(options)
      client.create('customFields',
        'name'       => options[:name],
        'idModel'    => options[:model_id],
        'modelType'  => options[:model_type],
        'fieldGroup' => options[:field_group],
        'type'       => options[:type],
        'pos'        => options[:pos]
      )
    end
  end

  # References Board where this custom field is located
  # Currently, model_type will always be "board" at the customFields endpoint
  one :board, path: :boards, using: :model_id

  # If type == 'list'
  many :custom_field_options, path: 'options'

  def update_fields(fields)
    send('name_will_change!') if fields_has_key?(fields, :name)
    send('pos_will_change!') if fields_has_key?(fields, :pos)

    initialize_fields(fields)
  end

  def initialize(fields = {})
    initialize_fields(fields)
  end

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

    from_response client.post("/customFields", {
      name:       name,
      idModel:    model_id,
      modelType:  model_type,
      type:       type,
      pos:        pos,
      fieldGroup: field_group
    })
  end

  # Update an existing custom field.
  def update!
    @previously_changed = changes
    # extract only new values to build payload
    payload = Hash[changes.map { |key, values| [SYMBOL_TO_STRING[key.to_sym].to_sym, values[1]] }]
    @changed_attributes.clear if @changed_attributes.respond_to?(:clear)
    changes_applied if respond_to?(:changes_applied)

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

  # Delete this custom field
  # Also deletes all associated values across all cards
  def delete
    client.delete("/customFields/#{id}")
  end

  # If type == 'list', create a new option and add to this Custom Field
  def create_new_option(value)
    payload = { value: value }
    client.post("/customFields/#{id}/options", payload)
  end

  # Will also clear it from individual cards that have this option selected
  def delete_option(option_id)
    client.delete("/customFields/#{id}/options/#{option_id}")
  end

  private

  def fields_has_key?(fields, key)
    fields.key?(SYMBOL_TO_STRING[key]) || fields.key?(key)
  end

  def initialize_fields(fields)
    attributes[:id]          = fields[SYMBOL_TO_STRING[:id]] || fields[:id] || attributes[:id]
    attributes[:name]        = fields[SYMBOL_TO_STRING[:name]] || fields[:name] || attributes[:name]
    attributes[:model_id]    = fields[SYMBOL_TO_STRING[:model_id]] || fields[:model_id] || attributes[:model_id]
    attributes[:model_type]  = fields[SYMBOL_TO_STRING[:model_type]] || fields[:model_type] || attributes[:model_type]
    attributes[:field_group] = fields[SYMBOL_TO_STRING[:field_group]] || fields[:field_group] || attributes[:field_group]
    attributes[:type]        = fields[SYMBOL_TO_STRING[:type]] || fields[:type] || attributes[:type]
    attributes[:pos]         = fields[SYMBOL_TO_STRING[:pos]] || fields[:pos] || attributes[:pos]
    self
  end
end

#posFloat

Returns:

  • (Float)


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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/trello/custom_field.rb', line 20

class CustomField < BasicData
  register_attributes :id, :model_id, :model_type, :field_group, :name, :pos, :type
  validates_presence_of :id, :model_id, :model_type, :name, :type, :pos

  SYMBOL_TO_STRING = {
    id:          'id',
    name:        'name',
    model_id:    'idModel',
    model_type:  'modelType',
    field_group: 'fieldGroup',
    type:        'type',
    pos:         'pos'
  }

  class << self
    # Find a custom field by its id.
    def find(id, params = {})
      client.find('customFields', id, params)
    end

    # Create a new custom field and save it on Trello.
    def create(options)
      client.create('customFields',
        'name'       => options[:name],
        'idModel'    => options[:model_id],
        'modelType'  => options[:model_type],
        'fieldGroup' => options[:field_group],
        'type'       => options[:type],
        'pos'        => options[:pos]
      )
    end
  end

  # References Board where this custom field is located
  # Currently, model_type will always be "board" at the customFields endpoint
  one :board, path: :boards, using: :model_id

  # If type == 'list'
  many :custom_field_options, path: 'options'

  def update_fields(fields)
    send('name_will_change!') if fields_has_key?(fields, :name)
    send('pos_will_change!') if fields_has_key?(fields, :pos)

    initialize_fields(fields)
  end

  def initialize(fields = {})
    initialize_fields(fields)
  end

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

    from_response client.post("/customFields", {
      name:       name,
      idModel:    model_id,
      modelType:  model_type,
      type:       type,
      pos:        pos,
      fieldGroup: field_group
    })
  end

  # Update an existing custom field.
  def update!
    @previously_changed = changes
    # extract only new values to build payload
    payload = Hash[changes.map { |key, values| [SYMBOL_TO_STRING[key.to_sym].to_sym, values[1]] }]
    @changed_attributes.clear if @changed_attributes.respond_to?(:clear)
    changes_applied if respond_to?(:changes_applied)

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

  # Delete this custom field
  # Also deletes all associated values across all cards
  def delete
    client.delete("/customFields/#{id}")
  end

  # If type == 'list', create a new option and add to this Custom Field
  def create_new_option(value)
    payload = { value: value }
    client.post("/customFields/#{id}/options", payload)
  end

  # Will also clear it from individual cards that have this option selected
  def delete_option(option_id)
    client.delete("/customFields/#{id}/options/#{option_id}")
  end

  private

  def fields_has_key?(fields, key)
    fields.key?(SYMBOL_TO_STRING[key]) || fields.key?(key)
  end

  def initialize_fields(fields)
    attributes[:id]          = fields[SYMBOL_TO_STRING[:id]] || fields[:id] || attributes[:id]
    attributes[:name]        = fields[SYMBOL_TO_STRING[:name]] || fields[:name] || attributes[:name]
    attributes[:model_id]    = fields[SYMBOL_TO_STRING[:model_id]] || fields[:model_id] || attributes[:model_id]
    attributes[:model_type]  = fields[SYMBOL_TO_STRING[:model_type]] || fields[:model_type] || attributes[:model_type]
    attributes[:field_group] = fields[SYMBOL_TO_STRING[:field_group]] || fields[:field_group] || attributes[:field_group]
    attributes[:type]        = fields[SYMBOL_TO_STRING[:type]] || fields[:type] || attributes[:type]
    attributes[:pos]         = fields[SYMBOL_TO_STRING[:pos]] || fields[:pos] || attributes[:pos]
    self
  end
end

#typeString

Returns:



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/trello/custom_field.rb', line 20

class CustomField < BasicData
  register_attributes :id, :model_id, :model_type, :field_group, :name, :pos, :type
  validates_presence_of :id, :model_id, :model_type, :name, :type, :pos

  SYMBOL_TO_STRING = {
    id:          'id',
    name:        'name',
    model_id:    'idModel',
    model_type:  'modelType',
    field_group: 'fieldGroup',
    type:        'type',
    pos:         'pos'
  }

  class << self
    # Find a custom field by its id.
    def find(id, params = {})
      client.find('customFields', id, params)
    end

    # Create a new custom field and save it on Trello.
    def create(options)
      client.create('customFields',
        'name'       => options[:name],
        'idModel'    => options[:model_id],
        'modelType'  => options[:model_type],
        'fieldGroup' => options[:field_group],
        'type'       => options[:type],
        'pos'        => options[:pos]
      )
    end
  end

  # References Board where this custom field is located
  # Currently, model_type will always be "board" at the customFields endpoint
  one :board, path: :boards, using: :model_id

  # If type == 'list'
  many :custom_field_options, path: 'options'

  def update_fields(fields)
    send('name_will_change!') if fields_has_key?(fields, :name)
    send('pos_will_change!') if fields_has_key?(fields, :pos)

    initialize_fields(fields)
  end

  def initialize(fields = {})
    initialize_fields(fields)
  end

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

    from_response client.post("/customFields", {
      name:       name,
      idModel:    model_id,
      modelType:  model_type,
      type:       type,
      pos:        pos,
      fieldGroup: field_group
    })
  end

  # Update an existing custom field.
  def update!
    @previously_changed = changes
    # extract only new values to build payload
    payload = Hash[changes.map { |key, values| [SYMBOL_TO_STRING[key.to_sym].to_sym, values[1]] }]
    @changed_attributes.clear if @changed_attributes.respond_to?(:clear)
    changes_applied if respond_to?(:changes_applied)

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

  # Delete this custom field
  # Also deletes all associated values across all cards
  def delete
    client.delete("/customFields/#{id}")
  end

  # If type == 'list', create a new option and add to this Custom Field
  def create_new_option(value)
    payload = { value: value }
    client.post("/customFields/#{id}/options", payload)
  end

  # Will also clear it from individual cards that have this option selected
  def delete_option(option_id)
    client.delete("/customFields/#{id}/options/#{option_id}")
  end

  private

  def fields_has_key?(fields, key)
    fields.key?(SYMBOL_TO_STRING[key]) || fields.key?(key)
  end

  def initialize_fields(fields)
    attributes[:id]          = fields[SYMBOL_TO_STRING[:id]] || fields[:id] || attributes[:id]
    attributes[:name]        = fields[SYMBOL_TO_STRING[:name]] || fields[:name] || attributes[:name]
    attributes[:model_id]    = fields[SYMBOL_TO_STRING[:model_id]] || fields[:model_id] || attributes[:model_id]
    attributes[:model_type]  = fields[SYMBOL_TO_STRING[:model_type]] || fields[:model_type] || attributes[:model_type]
    attributes[:field_group] = fields[SYMBOL_TO_STRING[:field_group]] || fields[:field_group] || attributes[:field_group]
    attributes[:type]        = fields[SYMBOL_TO_STRING[:type]] || fields[:type] || attributes[:type]
    attributes[:pos]         = fields[SYMBOL_TO_STRING[:pos]] || fields[:pos] || attributes[:pos]
    self
  end
end

Class Method Details

.create(options) ⇒ Object

Create a new custom field and save it on Trello.



41
42
43
44
45
46
47
48
49
50
# File 'lib/trello/custom_field.rb', line 41

def create(options)
  client.create('customFields',
    'name'       => options[:name],
    'idModel'    => options[:model_id],
    'modelType'  => options[:model_type],
    'fieldGroup' => options[:field_group],
    'type'       => options[:type],
    'pos'        => options[:pos]
  )
end

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

Find a custom field by its id.



36
37
38
# File 'lib/trello/custom_field.rb', line 36

def find(id, params = {})
  client.find('customFields', id, params)
end

Instance Method Details

#create_new_option(value) ⇒ Object

If type == ‘list’, create a new option and add to this Custom Field



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

def create_new_option(value)
  payload = { value: value }
  client.post("/customFields/#{id}/options", payload)
end

#deleteObject

Delete this custom field Also deletes all associated values across all cards



99
100
101
# File 'lib/trello/custom_field.rb', line 99

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

#delete_option(option_id) ⇒ Object

Will also clear it from individual cards that have this option selected



110
111
112
# File 'lib/trello/custom_field.rb', line 110

def delete_option(option_id)
  client.delete("/customFields/#{id}/options/#{option_id}")
end

#saveObject

Saves a record.



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

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

  from_response client.post("/customFields", {
    name:       name,
    idModel:    model_id,
    modelType:  model_type,
    type:       type,
    pos:        pos,
    fieldGroup: field_group
  })
end

#update!Object

Update an existing custom field.



87
88
89
90
91
92
93
94
95
# File 'lib/trello/custom_field.rb', line 87

def update!
  @previously_changed = changes
  # extract only new values to build payload
  payload = Hash[changes.map { |key, values| [SYMBOL_TO_STRING[key.to_sym].to_sym, values[1]] }]
  @changed_attributes.clear if @changed_attributes.respond_to?(:clear)
  changes_applied if respond_to?(:changes_applied)

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

#update_fields(fields) ⇒ Object



60
61
62
63
64
65
# File 'lib/trello/custom_field.rb', line 60

def update_fields(fields)
  send('name_will_change!') if fields_has_key?(fields, :name)
  send('pos_will_change!') if fields_has_key?(fields, :pos)

  initialize_fields(fields)
end