forked from spree/spree
-
Notifications
You must be signed in to change notification settings - Fork 0
Add mobility gem and basic usage for products #13
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
Open
adrianryt
wants to merge
14
commits into
fix/update_sanbox.sh
Choose a base branch
from
feature/mobility_gem
base: fix/update_sanbox.sh
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
13e973d
Merge pull request #11742 from upsidelab/fix/update_sanbox.sh
rafalcymerys aefbda4
add yarn build to sandbox.sh
adrianryt 1766009
Add mobility gem and basic usage for products
adrianryt 28d8a2c
fix spree_sample::load
adrianryt 4bee9d1
Add translations for taxons, fix loading taxons with spree_sample
adrianryt 66d128f
Permit :locale as param
adrianryt 0d34d84
Add mobility ransack and join translations table for taxons
adrianryt a29482a
fix ransack sorting
4003523
Downgrade mobility-ransack to 1.0.1
eb1d96c
Add friendly_id-mobility gem and translatable slugs
26ce55f
Small code changes connected to mobility gem
9250542
validates taxon name presence
8af9726
Add migration for copying existing data to translatable tables
113f7aa
Update tables after migrating translatable data
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
15 changes: 15 additions & 0 deletions
15
api/app/controllers/spree/api/v2/platform/translatable_resource_controller.rb
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Spree | ||
module Api | ||
module V2 | ||
module Platform | ||
class TranslatableResourceController < ResourceController | ||
def scope | ||
super.joins(:translations) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
|
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
Mobility.configure do | ||
# PLUGINS | ||
plugins do | ||
# Ransack plugin | ||
ransack | ||
# Backend | ||
# | ||
# Sets the default backend to use in models. This can be overridden in models | ||
# by passing +backend: ...+ to +translates+. | ||
# | ||
# To default to a different backend globally, replace +:key_value+ by another | ||
# backend name. | ||
# | ||
backend :table | ||
fallbacks | ||
locale_accessors | ||
|
||
# ActiveRecord | ||
# | ||
# Defines ActiveRecord as ORM, and enables ActiveRecord-specific plugins. | ||
active_record | ||
|
||
# Accessors | ||
# | ||
# Define reader and writer methods for translated attributes. Remove either | ||
# to disable globally, or pass +reader: false+ or +writer: false+ to | ||
# +translates+ in any translated model. | ||
# | ||
reader | ||
writer | ||
|
||
# Backend Reader | ||
# | ||
# Defines reader to access the backend for any attribute, of the form | ||
# +<attribute>_backend+. | ||
# | ||
backend_reader | ||
# | ||
# Or pass an interpolation string to define a different pattern: | ||
# backend_reader "%s_translations" | ||
|
||
# Query | ||
# | ||
# Defines a scope on the model class which allows querying on | ||
# translated attributes. The default scope is named +i18n+, pass a different | ||
# name as default to change the global default, or to +translates+ in any | ||
# model to change it for that model alone. | ||
# | ||
query | ||
|
||
# Cache | ||
# | ||
# Comment out to disable caching reads and writes. | ||
# | ||
cache | ||
|
||
# Dirty | ||
# | ||
# Uncomment this line to include and enable globally: | ||
dirty | ||
# | ||
# Or uncomment this line to include but disable by default, and only enable | ||
# per model by passing +dirty: true+ to +translates+. | ||
# dirty false | ||
|
||
# Fallbacks | ||
# | ||
# Uncomment line below to enable fallbacks, using +I18n.fallbacks+. | ||
# fallbacks | ||
# | ||
# Or uncomment this line to enable fallbacks with a global default. | ||
# fallbacks { :pt => :en } | ||
|
||
# Presence | ||
# | ||
# Converts blank strings to nil on reads and writes. Comment out to | ||
# disable. | ||
# | ||
presence | ||
|
||
# Default | ||
# | ||
# Set a default translation per attributes. When enabled, passing +default: | ||
# 'foo'+ sets a default translation string to show in case no translation is | ||
# present. Can also be passed a proc. | ||
# | ||
# default 'foo' | ||
|
||
# Fallthrough Accessors | ||
# | ||
# Uses method_missing to define locale-specific accessor methods like | ||
# +title_en+, +title_en=+, +title_fr+, +title_fr=+ for each translated | ||
# attribute. If you know what set of locales you want to support, it's | ||
# generally better to use Locale Accessors (or both together) since | ||
# +method_missing+ is very slow. (You can use both fallthrough and locale | ||
# accessor plugins together without conflict.) | ||
# | ||
# fallthrough_accessors | ||
|
||
# Locale Accessors | ||
# | ||
# Uses +def+ to define accessor methods for a set of locales. By default uses | ||
# +I18n.available_locales+, but you can pass the set of locales with | ||
# +translates+ and/or set a global default here. | ||
# | ||
# locale_accessors | ||
# | ||
# Or define specific defaults by uncommenting line below | ||
# locale_accessors [:en, :ja] | ||
|
||
# Attribute Methods | ||
# | ||
# Adds translated attributes to +attributes+ hash, and defines methods | ||
# +translated_attributes+ and +untranslated_attributes+ which return hashes | ||
# with translated and untranslated attributes, respectively. Be aware that | ||
# this plugin can create conflicts with other gems. | ||
# | ||
# attribute_methods | ||
end | ||
end |
This file contains hidden or 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
19 changes: 19 additions & 0 deletions
19
...0706112554_create_product_name_and_description_translations_for_mobility_table_backend.rb
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class CreateProductNameAndDescriptionTranslationsForMobilityTableBackend < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :spree_product_translations do |t| | ||
|
||
# Translated attribute(s) | ||
t.string :name | ||
t.text :description | ||
|
||
t.string :locale, null: false | ||
t.references :spree_product, null: false, foreign_key: true, index: false | ||
|
||
t.timestamps null: false | ||
end | ||
|
||
add_index :spree_product_translations, :locale, name: :index_spree_product_translations_on_locale | ||
add_index :spree_product_translations, [:spree_product_id, :locale], name: :index_89f757462683439a75913375358673bb7f45ebe0, unique: true | ||
|
||
end | ||
end |
7 changes: 7 additions & 0 deletions
7
...a_description_and_meta_keywords_and_meta_title_translations_for_mobility_table_backend.rb
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class CreateSpreeProductMetaDescriptionAndMetaKeywordsAndMetaTitleTranslationsForMobilityTableBackend < ActiveRecord::Migration[7.0] | ||
def change | ||
add_column :spree_product_translations, :meta_description, :text | ||
add_column :spree_product_translations, :meta_keywords, :string | ||
add_column :spree_product_translations, :meta_title, :string | ||
end | ||
end |
5 changes: 5 additions & 0 deletions
5
core/db/migrate/20220715120222_change_product_name_null_to_true.rb
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class ChangeProductNameNullToTrue < ActiveRecord::Migration[7.0] | ||
def change | ||
change_column_null :spree_products, :name, true | ||
end | ||
end |
19 changes: 19 additions & 0 deletions
19
...100743_create_spree_taxon_name_and_description_translations_for_mobility_table_backend.rb
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class CreateSpreeTaxonNameAndDescriptionTranslationsForMobilityTableBackend < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :spree_taxon_translations do |t| | ||
|
||
# Translated attribute(s) | ||
t.string :name | ||
t.text :description | ||
|
||
t.string :locale, null: false | ||
t.references :spree_taxon, null: false, foreign_key: true, index: false | ||
|
||
t.timestamps null: false | ||
end | ||
|
||
add_index :spree_taxon_translations, :locale, name: :index_spree_taxon_translations_on_locale | ||
add_index :spree_taxon_translations, [:spree_taxon_id, :locale], name: :index_spree_taxon_translations_on_spree_taxon_id_and_locale, unique: true | ||
|
||
end | ||
end |
5 changes: 5 additions & 0 deletions
5
core/db/migrate/20220718100948_change_taxon_name_null_to_true.rb
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class ChangeTaxonNameNullToTrue < ActiveRecord::Migration[7.0] | ||
def change | ||
change_column_null :spree_taxons, :name, true | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
core/db/migrate/20220802070609_add_locale_to_friendly_id_slugs.rb
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class AddLocaleToFriendlyIdSlugs < ActiveRecord::Migration[7.0] | ||
def change | ||
add_column :friendly_id_slugs, :locale, :string, null: :false, after: :scope | ||
|
||
remove_index :friendly_id_slugs, [:slug, :sluggable_type] | ||
add_index :friendly_id_slugs, [:slug, :sluggable_type, :locale], length: { slug: 140, sluggable_type: 50, locale: 2 } | ||
remove_index :friendly_id_slugs, [:slug, :sluggable_type, :scope] | ||
add_index :friendly_id_slugs, [:slug, :sluggable_type, :scope, :locale], length: { slug: 70, sluggable_type: 50, scope: 70, locale: 2 }, unique: true, name: :index_friendly_id_slugs_unique | ||
add_index :friendly_id_slugs, :locale | ||
end | ||
end |
5 changes: 5 additions & 0 deletions
5
...grate/20220802073225_create_spree_product_slug_translations_for_mobility_table_backend.rb
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class CreateSpreeProductSlugTranslationsForMobilityTableBackend < ActiveRecord::Migration[7.0] | ||
def change | ||
add_column :spree_product_translations, :slug, :string | ||
end | ||
end |
66 changes: 66 additions & 0 deletions
66
core/db/migrate/20220804073928_transfer_data_to_translatable_tables.rb
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
class TransferDataToTranslatableTables < ActiveRecord::Migration[7.0] | ||
DEFAULT_LOCALE = 'en' | ||
PRODUCTS_TABLE = 'spree_products' | ||
PRODUCT_TRANSLATIONS_TABLE = 'spree_product_translations' | ||
TAXONS_TABLE = 'spree_taxons' | ||
TAXON_TRANSLATIONS_TABLE = 'spree_taxon_translations' | ||
|
||
def up | ||
# Products | ||
ActiveRecord::Base.connection.execute(" | ||
INSERT INTO #{PRODUCT_TRANSLATIONS_TABLE} (name, description, locale, spree_product_id, created_at, updated_at, meta_description, meta_keywords, meta_title, slug) | ||
SELECT name, description, '#{DEFAULT_LOCALE}' as locale, id, created_at, updated_at, meta_description, meta_keywords, meta_title, slug FROM #{PRODUCTS_TABLE} | ||
") | ||
ActiveRecord::Base.connection.execute(" | ||
UPDATE #{PRODUCTS_TABLE} | ||
SET name=null, description=null, meta_description=null, meta_keywords=null, meta_title=null, slug=null; | ||
") | ||
#Taxons | ||
ActiveRecord::Base.connection.execute(" | ||
INSERT INTO #{TAXON_TRANSLATIONS_TABLE} (name, description, locale, spree_taxon_id, created_at, updated_at) | ||
SELECT name, description, '#{DEFAULT_LOCALE}' as locale, id, created_at, updated_at FROM #{TAXONS_TABLE} | ||
") | ||
ActiveRecord::Base.connection.execute(" | ||
UPDATE #{TAXONS_TABLE} | ||
SET name=null, description=null | ||
") | ||
end | ||
|
||
def down | ||
ActiveRecord::Base.connection.execute(" | ||
UPDATE #{PRODUCTS_TABLE} as products | ||
SET (name, | ||
description, | ||
meta_description, | ||
meta_keywords, | ||
meta_title, | ||
slug) = | ||
(t_products.name, | ||
t_products.description, | ||
t_products.meta_description, | ||
t_products.meta_keywords, | ||
t_products.meta_title, | ||
t_products.slug) | ||
FROM #{PRODUCT_TRANSLATIONS_TABLE} AS t_products | ||
WHERE t_products.spree_product_id = products.id | ||
") | ||
|
||
ActiveRecord::Base.connection.execute(" | ||
TRUNCATE TABLE #{PRODUCT_TRANSLATIONS_TABLE} | ||
") | ||
|
||
ActiveRecord::Base.connection.execute(" | ||
UPDATE #{TAXONS_TABLE} as taxons | ||
SET (name, | ||
description) = | ||
(t_taxons.name, | ||
t_taxons.description) | ||
FROM #{TAXON_TRANSLATIONS_TABLE} AS t_taxons | ||
WHERE t_taxons.spree_taxon_id = taxons.id | ||
") | ||
|
||
ActiveRecord::Base.connection.execute(" | ||
TRUNCATE TABLE #{TAXON_TRANSLATIONS_TABLE} | ||
") | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.