Sha256: 98f63704675812a7dbe0941774b369aceb288c39a9c062a91be555dd97dbc4ef

Contents?: true

Size: 1.07 KB

Versions: 6

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

module JsonbAccessor
  module Helpers
    module_function

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

    # 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

    # 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)
      return value if value.blank?

      if attribute_type == :datetime
        value = if active_record_default_timezone == :utc
                  Time.find_zone("UTC").parse(value).in_time_zone
                else
                  Time.zone.parse(value)
                end
      end

      value
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
jsonb_accessor-1.3.10 lib/jsonb_accessor/helpers.rb
jsonb_accessor-1.3.10-java lib/jsonb_accessor/helpers.rb
jsonb_accessor-1.3.8 lib/jsonb_accessor/helpers.rb
jsonb_accessor-1.3.7-java lib/jsonb_accessor/helpers.rb
jsonb_accessor-1.3.7 lib/jsonb_accessor/helpers.rb
jsonb_accessor-1.3.6 lib/jsonb_accessor/helpers.rb