Sha256: 4b758e51da424c751522a3beac9e91d4d06b1134a7e02916ef0bd24debebca83
Contents?: true
Size: 1.4 KB
Versions: 10
Compression:
Stored size: 1.4 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.temp { ... }. # # 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
10 entries across 10 versions & 1 rubygems