Skip to content

Instantly share code, notes, and snippets.

@jhjguxin
Created August 20, 2012 08:23
Show Gist options
  • Save jhjguxin/3402207 to your computer and use it in GitHub Desktop.
Save jhjguxin/3402207 to your computer and use it in GitHub Desktop.
seo and breadcrumbs defined demo base on bbtang.com
<% if title = get_seo_info("title") %>
<title><%= "#{title} #{ @default_title}" %></title>
<% else %>
<title><%= "#{ @default_title}" %></title>
<% end %>
<% if keywords = get_seo_info("keywords") %>
<meta name="keywords" content="<%= keywords %>">
<% else %>
<meta name="keywords" content="<%= Askjane::MetaCache.get_config_data("default_meta_keywords") %>" />
<% end %>
<% if description = get_seo_info("description") %>
<meta name="description" content="<%= description %>" />
<% else %>
<meta name="description" content="<%= Askjane::MetaCache.get_config_data("default_meta_description") %>" />
<% end %>
# encoding: utf-8
class ApplicationController < ActionController::Base
before_filter :add_initial_breadcrumbs
before_filter :add_model_breadcrumbs
before_filter :set_seo_meta
def set_seo_meta(title = '',meta_keywords = '', meta_description = '')
g_obj = guessed_object
if g_obj.present?
[:title, :name, :nickname].each do |col|
#@meta_title = g_obj.send col if g_obj.respond_to? col
if (g_obj.respond_to? col)
translations = I18n.backend.send(:translations)
if translations[:"zh-CN"][:helpers][:titles]["#{params[:action]}".to_sym].present?
@meta_title = I18n.t("helpers.titles.#{params[:action]}",:model => (g_obj.send col))
else
@meta_title = g_obj.send col
end
end
end
@meta_keywords = g_obj.tag_list.join(",") if g_obj.respond_to? :tag_list
@meta_description = g_obj.summary if g_obj.respond_to? :summary
@meta_description = g_obj.description if g_obj.respond_to? :description
end
if title.present?
@meta_title = "#{title}"
end
@meta_keywords = meta_keywords if meta_keywords.present?
@meta_description = meta_description if meta_description.present?
end
def guessed_object
@model_class = params[:controller].classify.constantize if params[:controller].classify.class_exists?
@model_class = params[:controller].gsub(/\w+([\/]+)/,"").classify.constantize if params[:controller].gsub(/\w+([\/]+)/,"").classify.class_exists?
if @model_class.present?
@model_class.where(id: params[:id]).first if params[:id].present?
#guessed_instance_variable_name = "@#{@model_class.name.singularize.downcase}"
#if self.instance_variable_defined? guessed_instance_variable_name
# self.instance_variable_get guessed_instance_variable_name
#end
end
end
def add_initial_breadcrumbs
breadcrumbs.add :home, root_path
end
def add_model_breadcrumbs
model_class = params[:controller].gsub(/\w+([\/]+)/,"").classify.constantize if params[:controller].gsub(/\w+([\/]+)/,"").classify.class_exists?
if model_class.present?
controller = params[:controller].gsub('/','_') if params[:controller].present?
if model_class.model_name.singularize.eql? model_class.model_name.pluralize
controller += "_index" if params[:controller].present?
end
route_name = "#{controller}_path"
breadcrumbs.add I18n.t("mod.#{controller}",opt = {}), self.send(route_name) if self.respond_to? route_name
end
end
end
# encoding: utf-8
module ApplicationHelper
# 获取seo配置
def get_seo_info(type_name)
@default_title = "#{breadcrumbs.items.collect{|item| item.first}.reverse.join('_')}".gsub("_首页","|#{Setting.app_name}")
@default_title = "|#{Setting.app_name}" if @default_title.eql? "首页"
name = @controller.to_s.downcase + "#" + @action.to_s.downcase
if seo = Rails.configuration.self_settings["seos"][name]
seo = JSON.parse(seo)
return seo[type_name]
else
return self.instance_variable_get "@meta_#{type_name}"
end
end
end
# encoding: utf-8
"zh-CN":
mod:
albums: "相册"
answers: "回答"
knowledges: "自学当妈"
questions: "有问有答"
photos: "照片"
subjects: "专题"
topics: "主题"
people: "个人主页"
notes: "个人日志"
profiles: "个人资料"
breadcrumbs:
home: "首页"
registration: "注册"
searchs: "搜索"
tag: "标签"
first: "First"
second: "Second"
third: "Third"
people:
show: "%{user}的个人主页"
knowledges: "%{user}的个人收藏"
questions: "%{user}的问答"
followings: "%{user}关注的人"
followers: "%{user}的粉丝"
mycomments: "关于%{user}的评论"
add_comm: "为%{user}添加留言"
designateds: "%{user}的邀请回答"
notes:
index: "%{user}的日志"
new: "新增日志"
show: "浏览%{note}"
albums:
index: "%{user}的个人相册"
photos:
new: "添加照片"
show: "浏览%{photo}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment