diff --git a/app/models/taxon_decorator.rb b/app/models/taxon_decorator.rb
index 4f0de28..40c4aeb 100644
--- a/app/models/taxon_decorator.rb
+++ b/app/models/taxon_decorator.rb
@@ -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
#
@@ -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
\ No newline at end of file
diff --git a/app/overrides/spree/admin/taxons/_form/replace_permalink_part_by_permalink.html.erb.deface b/app/overrides/spree/admin/taxons/_form/replace_permalink_part_by_permalink.html.erb.deface
new file mode 100644
index 0000000..26eae78
--- /dev/null
+++ b/app/overrides/spree/admin/taxons/_form/replace_permalink_part_by_permalink.html.erb.deface
@@ -0,0 +1,6 @@
+
+
+<%= f.field_container :permalink do %>
+ <%= f.label :permalink, t(:permalink) %>*
+ <%= @taxon.permalink.split("/")[0...-1].join("/") + "/" %><%= text_field_tag "taxon[permalink]", @permalink_part %>
+<% end %>
diff --git a/app/views/spree/admin/taxons/_form.html.erb b/app/views/spree/admin/taxons/_form.html.erb
deleted file mode 100644
index 5487d86..0000000
--- a/app/views/spree/admin/taxons/_form.html.erb
+++ /dev/null
@@ -1,23 +0,0 @@
-
- <%= f.field_container :name do %>
- <%= f.label :name, t(:name) %> *
- <%= error_message_on :taxon, :name, :class => 'fullwidth title' %>
- <%= text_field :taxon, :name %>
- <% end %>
-
- <%= f.field_container :permalink do %>
- <%= f.label :permalink, t(:permalink) %>*
- <%= @taxon.permalink.split("/")[0...-1].join("/") + "/" %><%= text_field_tag "taxon[permalink]", @permalink_part %>
- <% end %>
-
- <%= f.field_container :icon do %>
- <%= f.label :icon, t(:icon) %>
- <%= f.file_field :icon %>
- <% end %>
-
- <%= f.field_container :description do %>
- <%= f.label :description, t(:description) %>
- <%= f.text_area :description %>
- <% end %>
-
-
diff --git a/spec/models/taxon_spec.rb b/spec/models/taxon_spec.rb
index 22d8ef3..83d77ab 100644
--- a/spec/models/taxon_spec.rb
+++ b/spec/models/taxon_spec.rb
@@ -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"
@@ -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
\ No newline at end of file
diff --git a/spec/requests/option_types_spec.rb b/spec/requests/option_types_spec.rb
index 18edbad..d1f9f04 100644
--- a/spec/requests/option_types_spec.rb
+++ b/spec/requests/option_types_spec.rb
@@ -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
\ No newline at end of file
diff --git a/spec/requests/products_spec.rb b/spec/requests/products_spec.rb
index 8d6114e..4a147ea 100644
--- a/spec/requests/products_spec.rb
+++ b/spec/requests/products_spec.rb
@@ -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
diff --git a/spec/requests/taxons_spec.rb b/spec/requests/taxons_spec.rb
index 7793afe..5fcddcd 100644
--- a/spec/requests/taxons_spec.rb
+++ b/spec/requests/taxons_spec.rb
@@ -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
@@ -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
@@ -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
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 8bd36fb..023a81b 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -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
diff --git a/spree_multi_lingual.gemspec b/spree_multi_lingual.gemspec
index c83e409..0e9f44a 100644
--- a/spree_multi_lingual.gemspec
+++ b/spree_multi_lingual.gemspec
@@ -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'