Skip to content

Commit

Permalink
refactored taxons : permalink generation from ancestors, using overri…
Browse files Browse the repository at this point in the history
…de to form taxons and bump capybara
  • Loading branch information
sbounmy committed Mar 29, 2013
1 parent 6d332ef commit db7ea9d
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 56 deletions.
64 changes: 49 additions & 15 deletions app/models/taxon_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module Spree
Taxon.class_eval do
translates :name, :description, :permalink
after_save :set_permalink_and_save

before_update :set_permalink

# Public : Permalink setter with multi language support
#
Expand All @@ -13,27 +16,58 @@ module Spree
locale_suffix = locale.empty? ? "" : "_#{locale}"
define_method("permalink#{locale_suffix}=") do |permalink_part|
opts = locale.empty? ? {} : { :locale => locale.to_sym }
unless new_record?
_permalink = (read_attribute(:permalink, opts) || []).split("/")[0...-1]
permalink_part = (_permalink << permalink_part).join("/")
write_attribute(:permalink, (ancestors_permalink(opts) << permalink_part).join('/'), opts)
end
end

def ancestors_permalink(opts = {})
ancestors.map { |a| a.permalink_name(opts) }
end

def permalink_prefix
ancestors_permalink.join('/')
end

# Returns last part of permalink
#
# Example :
# taxon.permalink
# => 'ruby-on-rails-fr/sinatra-fr'
# taxon.permalink_name
# => 'sinatra-fr'
def permalink_name(opts = {})
read_attribute(:permalink, opts).split('/').last
end

def default_permalink_name
permalink.blank? ? name.to_url : permalink_name
end

def localed_permalink
[].tap do |res|
SpreeMultiLingual.languages.each do |lang|
res << {:permalink => permalink_name(:locale => lang), :locale => lang}
end
write_attribute(:permalink, permalink_part, opts)
end
end

# Creates permalink based on Stringex's .to_url method
def set_permalink
if parent_id.nil?
self.permalink = name.to_url if self.permalink.blank?
else
parent_taxon = Taxon.find(parent_id)
parent_taxon.translations_for(:permalink).each do |attribute|
parent_permalink, locale = attribute[:permalink], attribute[:locale]
permalink_locale = read_attribute(:permalink, :locale => locale)
write_attribute(:permalink, [parent_permalink, (permalink_locale.blank? ? name.to_url : permalink_locale.split('/').last)].join('/'), :locale => locale)
end
write_attribute :permalink, [parent_taxon.permalink, (self.permalink.blank? ? name.to_url : self.permalink.split('/').last)].join('/')
self.permalink = default_permalink_name
localed_permalink.each do |t|
self.send("permalink_#{t[:locale]}=", t[:permalink])
end
children.reload.each { |c| c.save }
true
end

# awesome_set hack to run this callback only on create, could not access ancestors with after_create
# https://github.com/collectiveidea/awesome_nested_set/issues/29
def set_permalink_and_save
return true if @permalinK_done
set_permalink
@permalinK_done = true
save!
end

end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!-- replace "code[erb-loud]:contains('f.field_container :permalink_part do')" closing_selector 'code[erb-silent]:contains("end")' enabled -->

<%= f.field_container :permalink do %>
<%= f.label :permalink, t(:permalink) %><span class="required">*</span><br />
<%= @taxon.permalink.split("/")[0...-1].join("/") + "/" %><%= text_field_tag "taxon[permalink]", @permalink_part %>
<% end %>
23 changes: 0 additions & 23 deletions app/views/spree/admin/taxons/_form.html.erb

This file was deleted.

38 changes: 35 additions & 3 deletions spec/models/taxon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,27 @@
taxon.permalink_en.should == "ruby-on-rails"
end

context "when child taxon" do
before { taxon.update_attributes!(:permalink => "ruby-on-rails", :permalink_fr => "ruby-on-rails-fr", :permalink_es => "ruby-on-rails-es") }
it 'should update multi lingual permalink' do
taxon.update_attributes!(:permalink => "ruby-on-rails", :permalink_fr => "ruby-on-rails-fr", :permalink_es => "ruby-on-rails-es")
taxon.permalink_fr.should == "ruby-on-rails-fr"
taxon.permalink_es.should == "ruby-on-rails-es"
taxon.permalink_en.should == "ruby-on-rails"
end

it 'child should have correct permalink' do
child.permalink_fr.should == 'ruby-on-rails/sinatra'
child.permalink_es.should == 'ruby-on-rails/sinatra'
child.permalink_en.should == 'ruby-on-rails/sinatra'
end

context "when update" do
before do
taxon.update_attributes!(:permalink => "ruby-on-rails", :permalink_fr => "ruby-on-rails-fr", :permalink_es => "ruby-on-rails-es")
child
end

