Module: DiscoApp::ApplicationHelper

Defined in:
app/helpers/disco_app/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#errors_to_react(model) ⇒ Object

Helper method that provides detailed error information from an active record as JSON



60
61
62
63
64
65
66
# File 'app/helpers/disco_app/application_helper.rb', line 60

def errors_to_react(model)
  {
    type: model.model_name.human.downcase,
    errors: model.errors.keys,
    messages: model.errors.full_messages
  }.as_json
end

Provide link to dynamically add a new nested fields association



36
37
38
39
40
41
42
43
# File 'app/helpers/disco_app/application_helper.rb', line 36

def link_to_add_fields(name, form, association)
  new_object = form.object.send(association).klass.new
  id = new_object.object_id
  fields = form.fields_for(association, new_object, child_index: id) do |builder|
    render(association.to_s.singularize + '_fields', f: builder)
  end
  link_to(name, '#', class: 'add_fields', data: { id: id, fields: fields.delete("\n") })
end

Generate a link that will open its href in an embedded Shopify modal.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/helpers/disco_app/application_helper.rb', line 15

def link_to_modal(name, path, options = {})
  modal_options = {
    src: path,
    title: options.delete(:modal_title),
    width: options.delete(:modal_width),
    height: options.delete(:modal_height),
    buttons: options.delete(:modal_buttons)
  }
  options[:onclick] = "ShopifyApp.Modal.open(#{modal_options.to_json}); return false;"
  options[:onclick].gsub!(/"function(.*?)"/, 'function\1')
  link_to(name, path, options)
end

Generates a link pointing to an object (such as an order or customer) inside the given shop’s Shopify admin. This helper makes it easy to create links to objects within the admin that support both right-clicking and opening in a new tab as well as capturing a left click and redirecting to the relevant object using ‘ShopifyApp.redirect()`.



8
9
10
11
12
# File 'app/helpers/disco_app/application_helper.rb', line 8

def link_to_shopify_admin(shop, name, admin_path, options = {})
  options[:onclick] = "ShopifyApp.redirect('#{admin_path}'); return false;"
  options[:'data-no-turbolink'] = true
  link_to(name, "https://#{shop.shopify_domain}/admin/#{admin_path}", options)
end

#model_form_props(model) ⇒ Object

Return the props required to instantiate a React ModelForm component for the given model instance.



47
48
49
50
51
52
53
54
55
56
57
# File 'app/helpers/disco_app/application_helper.rb', line 47

def model_form_props(model)
  {
    model: model,
    modelTitle: model.persisted? ? model.to_s : "New #{model.model_name.human.downcase}",
    modelName: model.model_name.singular,
    modelUrl: model.persisted? ? send("#{model.model_name.singular}_path", model) : nil,
    modelsUrl: send("#{model.model_name.plural}_path"),
    authenticityToken: form_authenticity_token,
    errors: errors_to_react(model)
  }.as_json
end

#react_component_with_content(name, args = {}, options = {}, &block) ⇒ Object

Render a React component with inner HTML content. Thanks to github.com/reactjs/react-rails/pull/166#issuecomment-86178980



30
31
32
33
# File 'app/helpers/disco_app/application_helper.rb', line 30

def react_component_with_content(name, args = {}, options = {}, &block)
  args[:__html] = capture(&block) if block.present?
  react_component(name, args, options)
end