Skip to content

Instantly share code, notes, and snippets.

@krtschmr
Created July 25, 2015 14:43
Show Gist options
  • Save krtschmr/1cafd73bc6278b728bf0 to your computer and use it in GitHub Desktop.
Save krtschmr/1cafd73bc6278b728bf0 to your computer and use it in GitHub Desktop.
asd
require "titlefy/version"
module Titlefy
# call from your controller (mostly ApplicationController)
def titlefy
send :include, InstanceMethods
end
module InstanceMethods
def render(*args)
set_title_tag if (!!!@title)
super
end
def set_title_tag
translations = I18n.backend.send(:translations)
title_tags = translations[I18n.locale][:title_tags]
# en:
# default: My Awesome Webpage
#
# list_items_path: Index of Items
# order_path: Order at our Webshop
#
# files_controller:
# action_name: This is a File
#
# # for Admin::PagesController
#
# admin:
# file_controller:
# action_name: Edit a File | Admin
title = nil
title = title_tags[namespace][controller_name.to_sym][action_name.to_sym] rescue nil unless title
title = title_tags[controller_name.to_sym][action_name.to_sym] rescue nil unless title
title = title_tags[route_name.to_sym] rescue nil unless title
raise namespace.inspect
@title = title || default_title
end
def add_title(title)
@title = title
end
def app_name
I18n.translate "app.name".to_sym, default: Rails.application.class.parent_name
end
def namespace
self.class.parent.to_s.downcase
end
def default_title
title = title_tags[:default] rescue app_name
end
def route_name
Rails.application.routes.router.recognize(request) do |route, _|
return route.name.to_s.split("_")[0..1].join("_")
end
end
end
end
ActiveRecord::Base.extend Titlefy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment