From 58c7ca0ae7177bcff5652df15472332af9d28bd4 Mon Sep 17 00:00:00 2001 From: JGutierrezC Date: Sat, 10 Sep 2016 11:23:41 -0500 Subject: [PATCH 1/2] Fix for multi select custom fields when saving For a multi_select field type, amoCRM demands to be a direct array instead of a values: { [value: '1']}. This commit fixes the way it's sent --- lib/amorail/entity/params.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/amorail/entity/params.rb b/lib/amorail/entity/params.rb index 2de76e7..0d7ad65 100644 --- a/lib/amorail/entity/params.rb +++ b/lib/amorail/entity/params.rb @@ -17,11 +17,10 @@ def custom_fields props = properties.send(self.class.amo_name) custom_fields = [] - - self.class.properties.each do |k, v| + self.class.properties.each do |k, value| prop_id = props.send(k).id - prop_val = { value: send(k) }.merge(v) - custom_fields << { id: prop_id, values: [prop_val] } + prop_val = props.send(k)['type_id'] == "5" ? [send(k)] : { value: send(k) }.merge(value) + custom_fields << { id: prop_id, values: [prop_val].flatten } end custom_fields @@ -41,7 +40,10 @@ def create_params(method) def normalize_custom_fields(val) val.reject do |field| - field[:values].all? { |item| !item[:value] } + next if field[:values].instance_of? Array + field[:values].all? { |item| + !item[:value] + } end end From 3bc59824b6aa7cd75a355559c8207522477f865a Mon Sep 17 00:00:00 2001 From: JGutierrezC Date: Wed, 14 Sep 2016 09:26:34 -0500 Subject: [PATCH 2/2] Using constant for multiselect type --- lib/amorail/entity/params.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/amorail/entity/params.rb b/lib/amorail/entity/params.rb index 0d7ad65..26433ff 100644 --- a/lib/amorail/entity/params.rb +++ b/lib/amorail/entity/params.rb @@ -1,4 +1,7 @@ module Amorail # :nodoc: all + + MULTISELECT_FIELD_TYPE = '5' + class Entity def params data = {} @@ -19,7 +22,7 @@ def custom_fields custom_fields = [] self.class.properties.each do |k, value| prop_id = props.send(k).id - prop_val = props.send(k)['type_id'] == "5" ? [send(k)] : { value: send(k) }.merge(value) + prop_val = props.send(k)['type_id'] == MULTISELECT_FIELD_TYPE ? [send(k)] : { value: send(k) }.merge(value) custom_fields << { id: prop_id, values: [prop_val].flatten } end