Class: ShopifyApp::ScripttagsManager

Inherits:
Object
  • Object
show all
Defined in:
lib/shopify_app/managers/scripttags_manager.rb

Defined Under Namespace

Classes: CreationFailed

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scripttags, shop_domain) ⇒ ScripttagsManager

Returns a new instance of ScripttagsManager.



25
26
27
28
# File 'lib/shopify_app/managers/scripttags_manager.rb', line 25

def initialize(scripttags, shop_domain)
  @required_scripttags = scripttags
  @shop_domain = shop_domain
end

Instance Attribute Details

#required_scripttagsObject (readonly)

Returns the value of attribute required_scripttags.



23
24
25
# File 'lib/shopify_app/managers/scripttags_manager.rb', line 23

def required_scripttags
  @required_scripttags
end

#shop_domainObject (readonly)

Returns the value of attribute shop_domain.



23
24
25
# File 'lib/shopify_app/managers/scripttags_manager.rb', line 23

def shop_domain
  @shop_domain
end

Class Method Details

.build_src(scripttags, domain) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/shopify_app/managers/scripttags_manager.rb', line 14

def self.build_src(scripttags, domain)
  scripttags.map do |tag|
    next tag unless tag[:src].respond_to?(:call)
    tag = tag.dup
    tag[:src] = tag[:src].call(domain)
    tag
  end
end

.queue(shop_domain, shop_token, scripttags) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/shopify_app/managers/scripttags_manager.rb', line 5

def self.queue(shop_domain, shop_token, scripttags)
  ShopifyApp::ScripttagsManagerJob.perform_later(
    shop_domain: shop_domain,
    shop_token: shop_token,
    # Procs cannot be serialized so we interpolate now, if necessary
    scripttags: build_src(scripttags, shop_domain)
  )
end

Instance Method Details

#create_scripttagsObject



35
36
37
38
39
40
41
# File 'lib/shopify_app/managers/scripttags_manager.rb', line 35

def create_scripttags
  return unless required_scripttags.present?

  expanded_scripttags.each do |scripttag|
    create_scripttag(scripttag) unless scripttag_exists?(scripttag[:src])
  end
end

#destroy_scripttagsObject



43
44
45
46
47
48
49
50
# File 'lib/shopify_app/managers/scripttags_manager.rb', line 43

def destroy_scripttags
  scripttags = expanded_scripttags
  ShopifyAPI::ScriptTag.all.each do |tag|
    ShopifyAPI::ScriptTag.delete(tag.id) if is_required_scripttag?(scripttags, tag)
  end

  @current_scripttags = nil
end

#recreate_scripttags!Object



30
31
32
33
# File 'lib/shopify_app/managers/scripttags_manager.rb', line 30

def recreate_scripttags!
  destroy_scripttags
  create_scripttags
end