Commit 03601722 by Tran Hoang Viet

VietTH: Add and use gem meta-tags

parent 6d6f1f12
...@@ -94,4 +94,6 @@ gem 'rubocop', '~> 0.32.1' ...@@ -94,4 +94,6 @@ gem 'rubocop', '~> 0.32.1'
gem 'cartman', '~> 2.1.2' gem 'cartman', '~> 2.1.2'
gem 'font-awesome-rails', '~> 4.3.0.0' gem 'font-awesome-rails', '~> 4.3.0.0'
\ No newline at end of file
gem 'meta-tags', '~> 2.0.0'
\ No newline at end of file
...@@ -155,6 +155,8 @@ GEM ...@@ -155,6 +155,8 @@ GEM
nokogiri (>= 1.5.9) nokogiri (>= 1.5.9)
mail (2.6.3) mail (2.6.3)
mime-types (>= 1.16, < 3) mime-types (>= 1.16, < 3)
meta-tags (2.0.0)
actionpack (>= 3.0.0)
method_source (0.8.2) method_source (0.8.2)
mime-types (2.6.1) mime-types (2.6.1)
mini_magick (3.8.1) mini_magick (3.8.1)
...@@ -343,6 +345,7 @@ DEPENDENCIES ...@@ -343,6 +345,7 @@ DEPENDENCIES
jquery-rails jquery-rails
kaminari (~> 0.16.3) kaminari (~> 0.16.3)
letter_opener (~> 1.4.1) letter_opener (~> 1.4.1)
meta-tags (~> 2.0.0)
mini_magick (~> 3.8.1) mini_magick (~> 3.8.1)
mysql2 mysql2
pry-rails (~> 0.3.4) pry-rails (~> 0.3.4)
......
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
include MetaManagement
rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found
layout Proc.new { |controller| controller.devise_controller? ? 'devise' : 'application' } layout Proc.new { |controller| controller.devise_controller? ? 'devise' : 'application' }
...@@ -19,4 +21,5 @@ class ApplicationController < ActionController::Base ...@@ -19,4 +21,5 @@ class ApplicationController < ActionController::Base
def record_not_found def record_not_found
redirect_to(root_path, alert: "Resource not found") redirect_to(root_path, alert: "Resource not found")
end end
end end
\ No newline at end of file
class CategoriesController < ApplicationController class CategoriesController < ApplicationController
before_action :set_category, only: [:show] before_action :set_category, only: [:show]
before_action :set_meta, only: [:show]
before_action :add_breadcrumb_home before_action :add_breadcrumb_home
before_action :set_metas, only: [:show]
def show def show
@products = @category.products.page(params[:page]) @products = @category.products.page(params[:page])
add_breadcrumb(@category.decorate.title) add_breadcrumb(@category.decorate.title)
end end
def meta_attributes
return if @category.blank?
@meta_attributes ||= {
title: @category.decorate.title,
description: @category.decorate.title
}
end
private private
def set_category def set_category
@category = Category.find(params[:id]) @category = Category.find(params[:id])
end end
def set_meta
Meta.title = @category.decorate.title
Meta.name = @category.decorate.title
end
end end
\ No newline at end of file
module MetaManagement extend ActiveSupport::Concern
included do
def render(options = nil, extra_options = {}, &block)
set_metas
super
end
def meta_attributes_default
{
image: ActionController::Base.helpers.asset_url('logo.png'),
title: 'Venshop Zigexn VeNtura',
description: 'Venshop',
keywords: 'Ventura, Zigexn, venshop',
type: 'shop'
}
end
def set_metas
attributes = meta_attributes_default
if self.respond_to?(:meta_attributes) && meta_attributes.present?
attributes = attributes.merge(meta_attributes)
end
set_meta_tags(
title: attributes[:title],
description: attributes[:description],
keywords: attributes[:keywords],
separator: "&mdash;".html_safe,
og: {
title: attributes[:title],
type: attributes[:type],
url: Settings.base_url,
image: attributes[:image],
description: attributes[:description]
},
twitter: {
title: attributes[:title],
description: attributes[:description],
card: attributes[:type],
image: {
_: attributes[:image],
width: 100,
height: 100
}
}
)
end
end
end
\ No newline at end of file
...@@ -3,7 +3,6 @@ class ProductsController < ApplicationController ...@@ -3,7 +3,6 @@ class ProductsController < ApplicationController
before_action :set_categories, only: [:new, :create] before_action :set_categories, only: [:new, :create]
before_action :add_breadcrumb_home before_action :add_breadcrumb_home
before_action :authenticate_user!, only: [:new, :create, :add_cart] before_action :authenticate_user!, only: [:new, :create, :add_cart]
before_action :set_meta, only: [:show]
def new def new
@product = Product.new @product = Product.new
...@@ -39,6 +38,16 @@ class ProductsController < ApplicationController ...@@ -39,6 +38,16 @@ class ProductsController < ApplicationController
add_breadcrumb('Search result') add_breadcrumb('Search result')
end end
def meta_attributes
return if @product.blank?
@meta_attributes ||= {
title: @product.title,
image: @product.image_lg_url,
description: "#{@product.title} - #{@product.price}"
}
end
private private
def set_product def set_product
@product = Product.find(params[:id]) @product = Product.find(params[:id])
...@@ -56,13 +65,4 @@ class ProductsController < ApplicationController ...@@ -56,13 +65,4 @@ class ProductsController < ApplicationController
@cart_service ||= CartService.new(current_user, params) @cart_service ||= CartService.new(current_user, params)
end end
def set_meta
Meta.title = @product.title
Meta.name = @product.title
Meta.image = @product.image_md_url
Meta.image_lg = @product.image_lg_url
Meta.description = "#{@product.title} - #{@product.price}"
Meta.author = @product.user.email
end
end end
\ No newline at end of file
%title= Meta.title || 'Venshop Zigexn VeNtura'
%meta{content: Meta.description, name: "description"}
/ Schema.org markup for Google+
%meta{content: Meta.name, itemprop: "name"}
%meta{content: Meta.description, itemprop: "description"}
%meta{content: Meta.image, itemprop: "image"}
/ Twitter Card data
%meta{content: Meta.image_lg, name: "twitter:card"}
%meta{content: Meta.title, name: "twitter:title"}
%meta{content: Meta.description, name: "twitter:description"}
%meta{content: Meta.author, name: "twitter:creator"}
/ Twitter summary card with large image must be at least 280x150px
%meta{content: Meta.image_lg, name: "twitter:image:src"}
/ Open Graph data
%meta{content: Meta.title, property: "og:title"}
%meta{content: "article", property: "og:type"}
%meta{content: Settings.base_url, property: "og:url"}
%meta{content: Meta.image, property: "og:image"}
%meta{content: Meta.description, property: "og:description"}
%meta{content: Meta.title, property: "og:site_name"}
%meta{content: ENV['FB_APP_ID'], property: "fb:admins"}
\ No newline at end of file
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
= javascript_include_tag 'application' = javascript_include_tag 'application'
= csrf_meta_tags = csrf_meta_tags
= render 'layouts/meta' = display_meta_tags
%body %body
%nav.navbar.navbar-inverse.navbar-fixed-top %nav.navbar.navbar-inverse.navbar-fixed-top
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment