ruby-trello 2.0.0 → 2.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +106 -17
  3. data/lib/trello.rb +47 -34
  4. data/lib/trello/action.rb +10 -8
  5. data/lib/trello/association_builder/has_many.rb +17 -0
  6. data/lib/trello/association_builder/has_one.rb +16 -0
  7. data/lib/trello/association_fetcher/has_many.rb +26 -0
  8. data/lib/trello/association_fetcher/has_many/fetch.rb +47 -0
  9. data/lib/trello/association_fetcher/has_many/params.rb +56 -0
  10. data/lib/trello/association_fetcher/has_one.rb +25 -0
  11. data/lib/trello/association_fetcher/has_one/fetch.rb +49 -0
  12. data/lib/trello/association_fetcher/has_one/params.rb +36 -0
  13. data/lib/trello/association_infer_tool.rb +13 -0
  14. data/lib/trello/association_proxy.rb +1 -1
  15. data/lib/trello/attachment.rb +13 -10
  16. data/lib/trello/basic_data.rb +16 -49
  17. data/lib/trello/board.rb +17 -3
  18. data/lib/trello/card.rb +103 -38
  19. data/lib/trello/checklist.rb +14 -13
  20. data/lib/trello/client.rb +1 -1
  21. data/lib/trello/comment.rb +4 -4
  22. data/lib/trello/custom_field.rb +131 -0
  23. data/lib/trello/custom_field_item.rb +98 -0
  24. data/lib/trello/custom_field_option.rb +22 -0
  25. data/lib/trello/error.rb +12 -0
  26. data/lib/trello/item.rb +7 -7
  27. data/lib/trello/item_state.rb +3 -3
  28. data/lib/trello/label.rb +30 -12
  29. data/lib/trello/label_name.rb +10 -10
  30. data/lib/trello/list.rb +6 -6
  31. data/lib/trello/member.rb +10 -9
  32. data/lib/trello/notification.rb +6 -6
  33. data/lib/trello/organization.rb +11 -11
  34. data/lib/trello/plugin_datum.rb +6 -6
  35. data/lib/trello/register_attributes.rb +54 -0
  36. data/lib/trello/token.rb +6 -5
  37. data/lib/trello/webhook.rb +5 -5
  38. data/spec/action_spec.rb +22 -1
  39. data/spec/basic_data_spec.rb +58 -0
  40. data/spec/board_spec.rb +28 -2
  41. data/spec/card_spec.rb +183 -8
  42. data/spec/cassettes/can_add_a_file_from_url_on_a_card.yml +189 -0
  43. data/spec/cassettes/can_add_a_file_on_a_card.yml +200 -0
  44. data/spec/cassettes/can_add_a_member_to_a_card.yml +190 -0
  45. data/spec/cassettes/can_add_label_on_a_card.yml +281 -0
  46. data/spec/cassettes/can_close_bong_a_card.yml +189 -0
  47. data/spec/cassettes/can_get_actions.yml +196 -0
  48. data/spec/cassettes/can_get_attachments.yml +187 -0
  49. data/spec/cassettes/can_get_comments.yml +193 -0
  50. data/spec/cassettes/can_move_a_card_to_another_board_with_specific_list.yml +373 -0
  51. data/spec/cassettes/can_move_a_card_to_another_board_without_specific_list.yml +281 -0
  52. data/spec/cassettes/can_move_a_card_to_another_list.yml +281 -0
  53. data/spec/cassettes/can_remove_a_member_from_a_card.yml +185 -0
  54. data/spec/cassettes/can_remove_an_attachment_on_a_card.yml +277 -0
  55. data/spec/cassettes/can_remove_an_upvote_on_a_card.yml +465 -0
  56. data/spec/cassettes/can_remove_label_on_a_card.yml +279 -0
  57. data/spec/cassettes/can_success_add_comment_to_a_card.yml +196 -0
  58. data/spec/cassettes/can_success_copy_checklist_to_a_card.yml +281 -0
  59. data/spec/cassettes/can_success_copy_checklist_to_a_card_without_name_pos.yml +281 -0
  60. data/spec/cassettes/can_success_create_a_card.yml +99 -0
  61. data/spec/cassettes/can_success_create_new_checklist_to_a_card.yml +189 -0
  62. data/spec/cassettes/can_success_delete_card.yml +185 -0
  63. data/spec/cassettes/can_success_upate_a_card.yml +189 -0
  64. data/spec/cassettes/can_success_update_bong_a_board.yml +283 -0
  65. data/spec/cassettes/can_success_update_bong_a_card.yml +191 -0
  66. data/spec/cassettes/can_upvote_on_a_card.yml +469 -0
  67. data/spec/cassettes/card_find_with_id_and_get_all_fields.yml +95 -0
  68. data/spec/cassettes/card_find_with_id_and_get_specific_fields.yml +96 -0
  69. data/spec/cassettes/custom_field_item_save_1.yml +97 -0
  70. data/spec/cassettes/custom_field_item_save_2.yml +281 -0
  71. data/spec/cassettes/get_board_of_card.yml +187 -0
  72. data/spec/cassettes/get_check_item_states_of_card.yml +188 -0
  73. data/spec/cassettes/get_checklists_of_card.yml +187 -0
  74. data/spec/cassettes/get_cover_image_of_card.yml +187 -0
  75. data/spec/cassettes/get_custom_field_items_of_card.yml +187 -0
  76. data/spec/cassettes/get_list_of_card.yml +188 -0
  77. data/spec/cassettes/get_lists.yml +187 -0
  78. data/spec/cassettes/get_members_of_card.yml +189 -0
  79. data/spec/cassettes/get_plugin_data_of_card.yml +187 -0
  80. data/spec/cassettes/get_voters_of_card.yml +188 -0
  81. data/spec/cassettes/remove_upvote_on_a_card_when_have_not_voted.yml +366 -0
  82. data/spec/cassettes/revote_on_a_card.yml +464 -0
  83. data/spec/checklist_spec.rb +39 -3
  84. data/spec/client_spec.rb +5 -1
  85. data/spec/custom_field_item_spec.rb +192 -0
  86. data/spec/custom_field_option_spec.rb +49 -0
  87. data/spec/custom_field_spec.rb +261 -0
  88. data/spec/integration/basic_data/many_spec.rb +123 -0
  89. data/spec/integration/basic_data/one_spec.rb +84 -0
  90. data/spec/integration/basic_data/register_attributes_spec.rb +75 -0
  91. data/spec/integration/board/update!_spec.rb +31 -0
  92. data/spec/integration/board_lists_spec.rb +21 -0
  93. data/spec/integration/card/add_and_remove_attachment_spec.rb +45 -0
  94. data/spec/integration/card/add_and_remove_label_spec.rb +35 -0
  95. data/spec/integration/card/add_checklist_spec.rb +32 -0
  96. data/spec/integration/card/add_comment_spec.rb +19 -0
  97. data/spec/integration/card/add_memeber_spec.rb +19 -0
  98. data/spec/integration/card/associations/actions_spec.rb +17 -0
  99. data/spec/integration/card/associations/attachments_spec.rb +18 -0
  100. data/spec/integration/card/associations/board_spec.rb +17 -0
  101. data/spec/integration/card/associations/check_item_states_spec.rb +17 -0
  102. data/spec/integration/card/associations/checklists_spec.rb +18 -0
  103. data/spec/integration/card/associations/comments_spec.rb +19 -0
  104. data/spec/integration/card/associations/cover_image_spec.rb +18 -0
  105. data/spec/integration/card/associations/custom_field_items_spec.rb +18 -0
  106. data/spec/integration/card/associations/list_spec.rb +16 -0
  107. data/spec/integration/card/associations/members_spec.rb +18 -0
  108. data/spec/integration/card/associations/plugin_data_spec.rb +18 -0
  109. data/spec/integration/card/associations/voters_spec.rb +17 -0
  110. data/spec/integration/card/close!_spec.rb +16 -0
  111. data/spec/integration/card/create_new_check_list_spec.rb +19 -0
  112. data/spec/integration/card/create_spec.rb +50 -0
  113. data/spec/integration/card/delete_spec.rb +16 -0
  114. data/spec/integration/card/find_spec.rb +67 -0
  115. data/spec/integration/card/move_to_board_spec.rb +36 -0
  116. data/spec/integration/card/move_to_list_spec.rb +20 -0
  117. data/spec/integration/card/remove_member_spec.rb +19 -0
  118. data/spec/integration/card/save_spec.rb +61 -0
  119. data/spec/integration/card/update!_spec.rb +53 -0
  120. data/spec/integration/card/vote_spec.rb +50 -0
  121. data/spec/integration/custom_field_item_spec.rb +47 -0
  122. data/spec/item_spec.rb +20 -0
  123. data/spec/label_spec.rb +99 -0
  124. data/spec/list_spec.rb +21 -0
  125. data/spec/member_spec.rb +23 -0
  126. data/spec/notification_spec.rb +21 -0
  127. data/spec/organization_spec.rb +26 -0
  128. data/spec/spec_helper.rb +96 -23
  129. data/spec/token_spec.rb +19 -0
  130. data/spec/unit/trello/association_builder/has_many_spec.rb +36 -0
  131. data/spec/unit/trello/association_builder/has_one_spec.rb +36 -0
  132. data/spec/unit/trello/association_fetcher/has_many/fetch_spec.rb +38 -0
  133. data/spec/unit/trello/association_fetcher/has_many/params_spec.rb +107 -0
  134. data/spec/unit/trello/association_fetcher/has_many_spec.rb +50 -0
  135. data/spec/unit/trello/association_fetcher/has_one/fetch_spec.rb +51 -0
  136. data/spec/unit/trello/association_fetcher/has_one/params_spec.rb +81 -0
  137. data/spec/unit/trello/association_fetcher/has_one_spec.rb +49 -0
  138. data/spec/unit/trello/association_infer_tool_spec.rb +41 -0
  139. data/spec/unit/trello/basic_data_spec.rb +54 -0
  140. data/spec/unit/trello/card_spec.rb +103 -0
  141. data/spec/webhook_spec.rb +20 -0
  142. metadata +224 -30
@@ -45,17 +45,17 @@ module Trello
45
45
  # Supply a hash of string keyed data retrieved from the Trello API representing
46
46
  # a checklist.
47
47
  def update_fields(fields)
48
- attributes[:id] = fields['id']
49
- attributes[:name] = fields['name'] || fields[:name]
50
- attributes[:description] = fields['desc']
51
- attributes[:closed] = fields['closed']
52
- attributes[:url] = fields['url']
53
- attributes[:check_items] = fields['checkItems']
54
- attributes[:position] = fields['pos']
55
- attributes[:board_id] = fields['idBoard']
56
- attributes[:card_id] = fields['idCard'] || fields[:card_id]
57
- attributes[:list_id] = fields['idList']
58
- attributes[:member_ids] = fields['idMembers']
48
+ attributes[:id] = fields['id'] || attributes[:id]
49
+ attributes[:name] = fields['name'] || fields[:name] || attributes[:name]
50
+ attributes[:description] = fields['desc'] || attributes[:description]
51
+ attributes[:closed] = fields['closed'] if fields.has_key?('closed')
52
+ attributes[:url] = fields['url'] || attributes[:url]
53
+ attributes[:check_items] = fields['checkItems'] if fields.has_key?('checkItems')
54
+ attributes[:position] = fields['pos'] || attributes[:position]
55
+ attributes[:board_id] = fields['idBoard'] || attributes[:board_id]
56
+ attributes[:card_id] = fields['idCard'] || fields[:card_id] || attributes[:card_id]
57
+ attributes[:list_id] = fields['idList'] || attributes[:list_id]
58
+ attributes[:member_ids] = fields['idMembers'] || attributes[:member_ids]
59
59
  self
60
60
  end
61
61
 
@@ -109,9 +109,10 @@ module Trello
109
109
 
110
110
  # Update a checklist item's state, e.g.: "complete" or "incomplete"
111
111
  def update_item_state(item_id, state)
112
+ state = ( state ? 'complete' : 'incomplete' ) unless state.is_a?(String)
112
113
  client.put(
113
- "/cards/#{card_id}/checklist/#{id}/checkItem/#{item_id}/state",
114
- value: state,
114
+ "/cards/#{card_id}/checkItem/#{item_id}",
115
+ state: state
115
116
  )
116
117
  end
117
118
 
@@ -96,7 +96,7 @@ module Trello
96
96
 
97
97
  unless [200, 201].include? response.code
98
98
  Trello.logger.error("[#{response.code} #{name.to_s.upcase} #{uri}]: #{response.body}")
99
- raise Error, response.body
99
+ raise Error.new(response.body, response.code)
100
100
  end
101
101
 
102
102
  response.body
@@ -27,10 +27,10 @@ module Trello
27
27
  # Supply a hash of string keyed data retrieved from the Trello API representing
28
28
  # a Comment.
29
29
  def update_fields(fields)
30
- attributes[:action_id] = fields['id']
31
- attributes[:text] = fields['data']['text']
32
- attributes[:date] = Time.iso8601(fields['date'])
33
- attributes[:member_creator_id] = fields['idMemberCreator']
30
+ attributes[:action_id] = fields['id'] || attributes[:action_id]
31
+ attributes[:text] = fields['data']['text'] || attributes[:text]
32
+ attributes[:date] = Time.iso8601(fields['date']) if fields.has_key?('date')
33
+ attributes[:member_creator_id] = fields['idMemberCreator'] || attributes[:member_creator_id]
34
34
  self
35
35
  end
36
36
 
@@ -0,0 +1,131 @@
1
+ module Trello
2
+ # A Custom Field can be activated on a board. Values are stored at the card level.
3
+ #
4
+ # @!attribute id
5
+ # @return [String]
6
+ # @!attribute model_id
7
+ # @return [String]
8
+ # @!attribute model_type
9
+ # @return [String]
10
+ # @!attribute field_group
11
+ # @return [String]
12
+ # @!attribute name
13
+ # @return [String]
14
+ # @!attribute pos
15
+ # @return [Float]
16
+ # @!attribute type
17
+ # @return [String]
18
+ # @!attribute options
19
+ # @return [Array<Hash>]
20
+ class CustomField < BasicData
21
+ register_attributes :id, :model_id, :model_type, :field_group, :name, :pos, :type
22
+ validates_presence_of :id, :model_id, :model_type, :name, :type, :pos
23
+
24
+ SYMBOL_TO_STRING = {
25
+ id: 'id',
26
+ name: 'name',
27
+ model_id: 'idModel',
28
+ model_type: 'modelType',
29
+ field_group: 'fieldGroup',
30
+ type: 'type',
31
+ pos: 'pos'
32
+ }
33
+
34
+ class << self
35
+ # Find a custom field by its id.
36
+ def find(id, params = {})
37
+ client.find('customFields', id, params)
38
+ end
39
+
40
+ # Create a new custom field and save it on Trello.
41
+ def create(options)
42
+ client.create('customFields',
43
+ 'name' => options[:name],
44
+ 'idModel' => options[:model_id],
45
+ 'modelType' => options[:model_type],
46
+ 'fieldGroup' => options[:field_group],
47
+ 'type' => options[:type],
48
+ 'pos' => options[:pos]
49
+ )
50
+ end
51
+ end
52
+
53
+ # References Board where this custom field is located
54
+ # Currently, model_type will always be "board" at the customFields endpoint
55
+ one :board, path: :boards, using: :model_id
56
+
57
+ # If type == 'list'
58
+ many :custom_field_options, path: 'options'
59
+
60
+ def update_fields(fields)
61
+ send('name_will_change!') if fields_has_key?(fields, :name)
62
+ send('pos_will_change!') if fields_has_key?(fields, :pos)
63
+
64
+ initialize_fields(fields)
65
+ end
66
+
67
+ def initialize(fields = {})
68
+ initialize_fields(fields)
69
+ end
70
+
71
+ # Saves a record.
72
+ def save
73
+ # If we have an id, just update our fields.
74
+ return update! if id
75
+
76
+ from_response client.post("/customFields", {
77
+ name: name,
78
+ idModel: model_id,
79
+ modelType: model_type,
80
+ type: type,
81
+ pos: pos,
82
+ fieldGroup: field_group
83
+ })
84
+ end
85
+
86
+ # Update an existing custom field.
87
+ def update!
88
+ @previously_changed = changes
89
+ # extract only new values to build payload
90
+ payload = Hash[changes.map { |key, values| [SYMBOL_TO_STRING[key.to_sym].to_sym, values[1]] }]
91
+ @changed_attributes.clear if @changed_attributes.respond_to?(:clear)
92
+ changes_applied if respond_to?(:changes_applied)
93
+
94
+ client.put("/customFields/#{id}", payload)
95
+ end
96
+
97
+ # Delete this custom field
98
+ # Also deletes all associated values across all cards
99
+ def delete
100
+ client.delete("/customFields/#{id}")
101
+ end
102
+
103
+ # If type == 'list', create a new option and add to this Custom Field
104
+ def create_new_option(value)
105
+ payload = { value: value }
106
+ client.post("/customFields/#{id}/options", payload)
107
+ end
108
+
109
+ # Will also clear it from individual cards that have this option selected
110
+ def delete_option(option_id)
111
+ client.delete("/customFields/#{id}/options/#{option_id}")
112
+ end
113
+
114
+ private
115
+
116
+ def fields_has_key?(fields, key)
117
+ fields.key?(SYMBOL_TO_STRING[key]) || fields.key?(key)
118
+ end
119
+
120
+ def initialize_fields(fields)
121
+ attributes[:id] = fields[SYMBOL_TO_STRING[:id]] || fields[:id] || attributes[:id]
122
+ attributes[:name] = fields[SYMBOL_TO_STRING[:name]] || fields[:name] || attributes[:name]
123
+ attributes[:model_id] = fields[SYMBOL_TO_STRING[:model_id]] || fields[:model_id] || attributes[:model_id]
124
+ attributes[:model_type] = fields[SYMBOL_TO_STRING[:model_type]] || fields[:model_type] || attributes[:model_type]
125
+ attributes[:field_group] = fields[SYMBOL_TO_STRING[:field_group]] || fields[:field_group] || attributes[:field_group]
126
+ attributes[:type] = fields[SYMBOL_TO_STRING[:type]] || fields[:type] || attributes[:type]
127
+ attributes[:pos] = fields[SYMBOL_TO_STRING[:pos]] || fields[:pos] || attributes[:pos]
128
+ self
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,98 @@
1
+ module Trello
2
+ # A custom field item contains the value for a custom field on a particular card.
3
+ #
4
+ class CustomFieldItem < BasicData
5
+ register_attributes :id, :model_id, :model_type, :custom_field_id, :value, :option_id,
6
+ readonly: [ :id, :custom_field_id, :model_id, :model_type, :option_id ]
7
+ validates_presence_of :id, :model_id, :custom_field_id
8
+
9
+ # References the card with this custom field value
10
+ one :card, path: :cards, using: :model_id
11
+
12
+ # References the parent custom field that this item is an instance of
13
+ one :custom_field, path: 'customFields', using: :custom_field_id
14
+
15
+ # Update the fields of a custom field item.
16
+ #
17
+ # Supply a hash of string keyed data retrieved from the Trello API representing
18
+ # an item state.
19
+ def update_fields(fields)
20
+ if fields_has_key?(fields, 'value')
21
+ send('value_will_change!')
22
+ elsif fields_has_key?(fields, 'idValue')
23
+ send('option_id_will_change!')
24
+ end
25
+
26
+ initialize_fields(fields)
27
+ end
28
+
29
+ def initialize(fields = {})
30
+ initialize_fields(fields)
31
+ end
32
+
33
+ def update!
34
+ @previously_changed = changes
35
+ # extract only new values to build payload
36
+ payload = Hash[changes.map { |key, values| [key.to_sym, values[1]] }]
37
+ @changed_attributes.clear if @changed_attributes.respond_to?(:clear)
38
+ changes_applied if respond_to?(:changes_applied)
39
+
40
+ client.put("/card/#{model_id}/customField/#{custom_field_id}/item", payload)
41
+ end
42
+
43
+ # Saves a record.
44
+ #
45
+ # @raise [Trello::Error] if the card could not be saved
46
+ #
47
+ # @return [String] The JSON representation of the saved custom field item returned by
48
+ # the Trello API.
49
+ def save
50
+ # If we have an id, just update our fields.
51
+ return update! if id
52
+
53
+ from_response client.put("/card/#{model_id}/customField/#{custom_field_id}/item", {
54
+ value: value
55
+ })
56
+ end
57
+
58
+ # You can't "delete" a custom field item, you can only clear the value
59
+ def remove
60
+ params = { value: {} }
61
+ client.put("/card/#{model_id}/customField/#{custom_field_id}/item", params)
62
+ end
63
+
64
+ # Type is saved at the CustomField level
65
+ # Can equally be derived from :value, with a little parsing work: {"number": 42}
66
+ def type
67
+ custom_field.type
68
+ end
69
+
70
+ # Need to make another call to get the actual value if the custom field type == 'list'
71
+ def option_value
72
+ if option_id
73
+ option_endpoint = "/customFields/#{custom_field_id}/options/#{option_id}"
74
+ option = CustomFieldOption.from_response client.get(option_endpoint)
75
+ option.value
76
+ end
77
+ end
78
+
79
+ private
80
+
81
+ def fields_has_key?(fields, key)
82
+ fields.key?(key.to_s) || fields.key?(key.to_sym)
83
+ end
84
+
85
+ def initialize_fields(fields)
86
+ attributes[:id] = fields['id'] || fields[:id] || attributes[:id]
87
+ attributes[:model_id] = fields['idModel'] || fields[:model_id] || attributes[:model_id]
88
+ attributes[:custom_field_id] = fields['idCustomField'] || fields[:custom_field_id] || attributes[:custom_field_id]
89
+ attributes[:model_type] = fields['modelType'] || fields[:model_type] || attributes[:model_type]
90
+ # Dropdown custom field items do not have a value, they have an ID that maps to
91
+ # a different endpoint to get the value
92
+ attributes[:option_id] = fields['idValue'] || fields[:idValue] if fields_has_key?(fields, 'idValue')
93
+ # value format: { "text": "hello world" }
94
+ attributes[:value] = fields['value'] || fields[:value] if fields_has_key?(fields, 'value')
95
+ self
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,22 @@
1
+ module Trello
2
+ # A custom field option contains the individual items in a custom field dropdown menu.
3
+ #
4
+ class CustomFieldOption < BasicData
5
+ register_attributes :id, :value, :pos, :color,
6
+ readonly: [:id]
7
+ validates_presence_of :id, :value
8
+
9
+ # Update the fields of a custom field option.
10
+ #
11
+ # Supply a hash of string keyed data retrieved from the Trello API representing
12
+ # an item state.
13
+ def update_fields(fields)
14
+ attributes[:id] = fields['_id'] || fields[:id] || attributes[:id]
15
+ attributes[:color] = fields['color'] || fields[:color] || attributes[:color]
16
+ attributes[:pos] = fields['pos'] || fields[:pos] || attributes[:pos]
17
+ # value format: { "text": "hello world" }
18
+ attributes[:value] = fields['value'] || fields[:value] || attributes[:value]
19
+ self
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,12 @@
1
+ module Trello
2
+ class Error < StandardError
3
+
4
+ attr_reader :status_code
5
+
6
+ def initialize(message, status_code=nil)
7
+ @status_code = status_code
8
+ super(message)
9
+ end
10
+
11
+ end
12
+ end
@@ -20,13 +20,13 @@ module Trello
20
20
  # Supply a hash of string keyed data retrieved from the Trello API representing