it "returns translated parents permalink" do
child.permalink.should == "ruby-on-rails/sinatra"
child.reload.permalink.should == "ruby-on-rails/sinatra"
child.permalink_en.should == "ruby-on-rails/sinatra"
child.permalink_fr.should == "ruby-on-rails-fr/sinatra"
child.permalink_es.should == "ruby-on-rails-es/sinatra"
Expand Down Expand Up @@ -51,4 +67,20 @@
end
end
end

describe '#permalink_prefix' do
let(:child2) { FactoryGirl.create(:taxon, :name => "Padrino", :parent => child) }

it 'returns prefix (parents permalink)' do
child.permalink_prefix.should == 'ruby-on-rails'
child2.permalink_prefix.should == 'ruby-on-rails/sinatra'
end

it 'returns empty string root or new taxon' do
taxon.permalink_prefix.should == ''
Spree::Taxon.new.permalink_prefix.should == ''
end

end

end
5 changes: 2 additions & 3 deletions spec/requests/option_types_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@

select "fr", :from => "spree_multi_lingual_dropdown"

fill_in "option_type_name", :with => "size"
fill_in "option_type_presentation_fr", :with => "Taille"

click_button "Update"
page.should have_content("successfully updated!")
click_icon :edit

page.should have_content("Size")
first('#option_type_presentation')[:value].should == "Size"
select "fr", :from => "spree_multi_lingual_dropdown"
page.should have_content("Taille")
first('#option_type_presentation_fr')[:value].should == "Taille"
end
end
7 changes: 4 additions & 3 deletions spec/requests/products_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@

# Checking if each language have been corectly updated
%w(fr en es).each do |locale|
suffix = "#{locale.to_sym == I18n.locale ? "" : "_#{locale}"}"
select locale, :from => "spree_multi_lingual_dropdown"
page.should have_content("ror mug #{locale}")
page.should have_content("meta #{locale} desc")
page.should have_content("#{locale} keywords")
first("input#product_name#{suffix}")[:value].should == "ror mug #{locale}"
first("input#product_meta_description#{suffix}")[:value].should == "meta #{locale} desc"
first("input#product_meta_keywords#{suffix}")[:value].should == "#{locale} keywords"
end
end

Expand Down
21 changes: 13 additions & 8 deletions spec/requests/taxons_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@

click_link "fr"
fill_in "Name", :with => "Bonjour"

click_button "Update"

click_icon :edit
click_link "fr"
page.should have_content("Bonjour")
page.should have_content("BONJOUR")

within("h1") { click_link "es" }

click_link "es"
fill_in "Name", :with => "Hola"
click_button "Update"

click_icon :edit
click_link "es"
page.should have_content("Hola")
within("h1") { click_link "es" }
page.should have_content("HOLA")
end

context "edit taxons" do
Expand All @@ -49,7 +51,7 @@

suffix = "#{locale.to_sym == I18n.locale ? "" : "_#{locale}"}"

fill_in "taxon_name#{suffix}", :with => "TAXON - #{locale}"
fill_in "taxon_name#{suffix}", :with => "TAXON - #{locale.upcase}"
fill_in "taxon_description#{suffix}", :with => "TAXON Description - #{locale * 20}"
fill_in "taxon_permalink#{suffix}", :with => "taxon-#{locale}"
end
Expand All @@ -63,14 +65,17 @@
# verify if the form has correct values
%w(fr en es).each do |locale|
select locale, :from => "spree_multi_lingual_dropdown"
page.should have_content("TAXON - #{locale}")
page.should have_content("TAXON Description - #{locale * 20}")

suffix = "#{locale.to_sym == I18n.locale ? "" : "_#{locale}"}"

first("input#taxon_name#{suffix}")[:value].should == "TAXON - #{locale.upcase}"
first("textarea#taxon_description#{suffix}")[:value].should == "TAXON Description - #{locale * 20}"
end

# set local and ensure each page is visitable
%w(fr en es).each do |locale|
visit "/#{locale}/t/taxon-#{locale}"
page.should have_content "TAXON - #{locale}"
page.should have_content "TAXON - #{locale.upcase}"
end
end

Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
# instead of true.
config.use_transactional_fixtures = true
config.include Spree::UrlHelpers
config.include Capybara::DSL

config.after(:each) do
I18n.locale = nil
end
Expand Down
2 changes: 1 addition & 1 deletion spree_multi_lingual.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Gem::Specification.new do |s|

s.add_dependency 'coffee-rails'

s.add_development_dependency 'capybara', '1.0.1'
s.add_development_dependency 'capybara', '2.0.2'
s.add_development_dependency 'factory_girl_rails', '~> 1.7.0'
s.add_development_dependency 'ffaker'
s.add_development_dependency 'rspec-rails', '~> 2.9.0'
Expand Down

0 comments on commit db7ea9d

Please sign in to comment.