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.



41
42
43
44
45
46
47
48
# File 'app/helpers/administrate/application_helper.rb', line 41

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



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

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

#dashboard_from_resource(resource_name) ⇒ Object



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

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

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



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

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

#model_from_resource(resource_name) ⇒ Object



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

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
# File 'app/helpers/administrate/application_helper.rb', line 10

def render_field(field, locals = {})
  locals[:field] = field
  render locals: locals, partial: field.to_partial_path
end

#requireness(field) ⇒ Object



15
16
17
# File 'app/helpers/administrate/application_helper.rb', line 15

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

#resource_index_route(resource_name) ⇒ Object



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

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

#sanitized_order_params(page, current_field_name) ⇒ Object



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

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



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

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