module JsonbAccessor::Helpers

def active_record_default_timezone

def active_record_default_timezone
  ActiveRecord.try(:default_timezone) || ActiveRecord::Base.default_timezone
end

def convert_keys_to_store_keys(attributes, store_key_mapping)

Replaces all keys in `attributes` that have a defined store_key with the store_key
def convert_keys_to_store_keys(attributes, store_key_mapping)
  attributes.stringify_keys.transform_keys do |key|
    store_key_mapping[key] || key
  end
end

def convert_store_keys_to_keys(attributes, store_key_mapping)

Replaces all keys in `attributes` that have a defined store_key with the named key (alias)
def convert_store_keys_to_keys(attributes, store_key_mapping)
  convert_keys_to_store_keys(attributes, store_key_mapping.invert)
end

def deserialize_value(value, attribute_type)

def deserialize_value(value, attribute_type)
  return value if value.blank?
  if attribute_type == :datetime
    value = if value.is_a?(Array)
              value.map { |v| parse_date(v) }
            else
              parse_date(value)
            end
  end
  value
end

def parse_date(datetime)

Parse datetime based on the configured default_timezone
def parse_date(datetime)
  if active_record_default_timezone == :utc
    Time.find_zone("UTC").parse(datetime).in_time_zone
  else
    Time.zone.parse(datetime)
  end
end