Class: Binda::Shopify::Installer

Inherits:
Object
  • Object
show all
Defined in:
lib/binda/shopify/installer.rb

Instance Method Summary collapse

Instance Method Details

#create_item_structure(structure_name, name) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/binda/shopify/installer.rb', line 27

def create_item_structure structure_name, name
  structure_slug = name.parameterize
  structure = ::Binda::Structure.find_or_create_by name: name, slug: structure_slug, instance_type: 'component', has_categories: false
  ::Binda::Shopify::STRUCTURES[structure_name.to_sym].each do |field_group_slug, fields|
    field_group = structure.field_groups.find_by slug: "#{structure_slug}-#{field_group_slug}"
    if field_group.nil?
      field_group = structure.field_groups.create! name: field_group_slug.titleize, slug: "#{structure_slug}-#{field_group_slug}"
    end
    fields.each do |name_and_type, mapping|
      name, type = name_and_type.split(':')
      type ||= 'string'
      humanized_name = name.humanize(keep_id_suffix: true).gsub('-', ' ')
      field_group.field_settings.create! name: humanized_name, field_type: type, read_only: true
    end
    field_group.save
  end

  structure
end

#create_settings_board(values) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/binda/shopify/installer.rb', line 4

def create_settings_board values
  raise ArgumentError.new 'You must provide an array or hash of settings values' unless values.is_a?(Array) || values.is_a?(Hash)
  values = Hash[(CONNECTION_KEYS+STRUCTURES.keys).zip(values)] if values.is_a? Array
  structure = ::Binda::Structure.find_or_create_by( name: 'Shopify Settings', slug: 'shopify-settings', instance_type: 'board' )
  board = structure.board
  field_settings = structure.field_groups.first.field_settings
  CONNECTION_KEYS.each do |field_name|
    field_value = values[field_name]
    slug = [structure.slug, field_name.to_s.parameterize].join('-').gsub('_', '-')
    field = field_settings.find_or_create_by( name: field_name.to_s.titleize, slug: slug, field_type: 'string' )
    board.strings.find_or_create_by( field_setting_id: field.id ).update_attribute('content', field_value.strip)
  end

  STRUCTURES.keys.each do |structure_name|
    field_value = values[structure_name]
    slug = [structure.slug, structure_name.to_s.parameterize].join('-').gsub('_', '-')
    field = field_settings.find_or_create_by( name: structure_name.to_s.titleize, slug: slug, field_type: 'string' )
    board.strings.find_or_create_by( field_setting_id: field.id ).update_attribute('content', field_value.strip)
  end

  board
end