Skip to content

Commit

Permalink
ページ作成機能:フロント:メタタグの表示制御作成
Browse files Browse the repository at this point in the history
  • Loading branch information
appirits-ymatsumoto committed Jun 5, 2015
1 parent 2ea8777 commit 97d0d28
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
28 changes: 28 additions & 0 deletions core/app/helpers/comable/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ def liquidize(content, arguments)
string.respond_to?(:html_safe) ? string.html_safe : string
end

def current_meta_description
if current_resource.present? && current_resource_meta_method_respond_to?(:meta_description)
current_resource.meta_description
elsif current_store.meta_description.present?
current_store.meta_description
end
end

def current_meta_keywords
if current_resource.present? && current_resource_meta_method_respond_to?(:meta_keywords)
current_resource.meta_keywords
elsif current_store.meta_keywords.present?
current_store.meta_keywords
end
end

private

def after_sign_in_path_for(_resource)
Expand All @@ -69,5 +85,17 @@ def after_update_path_for(resource)
def after_resetting_password_path_for(resource)
signed_in_root_path(resource) || comable.root_path
end

def current_resource
instance_variable_get current_resource_name
end

def current_resource_name
"@#{controller.controller_name.singularize}"
end

def current_resource_meta_method_respond_to?(method)
current_resource.respond_to? method
end
end
end
57 changes: 57 additions & 0 deletions core/spec/helpers/comable/application_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,61 @@
expect(subject.liquidize('email: {{ order.email }}', order: order)).not_to include(order.email)
end
end

context 'メタタグ' do
let!(:page) { FactoryGirl.create(:page) }
describe 'インスタンス変数があってメタタグのメソッドがある場合' do
before do
allow(subject).to receive(:current_resource).and_return(page)
allow(subject).to receive(:current_resource_meta_method_respond_to?).and_return(true)
end

context 'インスタンス変数のメタタグを表示する' do
it '#current_meta_description' do
expect(subject.current_meta_description).to eq(page.meta_description)
end

it '#current_meta_keywords' do
expect(subject.current_meta_keywords).to eq(page.meta_keywords)
end
end
end

describe 'インスタンス変数があってメタタグのメソッドがない場合' do
let!(:store) { FactoryGirl.create(:store, meta_description: 'store_meta_description', meta_keywords: 'store_keywords') }
before do
allow(subject).to receive(:current_store).and_return(store)
allow(subject).to receive(:current_resource).and_return(page)
allow(subject).to receive(:current_resource_meta_method_respond_to?).and_return(false)
end

context 'ストアのメタタグを表示する' do
it '#current_meta_description' do
expect(subject.current_meta_description).to eq(store.meta_description)
end

it '#current_meta_keywords' do
expect(subject.current_meta_keywords).to eq(store.meta_keywords)
end
end
end

describe 'インスタンス変数がない場合' do
let!(:store) { FactoryGirl.create(:store, meta_description: 'store_meta_description', meta_keywords: 'store_keywords') }
before do
allow(subject).to receive(:current_store).and_return(store)
allow(subject).to receive(:current_resource)
end

context 'ストアのメタタグを表示する' do
it '#current_meta_description' do
expect(subject.current_meta_description).to eq(store.meta_description)
end

it '#current_meta_keywords' do
expect(subject.current_meta_keywords).to eq(store.meta_keywords)
end
end
end
end
end
9 changes: 5 additions & 4 deletions frontend/app/views/layouts/comable/application.slim
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ html
head
title = current_store.name

- if current_store.meta_keywords.present?
meta name="keywords" content="#{current_store.meta_keywords}"
- if current_meta_keywords.present?
meta name="keywords" content="#{current_meta_keywords}"

- if current_meta_description.present?
meta name="description" content="#{current_meta_description}"

- if current_store.meta_description.present?
meta name="description" content="#{current_store.meta_description}"

= stylesheet_link_tag "comable/frontend/application", media: "all"
= javascript_include_tag "comable/frontend/application"
Expand Down

0 comments on commit 97d0d28

Please sign in to comment.