Module: EPlat::Concerns::Metafieldable

Included in:
Order, Product
Defined in:
lib/e_plat/resource/concerns/metafieldable.rb

Overview

< ActiveResource::CustomMethods

Instance Method Summary collapse

Instance Method Details

#add_metafield(metafield) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/e_plat/resource/concerns/metafieldable.rb', line 27

def add_metafield(metafield)
  raise ArgumentError, "You can only add metafields to a resource that has been saved" if new?

  metafield.owner_id ||= id
  metafield.owner_resource ||= self.class.element_name

  if client.woocommerce?
     << metafield
    save

    .find{ _1.key == metafield.key }.then do |m|
      m.owner_id = id
      m.owner_resource = self.class.element_name
      m.owner = self
      m
    end
  else
    metafield.save
    metafield
  end
end

#find_metafield(id) ⇒ Object



23
24
25
# File 'lib/e_plat/resource/concerns/metafieldable.rb', line 23

def find_metafield(id)
  metafields.find { |m| m.id == id }
end

#metafields(**options) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/e_plat/resource/concerns/metafieldable.rb', line 7

def metafields(**options)
  if client.woocommerce?
    .map do |m|
      m.owner_id ||= id
      m.owner_resource ||= self.class.element_name
      m.owner = self
      m
    end
  else
    Metafield.find(:all,
      from: current_resources_metafield_path, 
      params: options
    ) || []
  end
end