21
21
  # an item.
22
22
  def update_fields(fields)
23
- attributes[:id] = fields['id']
24
- attributes[:card_id] = fields['idCard']
25
- attributes[:checklist_id] = fields['idChecklist']
26
- attributes[:name] = fields['name']
27
- attributes[:type] = fields['type']
28
- attributes[:state] = fields['state']
29
- attributes[:pos] = fields['pos']
23
+ attributes[:id] = fields['id'] || attributes[:id]
24
+ attributes[:card_id] = fields['idCard'] || attributes[:card_id]
25
+ attributes[:checklist_id] = fields['idChecklist'] || attributes[:checklist_id]
26
+ attributes[:name] = fields['name'] || attributes[:name]
27
+ attributes[:type] = fields['type'] || attributes[:type]
28
+ attributes[:state] = fields['state'] || attributes[:state]
29
+ attributes[:pos] = fields['pos'] || attributes[:pos]
30
30
  self
31
31
  end
32
32
 
@@ -16,9 +16,9 @@ module Trello
16
16
  # Supply a hash of string keyed data retrieved from the Trello API representing
17
17
  # an item state.
18
18
  def update_fields(fields)
19
- attributes[:id] = fields['id']
20
- attributes[:state] = fields['state']
21
- attributes[:item_id] = fields['idCheckItem']
19
+ attributes[:id] = fields['id'] || attributes[:id]
20
+ attributes[:state] = fields['state'] || attributes[:state]
21
+ attributes[:item_id] = fields['idCheckItem'] || attributes[:item_id]
22
22
  self
