Taxon seo #2787

Closed
wants to merge 2 commits into
from
Jump to file or symbol
Failed to load files and symbols.
+99 −3
Diff settings

Always

Just for now

@@ -18,8 +18,14 @@ def show
end
private
- def accurate_title
- @taxon ? @taxon.name : super
+
+ def accurate_title
+ if @taxon
+ @taxon.seo_title
+ else
+ super
end
+ end
+
end
end
@@ -8,7 +8,8 @@ class Taxon < ActiveRecord::Base
before_create :set_permalink
- attr_accessible :name, :parent_id, :position, :icon, :description, :permalink, :taxonomy_id
+ attr_accessible :name, :parent_id, :position, :icon, :description, :permalink, :taxonomy_id,
+ :meta_description, :meta_keywords, :meta_title
validates :name, :presence => true
@@ -36,6 +37,15 @@ def applicable_filters
fs
end
+ # Return meta_title if set otherwise generates from root name and/or taxon name
+ def seo_title
+ if meta_title
+ meta_title
+ else
+ root? ? name : "#{root.name} - #{name}"
+ end
+ end
+
# Creates permalink based on Stringex's .to_url method
def set_permalink
if parent_id.nil?
@@ -16,6 +16,22 @@
<%= f.label :icon, t(:icon) %><br />
<%= f.file_field :icon %>
<% end %>
+
+ <%= f.field_container :meta_title do %>
+ <%= f.label :meta_title, t(:meta_title) %><br />
+ <%= f.text_field :meta_title, :class => 'fullwidth', :rows => 6 %>
+ <% end %>
+
+ <%= f.field_container :meta_description do %>
+ <%= f.label :meta_description, t(:meta_description) %><br />
+ <%= f.text_field :meta_description, :class => 'fullwidth', :rows => 6 %>
+ <% end %>
+
+ <%= f.field_container :meta_description do %>
+ <%= f.label :meta_keywords, t(:meta_keywords) %><br />
+ <%= f.text_field :meta_keywords, :class => 'fullwidth', :rows => 6 %>
+ <% end %>
+
</div>
<div class="omega seven columns">
@@ -24,4 +40,5 @@
<%= f.text_area :description, :class => 'fullwidth', :rows => 6 %>
<% end %>
</div>
+
</div>
@@ -0,0 +1,9 @@
+class AddSeoMetasToTaxons < ActiveRecord::Migration
+ def change
+ change_table :spree_taxons do |t|
+ t.string :meta_title
+ t.string :meta_description
+ t.string :meta_keywords
+ end
+ end
+end
@@ -85,6 +85,36 @@ def find_label(text)
end
+RSpec::Matchers.define :have_meta do |name, expected|
+ match do |actual|
+ has_css?("meta[name='#{name}'][content='#{expected}']")
+ end
+
+ failure_message_for_should do |actual|
+ actual = first("meta[name='#{name}']")
+ if actual
+ "expected that meta #{name} would have content='#{expected}' but was '#{actual[:content]}'"
+ else
+ "expected that meta #{name} would exist with content='#{expected}'"
+ end
+ end
+end
+
+RSpec::Matchers.define :have_title do |expected|
+ match do |actual|
+ has_css?("title", :text => expected)
+ end
+
+ failure_message_for_should do |actual|
+ actual = first("title")
+ if actual
+ "expected that title would have been '#{expected}' but was '#{actual.text}'"
+ else
+ "expected that title would exist with '#{expected}'"
+ end
+ end
+end
+
RSpec.configure do |c|
c.include CapybaraExt
end
@@ -9,10 +9,34 @@
product = create(:product, :name => "Superman T-Shirt")
product.taxons << t_shirts
end
+ let(:metas) { { :meta_description => 'Brand new Ruby on Rails TShirts', :meta_title => "Ruby On Rails TShirt", :meta_keywords => 'ror, tshirt, ruby' } }
# Regression test for #1796
it "can see a taxon's products, even if that taxon has child taxons" do
visit '/t/category/clothing/t-shirts'
page.should have_content("Superman T-Shirt")
end
+
+ describe 'meta tags and title' do
+
+ it 'displays metas' do
+ t_shirts.update_attributes metas
+ visit '/t/category/clothing/t-shirts'
+ page.should have_meta(:description, 'Brand new Ruby on Rails TShirts')
+ page.should have_meta(:keywords, 'ror, tshirt, ruby')
+ end
+
+ it 'display title if set' do
+ t_shirts.update_attributes metas
+ visit '/t/category/clothing/t-shirts'
+ page.should have_title("Ruby On Rails TShirt")
+ end
+
+ it 'display title from taxon root and taxon name' do
+ visit '/t/category/clothing/t-shirts'
+ page.should have_title('Category - T-Shirts - Spree Demo Site')
+ end
+
+ end
+
end