Module: AutomaticPageTitles::PageTitleHelper

Defined in:
lib/automatic_page_titles/page_title_helper.rb

Instance Method Summary collapse

Instance Method Details

#admin_prefixObject



77
78
79
80
81
# File 'lib/automatic_page_titles/page_title_helper.rb', line 77

def admin_prefix
  current_path = request.try :path
  return '' unless current_path.to_s.match?(%r{\/admin\/})
  "Administrator - "
end

#automatic_variable_titleObject



65
66
67
68
69
70
# File 'lib/automatic_page_titles/page_title_helper.rb', line 65

def automatic_variable_title
  return unless controller && controller_name
  var_name = "@" + controller_name.singularize
  variable = controller.instance_variable_get(var_name)
  variable.try(:title) || variable.try(:name)
end

#controller_titleObject



33
34
35
# File 'lib/automatic_page_titles/page_title_helper.rb', line 33

def controller_title
  controller_name.titleize
end

#controller_title_singularObject



37
38
39
# File 'lib/automatic_page_titles/page_title_helper.rb', line 37

def controller_title_singular
  controller_title.singularize
end

#current_pathObject



55
56
57
# File 'lib/automatic_page_titles/page_title_helper.rb', line 55

def current_path
  request.try :path
end

#custom_action_page_titleObject



25
26
27
28
29
30
31
# File 'lib/automatic_page_titles/page_title_helper.rb', line 25

def custom_action_page_title
  if params[:id].present?
    "#{action_name.titleize} #{controller_title_singular}#{padded_automatic_variable_title}"
  else
    "#{action_name.titleize} #{controller_title}"
  end
end

#default_page_titleObject



51
52
53
# File 'lib/automatic_page_titles/page_title_helper.rb', line 51

def default_page_title
  Rails.application.class.to_s.split("::").first.underscore.humanize
end

#padded_automatic_variable_titleObject



72
73
74
75
# File 'lib/automatic_page_titles/page_title_helper.rb', line 72

def padded_automatic_variable_title
  return "" if automatic_variable_title.blank?
  ": #{automatic_variable_title}"
end

#page_title(page_title = nil) ⇒ Object



3
4
5
# File 'lib/automatic_page_titles/page_title_helper.rb', line 3

def page_title(page_title = nil)
  "#{admin_prefix}#{page_title_no_admin_prefix(page_title)}"
end

#page_title_no_admin_prefix(page_title = nil) ⇒ Object



7
8
9
10
# File 'lib/automatic_page_titles/page_title_helper.rb', line 7

def page_title_no_admin_prefix(page_title = nil)
  @page_title = page_title unless page_title.nil?
  @page_title || restful_page_title || custom_action_page_title
end

#path_to_page_title(path = current_path) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/automatic_page_titles/page_title_helper.rb', line 42

def path_to_page_title(path = current_path)
  title = path.to_s.split("/").last
  return nil if title.nil?

  title = title.gsub(/[-_]/, " ")
  title = title.gsub(/\s+/, " ").strip
  title.humanize
end

#path_without_last_segment(path) ⇒ Object



59
60
61
62
63
# File 'lib/automatic_page_titles/page_title_helper.rb', line 59

def path_without_last_segment(path)
  path  = path.to_s
  index = path.rindex("/")
  index ? path[0, index] : nil
end

#restful_page_titleObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/automatic_page_titles/page_title_helper.rb', line 12

def restful_page_title
  case action_name
  when "index"
    controller_title
  when "show"
    "#{controller_title_singular}#{padded_automatic_variable_title}"
  when "edit", "update"
    "Edit #{controller_title_singular}#{padded_automatic_variable_title}"
  when "new", "create"
    "New #{controller_title_singular}"
  end
end