Module: TinyAdmin::Utils

Included in:
Actions::BasicAction, BasicApp, Store, Views::BasicLayout
Defined in:
lib/tiny_admin/utils.rb

Instance Method Summary collapse

Instance Method Details

#humanize(string) ⇒ Object



42
43
44
45
46
# File 'lib/tiny_admin/utils.rb', line 42

def humanize(string)
  return '' unless string

  string.respond_to?(:humanize) ? string.humanize : string.tr('_', ' ').capitalize
end

#params_to_s(params) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/tiny_admin/utils.rb', line 5

def params_to_s(params)
  list = params.each_with_object([]) do |(param, value), result|
    if value.is_a?(Hash)
      values = value.map { |key, val| "#{param}[#{key}]=#{val}" }
      result.concat(values)
    else
      result.push(["#{param}=#{value}"])
    end
  end
  list.join('&')
end

#prepare_page(page_class, slug: nil, attributes: nil, options: nil, params: nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tiny_admin/utils.rb', line 17

def prepare_page(page_class, slug: nil, attributes: nil, options: nil, params: nil)
  page_class.new.tap do |page|
    page.options = options
    page.head_component = TinyAdmin.settings.components[:head]&.new
    page.flash_component = TinyAdmin.settings.components[:flash]&.new
    page.navbar_component = TinyAdmin.settings.components[:navbar]&.new
    page.navbar_component&.update_attributes(
      current_slug: slug,
      root_path: TinyAdmin.settings.root_path,
      root_title: TinyAdmin.settings.root[:title],
      items: options&.include?(:no_menu) ? [] : TinyAdmin.settings.store&.navbar
    )
    attrs = attributes || {}
    attrs[:params] = params if params
    attrs[:widgets] = attrs[:widgets].map { to_class(_1) } if attrs[:widgets]
    page.update_attributes(attrs) unless attrs.empty?
    yield(page) if block_given?
    page.setup if page.respond_to?(:setup)
  end
end

#to_class(klass) ⇒ Object



38
39
40
# File 'lib/tiny_admin/utils.rb', line 38

def to_class(klass)
  klass.is_a?(String) ? Object.const_get(klass) : klass
end