Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with latest Rails and Spree #5

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 23 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,67 +1,56 @@
# GlobalizeSpree

Localizing model data in Spree using Globalize3.
Updated to be compatible with Rails 3.2 and Spree 1.2.
It provides localization using the gem globalize3.
I also added an admin form for specifying the taxon name. This can be done by right clicking on the taxon in the Categories pages and choosing Edit. (ignore the errors displayed in the dropdown, they are harmless)

[![Build Status](https://secure.travis-ci.org/tomash/globalize-spree.png)](http://travis-ci.org/tomash/globalize-spree)



## Installation


Add to Gemfile:

gem "globalize_spree", :git => 'git://github.com/tomash/globalize-spree.git'

Also, to be super-sure (I've ran into some trouble before that, needs investigation) add

gem "globalize_spree", :git => 'https://github.com/victor-github/globalize-spree'
gem "i18n"
gem "globalize3"

to your application Gemfile as well.

Now run the rake task to copy migrations and run them:
rake globalize_spree:install
rake db:migrate
bundle rake globalize_spree:install
bundle rake db:migrate


## Usage

Works OOTB.

Currently globalizes: Product (name, description), Taxon (name).

### Basic usage (vanilla Globalize way)
### Basic usage

It works like Globalize. To set your locale, add your default locale, for example inside config/initializers/spree.rb

Spree.config do |config|
default_locale = "ro"
end

Then you can do:

Basically works like Globalize, so

# set the locale or do not to leave default one
I18n.locale = :en
# and you are ready to go
Product.last.name # <= get name for this product for :en locale
Product.last.name = "something" # <= set name for this product for :en locale

### Easy Globalize Accessors

For easy editing of translated stuff for predefined locales, it uses code from easy-globalize2-accessors. Inside self.activate of your lib/spree_site.rb put
For easy editing of translated stuff for predefined locales, it uses code from easy-globalize2-accessors. Inside config.to_prepare of your config/application.rb put

Product.class_eval do
globalize_accessors :pl, :en, :de
globalize_accessors :en, :ro, :de
attr_accessible :name_en, :name_ro, name_de, :description_en, :description_ro, :description_de
end

And spree-globalize will replace Product form's "name" field with, respectively, "name_pl", "name_en" and "name_de" fields ("description" following same pattern) in admin panel. Try it!


## ToDo

More models.


## Contributing

Feel free to use Github issues and/or contact me via Github PM. Pull requests are welcome, provided they include some explanation (and relevant open issue maybe?).

And spree-globalize will replace Product form's "name" field with, respectively, "name_en", "name_ro" and "name_de" fields ("description" following same pattern) in admin panel.

## Usual blabber
The attr_accessible line is necessary since in Rails 3.2 attributes need to be whitelisted explicitly in order for assignment to work.

Copyright (c) 2010 Tomasz "Tomash" Stachewicz, released under the New BSD License
### TO DOs:
Fix taxon edit menu errors
More models
2 changes: 1 addition & 1 deletion app/models/product_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Product.class_eval do
Spree::Product.class_eval do
translates :name, :description

extend Globalize::Migratable
Expand Down
2 changes: 1 addition & 1 deletion app/models/taxon_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Taxon.class_eval do
Spree::Taxon.class_eval do
translates :name, :description

extend Globalize::Migratable
Expand Down
6 changes: 3 additions & 3 deletions app/overrides/product_globalize_form_left.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Deface::Override.new(:virtual_path => "admin/products/_form",
Deface::Override.new(:virtual_path => "spree/admin/products/_form",
:replace => "[data-hook='admin_product_form_left']",
:partial => "admin/shared/product_globalize_form_left",
:name => "product_globalize_form_left")
:partial => "spree/admin/shared/product_globalize_form_left",
:name => "product_globalize_form_left")
5 changes: 5 additions & 0 deletions app/overrides/taxon_globalize_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Deface::Override.new(:virtual_path => "spree/admin/taxons/_form",
:replace => "[data-hook='admin_taxon_form']",
:partial => "spree/admin/taxon/form",
:name => "taxon_globalize_form")

Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<div class="left" data-hook="admin_product_form_left">
<% if(Product.easy_accessors.blank?) %>
<% if(Spree::Product.easy_accessors.blank?) %>
<%= f.field_container :name do %>
<%= f.label :name, t("name") %> <span class="required">*</span><br />
<%= f.text_field :name, :class => 'fullwidth title' %>
<%= f.error_message_on :name %>
<% end %>

<% else %>
<%- Product.easy_accessors.each do |locale| %>
<% Spree::Product.easy_accessors.each do |locale| %>
<% fieldsym = "name_#{locale}".to_sym %>
<%= f.field_container fieldsym do %>
<%= f.label fieldsym, t(fieldsym.to_s) %><br />
Expand All @@ -24,7 +24,7 @@
<%= f.error_message_on :permalink %>
<% end %>

<% if(Product.easy_accessors.blank?) %>
<% if(Spree::Product.easy_accessors.blank?) %>

<%= f.field_container :description do %>
<%= f.label :description, t("description")%><br />
Expand All @@ -34,7 +34,7 @@

<% else %>

<%- Product.easy_accessors.each do |locale| %>
<%- Spree::Product.easy_accessors.each do |locale| %>
<% fieldsym = "description_#{locale}".to_sym %>
<%= f.field_container fieldsym do %>
<%= f.label fieldsym, t(fieldsym.to_s)%><br />
Expand All @@ -44,4 +44,4 @@
<% end %>

<% end %>
</div>
</div>
35 changes: 35 additions & 0 deletions app/views/spree/admin/taxons/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<div data-hook="admin_inside_taxon_form">

<% if(Spree::Taxon.easy_accessors.blank?) %>
<%= f.field_container :name do %>
<%= f.label :name, t(:name) %> <span class="required">*</span><br />
<%= error_message_on :taxon, :name, :class => 'fullwidth title' %>
<%= text_field :taxon, :name %>
<% end %>
<% else %>
<% Spree::Taxon.easy_accessors.each do |locale| %>
<% fieldsym = "name_#{locale}".to_sym %>
<%= f.field_container fieldsym do %>
<%= f.label fieldsym, t(fieldsym.to_s) %> <span class="required">*</span><br />
<%= f.error_message_on fieldsym %>
<%= f.text_field fieldsym, :class => 'fullwidth title' %>
<% end %>
<% end %>
<% end %>

<%= f.field_container :permalink_part do %>
<%= f.label :permalink_part, t(:permalink) %><span class="required">*</span><br />
<%= @taxon.permalink.split("/")[0...-1].join("/") + "/" %><%= text_field_tag :permalink_part, @permalink_part %>
<% end %>

<%= f.field_container :icon do %>
<%= f.label :icon, t(:icon) %><br />
<%= f.file_field :icon %>
<% end %>

<%= f.field_container :description do %>
<%= f.label :description, t(:description) %><br />
<%= f.text_area :description %>
<% end %>
</div>

6 changes: 3 additions & 3 deletions db/migrate/20101107175511_add_globalize_to_products.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
class AddGlobalizeToProducts < ActiveRecord::Migration
def self.up
Product.create_translation_table! :name => :string, :description => :text
Spree::Product.create_translation_table! :name => :string, :description => :text

# save old values into brand new translations table
Product.migrate_translated_fields
Spree::Product.migrate_translated_fields
end

def self.down
Product.drop_translation_table!
Spree::Product.drop_translation_table!
end
end
6 changes: 3 additions & 3 deletions db/migrate/20101107185551_add_globalize_to_taxons.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
class AddGlobalizeToTaxons < ActiveRecord::Migration
def self.up
Taxon.create_translation_table! :name => :string, :description => :text
Spree::Taxon.create_translation_table! :name => :string, :description => :text

# save old values into brand new translations table
Taxon.migrate_translated_fields
Spree::Taxon.migrate_translated_fields
end

def self.down
Taxon.drop_translation_table!
Spree::Taxon.drop_translation_table!
end
end