23
23
  end
24
24
 
@@ -9,7 +9,7 @@ module Trello
9
9
  class Label < BasicData
10
10
  register_attributes :id, :name, :board_id, :uses,
11
11
  readonly: [ :id, :uses, :board_id ]
12
- validates_presence_of :id, :uses, :board_id, :name
12
+ validates_presence_of :id, :board_id, :name
13
13
  validates_length_of :name, in: 1..16384
14
14
 
15
15
  SYMBOL_TO_STRING = {
@@ -23,7 +23,7 @@ module Trello
23
23
  class << self
24
24
  VALID_LABEL_COLOURS = %w{green yellow orange red purple blue sky lime pink black} << ''
25
25
 
26
- # Find a specific card by its id.
26
+ # Find a specific label by its id.
27
27
  def find(id, params = {})
28
28
  client.find(:label, id, params)
29
29
  end
@@ -46,7 +46,7 @@ module Trello
46
46
  define_attribute_methods [:color]
47
47
 
48
48
  def color
49
- @attributes[:color]
49
+ @__attributes[:color]
50
50
  end
51
51
 
52
52
  def color= colour
@@ -55,8 +55,8 @@ module Trello
55
55
  return Trello.logger.warn "The label colour '#{colour}' does not exist."
56
56
  end
57
57
 
58
- self.send(:"color_will_change!") unless colour == @attributes[:color]
59
- @attributes[:color] = colour
58
+ self.send(:"color_will_change!") unless colour == @__attributes[:color]
59
+ @__attributes[:color] = colour
60
60
  end
61
61
 
62
62
  # Update the fields of a label.
@@ -64,12 +64,14 @@ module Trello
64
64
  # Supply a hash of stringkeyed data retrieved from the Trello API representing
65
65
  # a label.
66
66
  def update_fields(fields)
67
- attributes[:id] = fields['id']
68
- attributes[:name] = fields['name'] || fields[:name]
69
- attributes[:color] = fields['color'] || fields[:color]
70
- attributes[:board_id] = fields['idBoard'] || fields[:board_id]
71
- attributes[:uses] = fields['uses']
72
- self
67
+ send('name_will_change!') if fields_has_key?(fields, :name)
68
+ send('color_will_change!') if fields_has_key?(fields, :color)
69
+
70
+ initialize_fields(fields)
71
+ end
72
+
73
+ def initialize(fields = {})
74
+ initialize_fields(fields)
73
75
  end
74
76
 
75
77
  # Returns a reference to the board this label is currently connected.
@@ -95,7 +97,8 @@ module Trello
95
97
  @previously_changed = changes
96
98
  # extract only new values to build payload
97
99
  payload = Hash[changes.map { |key, values| [SYMBOL_TO_STRING[key.to_sym].to_sym, values[1]] }]
98
- @changed_attributes.clear
100
+ @changed_attributes.clear if @changed_attributes.respond_to?(:clear)
101
+ changes_applied if respond_to?(:changes_applied)
99
102
 
100
103
  client.put("/labels/#{id}", payload)
101
104
  end
@@ -104,5 +107,20 @@ module Trello
104
107
  def delete
105
108
  client.delete("/labels/#{id}")
106
109
  end
110
+
111
+ private
112
+
113
+ def fields_has_key?(fields, key)
114
+ fields.key?(SYMBOL_TO_STRING[key]) || fields.key?(key)
115
+ end
116
+
117
+ def initialize_fields(fields)
118
+ attributes[:id] = fields['id'] || attributes[:id]
119
+ attributes[:name] = fields['name'] || fields[:name] || attributes[:name]
120
+ attributes[:color] = fields['color'] || fields[:color] || attributes[:color]
121
+ attributes[:board_id] = fields['idBoard'] || fields[:board_id] || attributes[:board_id]
122
+ attributes[:uses] = fields['uses'] if fields.has_key?('uses')
123
+ self
124
+ end
107
125
  end
108
126
  end