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"