Skip to content

Commit

Permalink
Merge branch '1-3-stable'
Browse files Browse the repository at this point in the history
* 1-3-stable: (28 commits)
  update readme
  cleanup gemfile
  added email_spec dependency
  bump deface
  building against 1-3-stable and master
  added version file
  refactored taxons : permalink generation from ancestors, using override to form taxons and bump capybara
  Revert "bump globalize"
  bump globalize
  fixed product#duplicate
  removed config default locale
  force data-ajax-root-path to '/'
  bump spree_core
  spree 1-3-stable support
  fix : readme to use globalize3 repo
  Deleting language dropdown in option type index
  Deleting migrate_data_for_taxons migration because it breaks the translation
  Add ability to translate product properties
  Update spree version to 1.2.x
  Add ability to clone products with the translations
  ...
  • Loading branch information
sbounmy committed Mar 30, 2013
2 parents 1ef9e76 + 7540d49 commit 3f41a9c
Show file tree
Hide file tree
Showing 33 changed files with 393 additions and 129 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ script:

branches:
only:
- 1-0-stable
- 1-1-stable
- master
- 1-3-stable
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ source 'http://rubygems.org'

group :test do
gem 'ffaker'
gem 'debugger'
end

gemspec
43 changes: 25 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,31 @@ SpreeMultiLingual is originally a proof of concept for what could become a multi
Since then integration tests and features have been added.

## Requirements
- Spree 1.1.X
- Spree 1.3.X
- Rails 3.2.X


## Installation
Add gem to your Gemfile:

```ruby
gem "spree_multi_lingual", :git => "git@github.com:jipiboily/spree_multi_lingual.git"
```
gem 'spree_multi_lingual', :git => "git@github.com:jipiboily/spree_multi_lingual.git"
gem 'globalize3', :git => 'git://github.com/svenfuchs/globalize3.git'

Globalize3 edge version fixed important bug with dynamic finder : https://github.com/svenfuchs/globalize3/commit/b771fb87d3dda4a78cfe294da1fab7df266e72c9

Run Bundler

```ruby
bundle install
```
bundle install

**Assets**

In app/assets/javascripts/admin/all.js

//= require admin/spree_multi_lingual
//= require admin/spree_multi_lingual_class

This is required for the language dropdown!


Add an initializer file and set SpreeMultiLingual.languages to an array containing the languages you support.

Expand All @@ -33,14 +42,12 @@ For the moment, enable locale fallbacks for I18n (makes lookups for any locale f

```ruby
# config/application.rb
config.I18n.fallbacks = true
config.i18n.fallbacks = true
```

Run spree_multi_lingual install:

```ruby
rails g spree_multi_lingual:install
```
rails g spree_multi_lingual:install

If you want to use browser language detection using rack-contrib Locale :

Expand All @@ -56,18 +63,18 @@ run MyRailsApp::Application
```

## Use
On views where there is translated fields, there should be a dropdown to switch currently edited locale.
On views where there is translated fields, there should be a dropdown to switch currently edited locale.

Products:
http://dl.dropbox.com/u/6210261/spree_multi_lingual.swf

Taxons:
/!\ Using the taxonomy tree you can only edit another locale taxons name, to do so click on the links next to "Edit Taxonomy" to show the taxonomy for a given locale.
If you want to create taxons using the taxonomy tree, please only use the default locale for the moment.
** /!\ Using the taxonomy tree you can only edit another locale taxons name, to do so click on the links next to "Edit Taxonomy" to show the taxonomy for a given locale.
If you want to create taxons using the taxonomy tree, please only use the default locale for the moment.**

To edit taxons permalink please do as following:
![Taxon](http://i44.tinypic.com/dqir20.png)
![TaxonEdit](http://s18.postimage.org/7scp13vux/Screen_shot_2012_03_11_at_4_26_49_PM.png)
![Taxon](https://dl.dropbox.com/u/51922297/Screen%20Shot%202013-03-30%20at%201.03.00%20AM.png)
![TaxonEdit](https://dl.dropbox.com/u/51922297/Screen%20Shot%202013-03-30%20at%201.06.51%20AM.png)

### What is translated?

Expand All @@ -88,7 +95,7 @@ SpreeMultiLingual depends on a fork of routing-filter because it supports :exclu
## TODO

1. Make taxons multi languages editable from the taxonomy tree
2. Dynamically show taxon full permalink depending on dropdown language selected : Taxons#edit
2. Dynamically show taxon prefix permalink depending on dropdown language selected : Taxons#edit
3. Add things to translate:
- Option values
- Properties
Expand All @@ -108,4 +115,4 @@ Be sure to bundle your dependencies and then create a dummy test app for the spe
$ bundle exec rake test app
$ bundle exec rspec spec

Copyright (c) 2012 Jean-Philippe Boily, released under the New BSD License
Copyright (c) 2012 Jean-Philippe Boily, released under the New BSD License
1 change: 1 addition & 0 deletions Versionfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
# "0.60.x" => { :branch => "0-60-stable" }
# "0.40.x" => { :tag => "v1.0.0", :version => "1.0.0" }

"1.3.X" => { :branch => '1-3-stable' }
2 changes: 1 addition & 1 deletion app/controllers/admin/base_controller_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def locale_suffix(locale)
private

def set_user_language
I18n.locale = Spree::Config[:default_locale].to_sym
I18n.locale = (Rails.application.config.i18n.default_locale || 'en').to_sym
end

end
5 changes: 3 additions & 2 deletions app/controllers/controller_helpers_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Spree::Core::ControllerHelpers.module_eval do
Spree::Core::ControllerHelpers::Common.module_eval do
private

# Internal : Set user I18n.locale
Expand All @@ -10,8 +10,9 @@
# or I18n.locale set by rack-locale for example
# or Spree::Config[:default_locale]
# or Rails.application.config.i18n.default_locale

def set_user_language
locale = params[:locale] || session[:locale] || I18n.locale || Spree::Config[:default_locale] || Rails.application.config.i18n.default_locale
locale = params[:locale] || session[:locale] || I18n.locale || Rails.application.config.i18n.default_locale
locale = I18n.default_locale unless locale && I18n.available_locales.include?(locale.to_sym)
I18n.locale = locale.to_sym
end
Expand Down
5 changes: 5 additions & 0 deletions app/models/option_value_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Spree
OptionValue.class_eval do
translates :presentation
end
end
25 changes: 10 additions & 15 deletions app/models/order_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
module Spree
Spree::Order.class_eval do
# Finalizes an in progress order after checkout is complete.
# Called after transition to complete state when payments will have been processed
def finalize!
update_attribute(:completed_at, Time.now)
update_attribute(:locale, I18n.locale)
InventoryUnit.assign_opening_inventory(self)
# lock any optional adjustments (coupon promotions, etc.)
adjustments.optional.each { |adjustment| adjustment.update_attribute('locked', true) }
deliver_order_confirmation_email

self.state_changes.create({
:previous_state => 'cart',
:next_state => 'complete',
:name => 'order' ,
:user_id => (User.respond_to?(:current) && User.current.try(:id)) || self.user_id
}, :without_protection => true)
# override this to also set current user locale for email i18n
def deliver_order_confirmation_email
update_attribute(:locale, I18n.locale)
begin
OrderMailer.confirm_email(self).deliver
rescue Exception => e
logger.error("#{e.class.name}: #{e.message}")
logger.error(e.backtrace * "\n")
end
end

end
end
49 changes: 48 additions & 1 deletion app/models/spree/product_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,50 @@
Spree::Product.class_eval do
translates :name, :description, :meta_description, :meta_keywords, :permalink
end

# for adding products which are closely related to existing ones
# define "duplicate_extra" for site-specific actions, eg for additional fields
def duplicate
p = self.dup

(SpreeMultiLingual.languages).each do |locale|
locale_suffix = locale.empty? ? "" : "_#{locale}"

name_locale = (I18n.t :copy_of, :locale => locale) + read_attribute(:name, :locale => locale)
description_locale = read_attribute(:description, :locale => locale)
meta_description_locale = read_attribute(:meta_description, :locale => locale)
meta_keywords_locale = read_attribute(:meta_keywords, :locale => locale)
permalink_locale = (I18n.t :copy_of_permalink, :locale => locale) + read_attribute(:permalink, :locale => locale)

eval("p.name"+locale_suffix+" = name_locale")
eval("p.description"+locale_suffix+" = description_locale")
eval("p.meta_description"+locale_suffix+" = meta_description_locale")
eval("p.meta_keywords"+locale_suffix+" = meta_keywords_locale")
eval("p.permalink"+locale_suffix+" = permalink_locale")
end

p.deleted_at = nil
p.created_at = p.updated_at = nil
p.taxons = taxons

p.product_properties = product_properties.map { |q| r = q.dup; r.created_at = r.updated_at = nil; r }

image_dup = lambda { |i| j = i.dup; j.attachment = i.attachment.clone; j }

variant = master.dup
variant.sku = 'COPY OF ' + master.sku
variant.deleted_at = nil
variant.images = master.images.map { |i| image_dup.call i }
variant.price = master.price
variant.currency = master.currency
p.master = variant

# don't dup the actual variants, just the characterising types
p.option_types = option_types if has_variants?

# allow site to do some customization
p.send(:duplicate_extra, self) if p.respond_to?(:duplicate_extra)
p.save!
p
end
end

3 changes: 3 additions & 0 deletions app/models/spree/product_property_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Spree::ProductProperty.class_eval do
translates :value
end
22 changes: 11 additions & 11 deletions app/models/spree/variant_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Spree::Variant.class_eval do
alias_method :old_price=, :price=
# Spree::Variant.class_eval do
# alias_method :old_price=, :price=

def price=(value)
write_attribute(:price, value.to_s.tr(',', '.').to_f)
end
# def price=(value)
# write_attribute(:price, value.to_s.tr(',', '.').to_f)
# end

[:cost_price, :weight, :height, :width, :depth].each do |attribute|
define_method("#{attribute}=") do |value|
write_attribute(attribute, value.to_s.tr(',', '.').to_f)
end
end
end
# [:cost_price, :weight, :height, :width, :depth].each do |attribute|
# define_method("#{attribute}=") do |value|
# write_attribute(attribute, value.to_s.tr(',', '.').to_f)
# end
# end
# end
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
9 changes: 1 addition & 8 deletions app/overrides/option_type_add_language_dropdown.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
Deface::Override.new(
:virtual_path => "spree/admin/option_types/index",
:insert_before => "div#new_option_type",
:text => '<%= render "spree/admin/shared/language_dropdown", :object => Spree::OptionType.new -%>',
:name => "option_type_add_language_dropdown"
)

Deface::Override.new(
:virtual_path => "spree/admin/option_types/edit",
:insert_before => "fieldset",
:text => '<%= render "spree/admin/shared/language_dropdown", :object => @option_type -%>',
:name => "option_type_add_language_dropdown"
)
)
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 %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
set_attributes "body.admin"
attributes :'data-ajax-root-path' => '/' #spree.root_path(:locale => false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<tr class="product_property fields" data-hook="product_property">
<td class='property_name'>
<%= f.text_field :property_name, :class => 'autocomplete' %>
</td>
<%- SpreeMultiLingual.languages.each do |locale| %>
<td class='value'>
<% fieldsym = "value_#{locale}".to_sym %>
<%= f.text_field fieldsym, :class => 'autocomplete' %>
</td>
<% end %>

<td class="actions">
<%= link_to_remove_fields t(:remove), f %>
</td>
</tr>
Loading

0 comments on commit 3f41a9c

Please sign in to comment.