module CustomFields::Types::Json::Target

Protected Instance Methods

add_json_parsing_error(name) click to toggle source
# File lib/custom_fields/types/json.rb, line 71
def add_json_parsing_error(name)
  error = instance_variable_get(:"@#{name}_json_parsing_error")

  return unless error

  msg = "Invalid #{name}: \"#{error}\". Check it out on http://jsonlint.com"
  errors.add(name, msg)
end
decode_json(name, json) click to toggle source
# File lib/custom_fields/types/json.rb, line 57
def decode_json(name, json)
  value = json.respond_to?(:to_str) && !json.blank? ? ActiveSupport::JSON.decode(URI.decode_www_form_component(json)) : json
  value = nil if json.blank?

  # Only hashes are accepted
  raise ActiveSupport::JSON.parse_error, 'Only a Hash object is accepted' if value && !value.is_a?(Hash)

  instance_variable_set(:"@#{name}_json_parsing_error", nil)
  value
rescue ActiveSupport::JSON.parse_error
  instance_variable_set(:"@#{name}_json_parsing_error", $ERROR_INFO.message)
  nil
end