Module: Administrate::ApplicationHelper

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

Constant Summary collapse

PLURAL_MANY_COUNT =
2.1
SINGULAR_COUNT =
1

Instance Method Summary collapse

Instance Method Details

#accessible_action?(target, action_name) ⇒ Boolean

Unification of existing_action? and authorized_action?

Parameters:

  • target (ActiveRecord::Base, Class, Symbol, String)

    A resource, a class of resources, or the name of a class of resources.

  • action_name (String, Symbol)

    The name of an action that might be possible to perform on a resource or resource class.

Returns:

  • (Boolean)

    Whether the action both (a) exists for the record class, and (b) the current user is authorized to perform it on the record instance or class.



49
50
51
52
53
54
55
56
# File 'app/helpers/administrate/application_helper.rb', line 49

def accessible_action?(target, action_name)
  target = target.to_sym if target.is_a?(String)
  target_class_or_class_name =
    target.is_a?(ActiveRecord::Base) ? target.class : target

  existing_action?(target_class_or_class_name, action_name) &&
    authorized_action?(target, action_name)
end

#application_titleObject



6
7
8
# File 'app/helpers/administrate/application_helper.rb', line 6

def application_title
  Rails.application.class.module_parent_name.titlecase
end

#clear_search_paramsObject



88
89
90
91
92
# File 'app/helpers/administrate/application_helper.rb', line 88

def clear_search_params
  params.except(:search, :_page).permit(
    :per_page, resource_name => %i[order direction]
  )
end

#dashboard_from_resource(resource_name) ⇒ Object



27
28
29
# File 'app/helpers/administrate/application_helper.rb', line 27

def dashboard_from_resource(resource_name)
  "#{resource_name.to_s.singularize}_dashboard".classify.constantize
end

#display_resource_name(resource_name, opts = {}) ⇒ Object



58
59
60
61
62
63
# File 'app/helpers/administrate/application_helper.rb', line 58

def display_resource_name(resource_name, opts = {})
  dashboard_from_resource(resource_name).resource_name(
    count: opts[:singular] ? SINGULAR_COUNT : PLURAL_MANY_COUNT,
    default: default_resource_name(resource_name, opts)
  )
end

#find_partial_prefix(field) ⇒ Object



17
18
19
20
21
# File 'app/helpers/administrate/application_helper.rb', line 17

def find_partial_prefix(field)
  field.partial_prefixes.detect do |prefix|
    lookup_context.template_exists?(field.page, [prefix], true)
  end
end

#model_from_resource(resource_name) ⇒ Object



31
32
33
34
# File 'app/helpers/administrate/application_helper.rb', line 31

def model_from_resource(resource_name)
  dashboard = dashboard_from_resource(resource_name)
  dashboard.try(:model) || resource_name.to_sym
end

#render_field(field, locals = {}) ⇒ Object



10
11
12
13
14
15
# File 'app/helpers/administrate/application_helper.rb', line 10

def render_field(field, locals = {})
  locals[:field] = field
  if (prefix = find_partial_prefix(field))
    render locals: locals, partial: "#{prefix}/#{field.page}"
  end
end

#requireness(field) ⇒ Object



23
24
25
# File 'app/helpers/administrate/application_helper.rb', line 23

def requireness(field)
  field.required? ? "required" : "optional"
end

#resource_index_route(resource_name) ⇒ Object



73
74
75
76
77
78
# File 'app/helpers/administrate/application_helper.rb', line 73

def resource_index_route(resource_name)
  url_for(
    action: "index",
    controller: "/#{namespace}/#{resource_name}"
  )
end

#sanitized_order_params(page, current_field_name) ⇒ Object



80
81
82
83
84
85
86
# File 'app/helpers/administrate/application_helper.rb', line 80

def sanitized_order_params(page, current_field_name)
  collection_names = page.item_associations + [current_field_name]
  association_params = collection_names.map do |assoc_name|
    {assoc_name => %i[order direction page per_page]}
  end
  params.permit(:search, :id, :_page, :per_page, association_params)
end

#sort_order(order) ⇒ Object



65
66
67
68
69
70
71
# File 'app/helpers/administrate/application_helper.rb', line 65

def sort_order(order)
  case order
  when "asc" then "ascending"
  when "desc" then "descending"
  else "none"
  end
end