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

Switch from Globalize to Mobility #3360

Merged
merged 37 commits into from
May 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
c6a8b87
WIP : Move from Globalize to Mobility
bricesanchez Mar 24, 2018
d52da09
FIXME: emove create_translation_table!` method
bricesanchez Mar 24, 2018
1599b29
Fix translated seo_meta fields with mobility
bricesanchez Mar 24, 2018
46c012b
Fix undefined translation_class
bricesanchez Mar 26, 2018
f326cd7
Fix translation_for with *_backend.read
bricesanchez Mar 26, 2018
6c361a1
Lock mysql2 gem to '~> 0.4.10'
bricesanchez Mar 27, 2018
ae8f863
Remove unnecessary attribute for Globalize
bricesanchez Mar 27, 2018
3a7253c
Remove hacks for Globalize
bricesanchez Mar 27, 2018
4de5a92
Be sure ActiveRecord::RecordNotFound is the root namespace
bricesanchez Mar 27, 2018
8d34839
Fix page_menu spec
bricesanchez Mar 27, 2018
f27bba3
Rename globalize to mobility
bricesanchez Mar 29, 2018
6317d1f
Includes :translations for has_many :parts if respond_to?(:mobility)
bricesanchez Mar 29, 2018
1a46b13
PoC Try to fix scope bug with mobility
bricesanchez Mar 29, 2018
f28a68d
Remove unnecessary mobility index
bricesanchez Apr 6, 2018
53ed47d
Remove default_scope i18n
bricesanchez Apr 6, 2018
06e9a73
Improve Refiner::Page#slug_locale for mobility
bricesanchez Apr 6, 2018
5eecdae
Remove unnecessary translations_conditions method
bricesanchez Apr 6, 2018
b7a644b
Revert "Remove unnecessary translations_conditions method"
bricesanchez Apr 6, 2018
332774a
Fix should_generate_new_friendly_id? method
bricesanchez Apr 6, 2018
550cdc4
Fix translated_to_default_locale?: compare symbols
bricesanchez Apr 8, 2018
b1312d0
Remove hacks for Globalize 2
bricesanchez Apr 8, 2018
5dc3072
Fix Mobility fallbacks
bricesanchez Apr 8, 2018
658c652
Remove :locale from permitted_new_page_part_params
bricesanchez Apr 8, 2018
544af04
Use ::I18n.fallbacks instead of Mobility.new_fallbacks
bricesanchez Apr 10, 2018
4f1d9fc
Clean friendly_id_options in Page model
bricesanchez Apr 10, 2018
3bb95a0
Add after_save callback to save seo meta for the translation in the c…
bricesanchez Apr 10, 2018
398dbd4
Temp add mobility gem from table_finder_extension branch
bricesanchez Apr 10, 2018
36a3b7b
FIX localized create_table in engine generator
bricesanchez Apr 10, 2018
23c9cb2
Switch mobility to master branch
bricesanchez Apr 10, 2018
54d4fbf
Page Mobility: replace `in` method for `in_locale` method
bricesanchez Apr 11, 2018
bef22e5
Remove awesome_nested_set dependency from core
bricesanchez Apr 11, 2018
cfb024f
Remove mobility dependency from images and
bricesanchez Apr 11, 2018
2c40d60
Clean images, pages and resources dependencies
bricesanchez Apr 11, 2018
0d260cf
Remove moibility from Gemfile
bricesanchez May 21, 2018
f1f7fbc
Update refinerycms-i18n to 5.0.0
bricesanchez May 21, 2018
f48af77
update refinerycms-i18n branch
parndt May 24, 2018
e214351
Remove refinerycms-i18n from Gemfile
parndt May 25, 2018
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
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