Module: Admin::ComponentsHelper

Defined in:
app/helpers/admin/components_helper.rb

Constant Summary collapse

HIDDEN_FIELD_RE =
/\b(?<=name=")[^"]+(?=")/.freeze

Instance Method Summary collapse

Instance Method Details

#association_picker_field(form, collection, path:, name:, allow_create: false, create_name: nil, allow_multiple: true) ⇒ Object

rubocop:disable Metrics/ParameterLists



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/helpers/admin/components_helper.rb', line 28

def association_picker_field(form, collection, path:, name:, allow_create: false, create_name: nil,
                             allow_multiple: true)
  path = path.to_s
  collection = collection ? Array.wrap(collection) : []

  scoped_name = form.hidden_field(name).scan(HIDDEN_FIELD_RE)[0]

  scoped_create_name = scoped_name.gsub(name.to_s, create_name.to_s) if create_name

  component = react_component(
    'admin/association-picker/AssociationPicker',
    name: scoped_name,
    create_name: scoped_create_name,
    association: path,
    items: collection.map { |i| { id: i.id, name: i.name } },
    allow_create: allow_create,
    allow_multiple: allow_multiple
  )
  (:div) do
    [
      concat((:label, path.titleize, for: path)),
      concat((:div, component, id: path))
    ]
  end
end

#directory_picker_props(resource) ⇒ Object

rubocop:enable Metrics/ParameterLists



55
56
57
58
59
60
61
62
63
64
65
# File 'app/helpers/admin/components_helper.rb', line 55

def directory_picker_props(resource)
  {
    directory: resource.directory,
    parent: {
      directory: resource.parent.try(:directory) || [],
      id: resource.parent_id,
      title: resource.parent.try(:title)
    },
    path: lcms_engine_url_helpers.children_admin_curriculum_path
  }
end

#resource_picker_field(form, collection, path:, name:, allow_multiple: true) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/helpers/admin/components_helper.rb', line 7

def resource_picker_field(form, collection, path:, name:, allow_multiple: true)
  path = path.to_s

  base_name = form.hidden_field(name).scan(HIDDEN_FIELD_RE)[0]
  computed_name = "#{base_name}[]"

  component = react_component(
    'admin/resource-picker/ResourcePicker',
    name: computed_name,
    resources: collection.map { |i| { id: i.id, title: i.title } },
    allow_multiple: allow_multiple
  )
  (:div) do
    [
      concat((:label, path.titleize, for: path)),
      concat((:div, component, id: path))
    ]
  end
end