Module: Para::Admin::BaseHelper

Includes:
Para::ApplicationHelper
Included in:
BaseController, FiltersGenerator, FormGenerator, NestedFieldsGenerator, TableGenerator, TreeItemGenerator
Defined in:
app/helpers/para/admin/base_helper.rb

Instance Method Summary collapse

Methods included from Helpers::ResourceName

#resource_name

Instance Method Details

#find_partial_for(relation, partial, partial_dir: 'admin/resources') ⇒ Object



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

def find_partial_for(relation, partial, partial_dir: 'admin/resources')
  relation_class = if model?(relation.class)
    relation = relation.class
  elsif model?(relation)
    relation
  end

  relation_name = find_relation_name_for(
    plural_file_path_for(relation), partial,
    relation_class: relation_class,
    overrides_root: 'admin'
  )

  if relation_name
    "admin/#{ relation_name }/#{ partial }"
  else
    "para/#{ partial_dir }/#{ partial }"
  end
end

#find_relation_name_for(relation, partial, **options) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/helpers/para/admin/base_helper.rb', line 26

def find_relation_name_for(relation, partial, **options)
  return relation if partial_exists?(relation, partial, **options)
  return nil unless options[:relation_class]

  relation = options[:relation_class].ancestors.find do |ancestor|
    next unless model?(ancestor)
    break if ancestor == ActiveRecord::Base

    ancestor_name = plural_file_path_for(ancestor.name)
    partial_exists?(ancestor_name, partial, **options)
  end

  plural_file_path_for(relation) if relation
end

#flash_message(type, resource = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'app/helpers/para/admin/base_helper.rb', line 65

def flash_message(type, resource = nil)
  key = "#{ flash_shared_key }.#{ params[:action] }.#{ type }"

  translation = if resource
    ::I18n.t(key, model: resource.class.model_name.human)
  else
    ::I18n.t(key)
  end

  flash[type] = translation
end

#flash_shared_keyObject



77
78
79
# File 'app/helpers/para/admin/base_helper.rb', line 77

def flash_shared_key
  'para.flash.shared'
end

#registered_components_optionsObject



56
57
58
59
60
61
62
63
# File 'app/helpers/para/admin/base_helper.rb', line 56

def registered_components_options
  Para::Component.registered_components.keys.map do |identifier|
    [
      t("para.component.#{ identifier }.name", default: identifier.to_s.humanize),
      identifier
    ]
  end
end

#resource_title_for(resource) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'app/helpers/para/admin/base_helper.rb', line 46

def resource_title_for(resource)
  if resource.new_record?
    t('para.form.shared.new.title', model: resource.class.model_name.human)
  else
    resource.try(:title).presence ||
      resource.try(:name).presence ||
      t('para.form.shared.edit.title', model: resource.class.model_name.human)
  end
end

#template_path_lookup(*paths) ⇒ Object



41
42
43
44
# File 'app/helpers/para/admin/base_helper.rb', line 41

def template_path_lookup(*paths)
  path = paths.find { |path| lookup_context.find_all(path).any? }
  path&.gsub(/\/_([^\/]+)\z/, '/\1')
end