Skip to content

Commit

Permalink
Switch from Globalize to Mobility (#3360)
Browse files Browse the repository at this point in the history
In order to translate our content we have been using a library called
Globalize.  This has worked really well for us up until now, however a
new library has been released called Mobility which seems like it would
work better for our case.

This commit switches us from Globalize to Mobility in conjunction with
the release of refinerycms-i18n version 5.0.0.
  • Loading branch information
bricesanchez authored and parndt committed May 25, 2018
1 parent a36c571 commit d181fbb
Show file tree
Hide file tree
Showing 38 changed files with 192 additions and 189 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ end
if !ENV['TRAVIS'] || ENV['DB'] == 'mysql'
group :mysql do
gem 'activerecord-jdbcmysql-adapter', '>= 1.3.0.rc1', platform: :jruby
gem 'mysql2', '~> 0.3.18', :platform => :ruby
gem 'mysql2', '~> 0.4.10', :platform => :ruby
end
end

Expand Down
2 changes: 1 addition & 1 deletion core/lib/generators/refinery/engine/templates/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ end

platforms :ruby do
gem 'sqlite3'
gem 'mysql2'
gem 'mysql2', '~> 0.4.10'
gem 'pg'
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Refinery
<% end %>
<% if localized? -%>

extend Mobility
translates <%= localized_attributes.map { |a| ":#{a.name}" }.join(', ') %>
<% end -%>
<% if string_attributes.any? -%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<% if localized? -%>
<%%= render '/refinery/admin/locale_picker',
:current_locale => Globalize.locale %>
:current_locale => Mobility.locale %>
<% end -%>
<% attributes.each_with_index do |attribute, index| -%>
<% if attribute.refinery_type == :image -%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@ class Create<%= namespacing %><%= class_name.pluralize %> < ActiveRecord::Migrat

def up
create_table :refinery_<%= "#{namespacing.underscore}_" if table_name != namespacing.underscore.pluralize -%><%= table_name %> do |t|
<% attributes.each do |attribute| -%>
<% (attributes - localized_attributes).each do |attribute| -%>
t.<%= attribute.type %> :<%= attribute.column_name %>
<% end -%>
t.integer :position

t.timestamps
end

<% if localized? %>
Refinery::<%= namespacing %>::<%= class_name %>.create_translation_table! <%= attributes_for_translation_table %>
create_table :<%= localized_table_name %> do |t|
<% localized_attributes.each do |attribute| -%>
t.<%= attribute.type %> :<%= attribute.column_name %>
<% end -%>
t.string :locale, null: false
t.integer :refinery_<%= singular_table_name %>_id, null: false
t.timestamps
end

add_index :<%= localized_table_name %>, :locale, name: :index_<%= localized_table_name %>_on_locale
add_index :<%= localized_table_name %>, [:refinery_<%= singular_table_name %>_id, :locale], name: :index_<%= Digest::SHA1.hexdigest(localized_table_name) %>, unique: true
<% end %>
end


def down
if defined?(::Refinery::UserPlugin)
::Refinery::UserPlugin.destroy_all({:name => "refinerycms-<%= namespacing.underscore %>"})
Expand All @@ -25,8 +37,7 @@ class Create<%= namespacing %><%= class_name.pluralize %> < ActiveRecord::Migrat
<% end %>
drop_table :refinery_<%= "#{namespacing.underscore}_" if table_name != namespacing.underscore.pluralize -%><%= table_name %>
<% if localized? %>
Refinery::<%= namespacing %>::<%= class_name %>.drop_translation_table!
drop_table :<%= localized_table_name %>
<% end %>
end

end
9 changes: 9 additions & 0 deletions core/lib/refinery/core/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ def refinery_inclusion!
WillPaginate.per_page = 20
end

initializer "refinery.mobility" do
Mobility.configure do |config|
config.default_backend = :table
config.accessor_method = :translates
config.query_method = :i18n
config.default_options[:dirty] = true
end
end

before_inclusion do
Refinery::Plugin.register do |plugin|
plugin.pathname = root
Expand Down
7 changes: 7 additions & 0 deletions core/lib/refinery/extension_generation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ def localized_attributes
@localized_attributes ||= attributes.select{ |a| options[:i18n].include?(a.name)}
end

def localized_table_name
localized_table_name = [ 'refinery']
localized_table_name << namespacing.underscore if table_name != namespacing.underscore.pluralize
localized_table_name << [ singular_table_name, 'translations']
localized_table_name.join('_')
end

def attributes_for_translation_table
localized_attributes.inject([]) { |memo, attr| memo << ":#{attr.name} => :#{attr.type}"}.join(', ')
end
Expand Down
3 changes: 1 addition & 2 deletions core/refinerycms-core.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ Gem::Specification.new do |s|

s.required_ruby_version = Refinery::Version.required_ruby_version

s.add_dependency 'refinerycms-i18n', ['~> 4.0', '>= 4.0.0']
s.add_dependency 'awesome_nested_set', ['~> 3.0', '>= 3.0.0']
s.add_dependency 'refinerycms-i18n', ['~> 5.0', '>= 5.0.0']
s.add_dependency 'railties', rails_version
s.add_dependency 'activerecord', rails_version
s.add_dependency 'actionpack', rails_version
Expand Down
6 changes: 3 additions & 3 deletions core/spec/helpers/refinery/translation_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ module Refinery
let(:page) { FactoryBot.build(:page) }

before do
Globalize.with_locale(:en) do
Mobility.with_locale(:en) do
page.title = "draft"
page.save!
end

Globalize.with_locale(:lv) do
Mobility.with_locale(:lv) do
page.title = "melnraksts"
page.save!
end
Expand All @@ -33,7 +33,7 @@ module Refinery

context "when title for current locale isn't available" do
it "returns existing title from translations" do
Page.translation_class.where(locale: :en).first.destroy
Page::Translation.where(locale: :en).first.destroy
expect(helper.translated_field(page, :title)).to eq("melnraksts")
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ module Refinery
let(:page) { FactoryBot.build(:page) }

before do
Globalize.with_locale(:en) do
Mobility.with_locale(:en) do
page.title = "draft"
page.save!
end

Globalize.with_locale(:lv) do
Mobility.with_locale(:lv) do
page.title = "melnraksts"
page.save!
end
Expand All @@ -25,7 +25,7 @@ module Refinery

context "when title for current locale isn't available" do
it "returns existing title from translations" do
Page.translation_class.where(locale: :en).first.destroy
Page::Translation.where(locale: :en).first.destroy
expect(TranslatedFieldPresenter.new(page).call(:title)).to eq("melnraksts")
end
end
Expand Down
4 changes: 1 addition & 3 deletions images/app/models/refinery/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

module Refinery
class Image < Refinery::Core::BaseModel
extend Mobility
translates :image_title, :image_alt

attribute :image_title
attribute :image_alt

dragonfly_accessor :image, :app => :refinery_images

include Images::Validators
Expand Down
2 changes: 1 addition & 1 deletion images/app/views/refinery/admin/images/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
object: @image,
include_object_name: false %>
<%= render '/refinery/admin/locale_picker', :current_locale => Globalize.locale if @image.persisted? %>
<%= render '/refinery/admin/locale_picker', :current_locale => Mobility.locale if @image.persisted? %>

<div class="field">
<% if action_name =~ /(edit)|(update)/ %>
Expand Down
30 changes: 13 additions & 17 deletions images/db/migrate/20150430171341_translate_refinery_images.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
class TranslateRefineryImages < ActiveRecord::Migration[4.2]
def self.up
begin
::Refinery::Image.create_translation_table!({
image_alt: :string,
image_title: :string
}, {
:migrate_data => true
})
rescue NameError
warn "Refinery::Image was not defined!"
end
end
def change
create_table :refinery_image_translations do |t|

# Translated attribute(s)
t.string :image_alt
t.string :image_title

def self.down
begin
Refinery::Image.drop_translation_table! migrate_data: true
rescue NameError
warn "Refinery::Image was not defined!"
t.string :locale, null: false
t.integer :refinery_image_id, null: false

t.timestamps null: false
end

add_index :refinery_image_translations, :locale, name: :index_refinery_image_translations_on_locale
add_index :refinery_image_translations, [:refinery_image_id, :locale], name: :index_2f245f0c60154d35c851e1df2ffc4c86571726f0, unique: true
end
end
2 changes: 1 addition & 1 deletion images/lib/refinery/images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ def factory_paths
end

ActiveSupport.on_load(:active_record) do
require 'globalize'
require 'mobility'
end
6 changes: 2 additions & 4 deletions images/refinerycms-images.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ Gem::Specification.new do |s|
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- spec/*`.split("\n")

s.add_dependency 'refinerycms-dragonfly', '~> 1.0'
s.add_dependency 'globalize', ['>= 5.1.0.beta1', '< 5.2']
s.add_dependency 'activemodel-serializers-xml', '~> 1.0', '>= 1.0.1'
s.add_dependency 'refinerycms-core', version
s.add_dependency 'refinerycms-core', version
s.add_dependency 'refinerycms-dragonfly', '~> 1.0'

s.required_ruby_version = Refinery::Version.required_ruby_version

Expand Down
2 changes: 1 addition & 1 deletion images/spec/support/shared_examples/image_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
click_button "Save"

expect(page).to have_content("'Titre de la première image' was successfully updated.")
expect(Refinery::Image.translation_class.count).to eq(1)
expect(Refinery::Image::Translation.count).to eq(1)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def new_page_part_params

private
def permitted_new_page_part_params
[:title, :slug, :body, :locale]
[:title, :slug, :body]
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions pages/app/controllers/refinery/admin/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ def find_page

# We can safely assume ::Refinery::I18n is defined because this method only gets
# Invoked when the before_action from the plugin is run.
def globalize!
def mobility!
return super unless action_name.to_s == 'index'

# Always display the tree of pages from the default frontend locale.
if Refinery::I18n.built_in_locales.keys.map(&:to_s).include?(params[:switch_locale])
Globalize.locale = params[:switch_locale].try(:to_sym)
Mobility.locale = params[:switch_locale].try(:to_sym)
else
Globalize.locale = Refinery::I18n.default_frontend_locale
Mobility.locale = Refinery::I18n.default_frontend_locale
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ class PagesDialogsController < ::Refinery::Admin::DialogsController

def link_to
# Get the switch_local variable to determine the locale we're currently editing
# Set up Globalize with our current locale
Globalize.locale = if params[:switch_locale].present? && Refinery::I18n.built_in_locales.keys.map(&:to_s).include?(params[:switch_locale])
Globalize.locale = params[:switch_locale]
# Set up Mobility with our current locale
Mobility.locale = if params[:switch_locale].present? && Refinery::I18n.built_in_locales.keys.map(&:to_s).include?(params[:switch_locale])
Mobility.locale = params[:switch_locale]
else
Refinery::I18n.default_locale
end

@pages = ::Refinery::Page.roots.paginate(:page => params[:page], :per_page => ::Refinery::Page.per_page(true))

@pages = @pages.with_globalize
@pages = @pages.with_mobility

if ::Refinery::Plugins.registered.names.include?('refinery_files')
@resources = Resource.paginate(:page => params[:resource_page], :per_page => Resource.per_page(true)).
Expand Down
Loading

0 comments on commit d181fbb

Please sign in to comment.