Created
August 20, 2012 08:23
-
-
Save jhjguxin/3402207 to your computer and use it in GitHub Desktop.
seo and breadcrumbs defined demo base on bbtang.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% 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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment