Sha256: 9384a2d3366994af402a2d24ed1ae1fd3cf9f398abc4e5b2ba89baceaa2c4de8

Contents?: true

Size: 1.41 KB

Versions: 26

Compression:

Stored size: 1.41 KB

Contents

module DiscoApp::Concerns::HasMetafields
  extend ActiveSupport::Concern

  included do

    # Write multiple metafields for this object in a single call.
    #
    # Expects an argument in a nested hash structure with :namespace => :key =>
    # :value, eg:
    #
    #   Product.write_metafields(myapp: {
    #      key1: 'value1',
    #      key2: 'value2'
    #   })
    #
    # This method assumes that it is being called within a valid Shopify API
    # session context, eg @shop.with_api_context { ... }.
    #
    # It also assumes that the including class has defined the appropriate value
    # for SHOPIFY_API_CLASS and that calling the `id` method on the instance
    # will return the relevant object's Shopify ID.
    #
    # Returns true on success, false otherwise.
    def write_metafields(metafields)
      self.class::SHOPIFY_API_CLASS.new(
        id: id,
        metafields: build_metafields(metafields)
      ).save
    end

    # Give a nested hash of metafields in the format described above, return
    # an array of corresponding ShopifyAPI::Metafield instances.
    def build_metafields(metafields)
      metafields.map do |namespace, keys|
        keys.map do |key, value|
          ShopifyAPI::Metafield.new(
            namespace: namespace,
            key: key,
            value: value,
            value_type: value.is_a?(Integer) ? :integer : :string
          )
        end
      end.flatten
    end

  end

end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
disco_app-0.16.1.pre.sidekiq.pre.6.pre.release app/models/disco_app/concerns/has_metafields.rb
disco_app-0.12.0 app/models/disco_app/concerns/has_metafields.rb
disco_app-0.12.1 app/models/disco_app/concerns/has_metafields.rb
disco_app-0.12.5 app/models/disco_app/concerns/has_metafields.rb
disco_app-0.12.6 app/models/disco_app/concerns/has_metafields.rb
disco_app-0.12.7 app/models/disco_app/concerns/has_metafields.rb
disco_app-0.13.0 app/models/disco_app/concerns/has_metafields.rb
disco_app-0.13.1 app/models/disco_app/concerns/has_metafields.rb
disco_app-0.13.2 app/models/disco_app/concerns/has_metafields.rb
disco_app-0.13.3 app/models/disco_app/concerns/has_metafields.rb
disco_app-0.13.4 app/models/disco_app/concerns/has_metafields.rb
disco_app-0.13.5 app/models/disco_app/concerns/has_metafields.rb
disco_app-0.13.6 app/models/disco_app/concerns/has_metafields.rb
disco_app-0.13.7 app/models/disco_app/concerns/has_metafields.rb
disco_app-0.13.8 app/models/disco_app/concerns/has_metafields.rb
disco_app-0.14.1 app/models/disco_app/concerns/has_metafields.rb
disco_app-0.14.2 app/models/disco_app/concerns/has_metafields.rb
disco_app-0.15.0 app/models/disco_app/concerns/has_metafields.rb
disco_app-0.16.0 app/models/disco_app/concerns/has_metafields.rb
disco_app-0.14.4 app/models/disco_app/concerns/has_metafields.rb