Created
January 15, 2016 11:29
-
-
Save erino/eb4e1766f3203c58d0d3 to your computer and use it in GitHub Desktop.
Page and Slice localizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Page.all.each do |page| | |
Slices::PageLocalizer.new(page).localize | |
page.save | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Slices | |
class PageLocalizer | |
attr_reader :page | |
def initialize(page = nil) | |
@page = page | |
end | |
def localize | |
localize_fields(page) | |
page.embeded_slices do |embed_name| | |
page.slices_for(embed_name).each do |slice| | |
localize_fields(slice) | |
slice.embedded_relations.keys.each do |relation| | |
slice.send(relation).each do |r| | |
localize_fields(r) | |
end | |
end | |
end | |
end | |
end | |
private | |
def localize_fields(object) | |
object.class.localized_fields.each do |field, _| | |
value = object.attributes[field] | |
next if value.is_a?(Hash) | |
# We set the value to 'nil' so we can write the correct | |
# value and let Mongoid know the field has 'changed' | |
object.attributes[field] = {} | |
object.write_attribute(field, value) | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe Slices::PageLocalizer do | |
let :page do | |
Page.new.tap do |page| | |
page.attributes['name'] = 'Name' | |
page.attributes['title'] = 'Title' | |
page.meta_description = 'Meta Description' | |
end | |
end | |
let :slice do | |
TitleSlice.new.tap do |slice| | |
slice.attributes['title'] = 'Title' | |
end | |
end | |
let :attachment do | |
SlideshowSlice::Slide.new.tap do |slide| | |
slide.attributes['caption'] = 'Caption' | |
end | |
end | |
before do | |
page.slices << slice | |
page.slices << SlideshowSlice.new(slides: [attachment]) | |
end | |
context "before migration" do | |
it "has no translated values for a localized field" do | |
expect(page.name_translations).to eq 'Name' | |
end | |
it "has no translated values for a localized field on a slice" do | |
expect(slice.title_translations).to eq 'Title' | |
end | |
it "has no translated values for a localized attachment in a slice" do | |
expect(attachment.caption_translations).to eq 'Caption' | |
end | |
end | |
context "after localization" do | |
before do | |
Slices::PageLocalizer.new(page).localize | |
end | |
it "converts page fields to localized fields" do | |
expect(page.name).to eq 'Name' | |
expect(page.name_translations).to eq({ 'en' => 'Name' }) | |
end | |
it "converts fields on slices to localized fields" do | |
expect(slice.title).to eq 'Title' | |
expect(slice.title_translations).to eq({ 'en' => 'Title' }) | |
end | |
it "converts fields on slice attachments to localized fields" do | |
expect(attachment.caption).to eq 'Caption' | |
expect(attachment.caption_translations).to eq({ 'en' => 'Caption' }) | |
end | |
it "marks the fields as dirty" do | |
expect(page.name_changed?).to eq(true) | |
expect(slice.title_changed?).to eq(true) | |
expect(attachment.caption_changed?).to eq(true) | |
end | |
it "ignores already localized fields" do | |
expect(page.meta_description).to eq 'Meta Description' | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment