From 97d0d285391f4a5f63e6ea5e920090c0c14bce22 Mon Sep 17 00:00:00 2001 From: ymatsumoto Date: Fri, 5 Jun 2015 18:15:32 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=9A=E3=83=BC=E3=82=B8=E4=BD=9C=E6=88=90?= =?UTF-8?q?=E6=A9=9F=E8=83=BD=EF=BC=9A=E3=83=95=E3=83=AD=E3=83=B3=E3=83=88?= =?UTF-8?q?=EF=BC=9A=E3=83=A1=E3=82=BF=E3=82=BF=E3=82=B0=E3=81=AE=E8=A1=A8?= =?UTF-8?q?=E7=A4=BA=E5=88=B6=E5=BE=A1=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/helpers/comable/application_helper.rb | 28 +++++++++ .../comable/application_helper_spec.rb | 57 +++++++++++++++++++ .../views/layouts/comable/application.slim | 9 +-- 3 files changed, 90 insertions(+), 4 deletions(-) diff --git a/core/app/helpers/comable/application_helper.rb b/core/app/helpers/comable/application_helper.rb index 4551acf6..c8f4ac4d 100644 --- a/core/app/helpers/comable/application_helper.rb +++ b/core/app/helpers/comable/application_helper.rb @@ -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) @@ -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 diff --git a/core/spec/helpers/comable/application_helper_spec.rb b/core/spec/helpers/comable/application_helper_spec.rb index b1b4a985..521d6d41 100644 --- a/core/spec/helpers/comable/application_helper_spec.rb +++ b/core/spec/helpers/comable/application_helper_spec.rb @@ -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 diff --git a/frontend/app/views/layouts/comable/application.slim b/frontend/app/views/layouts/comable/application.slim index 01f18501..41cf9c9c 100644 --- a/frontend/app/views/layouts/comable/application.slim +++ b/frontend/app/views/layouts/comable/application.slim @@ -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"