Module: Administrate::ApplicationHelper

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

Constant Summary collapse

PLURAL_MANY_COUNT =
2.1

Instance Method Summary collapse

Instance Method Details

#class_from_resource(resource_name) ⇒ Object



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

def class_from_resource(resource_name)
  resource_name.to_s.classify.constantize
end

#clear_search_paramsObject



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

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

#display_resource_name(resource_name) ⇒ Object



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

def display_resource_name(resource_name)
  class_from_resource(resource_name).
    model_name.
    human(
      count: PLURAL_MANY_COUNT,
      default: resource_name.to_s.pluralize.titleize,
    )
end

#gravatar_for_email(email, size = 120) ⇒ Object



7
8
9
10
11
# File 'app/helpers/administrate/application_helper.rb', line 7

def gravatar_for_email(email, size = 120)
  hash = Digest::MD5.hexdigest(email)

  "//www.gravatar.com/avatar/#{hash}?s=#{size}"
end

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



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

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

#resource_index_route_key(resource_name) ⇒ Object



39
40
41
# File 'app/helpers/administrate/application_helper.rb', line 39

def resource_index_route_key(resource_name)
  ActiveModel::Naming.route_key(class_from_resource(resource_name))
end

#sanitized_order_params(page, current_field_name) ⇒ Object



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

def sanitized_order_params(page, current_field_name)
  collection_names = page.association_includes + [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



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

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