translatable_records 1.1.7 → 4.0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/MIT-LICENSE +1 -1
- data/README.md +26 -22
- data/Rakefile +1 -14
- data/lib/generators/translation/templates/migration.rb +14 -0
- data/lib/generators/translation/templates/model.rb +8 -0
- data/lib/generators/translation/translation_generator.rb +25 -0
- data/lib/translatable_records/builder.rb +66 -0
- data/lib/translatable_records/concern.rb +22 -0
- data/lib/translatable_records/extensions/active_record/base.rb +26 -0
- data/lib/translatable_records/railtie.rb +4 -2
- data/lib/translatable_records/version.rb +1 -1
- data/lib/translatable_records.rb +4 -2
- data/test/dummy/Rakefile +1 -2
- data/test/dummy/app/assets/javascripts/application.js +2 -2
- data/test/dummy/app/assets/stylesheets/application.css +6 -4
- data/test/dummy/app/models/product.rb +5 -0
- data/test/dummy/app/models/product_translation.rb +8 -0
- data/test/dummy/app/views/layouts/application.html.erb +9 -11
- data/test/dummy/bin/bundle +1 -0
- data/test/dummy/bin/rails +2 -1
- data/test/dummy/bin/rake +1 -0
- data/test/dummy/bin/setup +30 -0
- data/test/dummy/config/application.rb +3 -0
- data/test/dummy/config/boot.rb +1 -1
- data/test/dummy/config/database.yml +4 -22
- data/test/dummy/config/database.yml.travis +3 -0
- data/test/dummy/config/environment.rb +1 -1
- data/test/dummy/config/environments/development.rb +15 -3
- data/test/dummy/config/environments/production.rb +21 -26
- data/test/dummy/config/environments/test.rb +10 -12
- data/test/dummy/config/initializers/assets.rb +11 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/test/dummy/config/initializers/mime_types.rb +1 -2
- data/test/dummy/config/initializers/session_store.rb +1 -1
- data/test/dummy/config/routes.rb +2 -2
- data/test/dummy/config/secrets.yml +22 -0
- data/test/dummy/config.ru +1 -1
- data/test/dummy/db/migrate/20130819155126_create_products.rb +8 -0
- data/test/dummy/db/migrate/20161205171056_create_product_translations.rb +12 -0
- data/test/dummy/db/schema.rb +13 -11
- data/test/dummy/log/development.log +39 -0
- data/test/dummy/log/test.log +1443 -62
- data/test/dummy/public/404.html +58 -55
- data/test/dummy/public/422.html +58 -55
- data/test/dummy/public/500.html +57 -54
- data/test/generator_test.rb +21 -0
- data/test/record_test.rb +38 -0
- data/test/test_helper.rb +5 -13
- metadata +42 -33
- data/lib/generators/templates/migration.rb +0 -12
- data/lib/generators/templates/model.rb +0 -8
- data/lib/generators/translation_generator.rb +0 -21
- data/lib/translatable_records/active_record/base.rb +0 -56
- data/lib/translatable_records/active_record/translatable.rb +0 -26
- data/test/dummy/README.rdoc +0 -28
- data/test/dummy/app/models/model.rb +0 -3
- data/test/dummy/app/models/model_translation.rb +0 -8
- data/test/dummy/config/initializers/secret_token.rb +0 -13
- data/test/dummy/db/migrate/20130819155126_create_models.rb +0 -8
- data/test/dummy/db/migrate/20130819165249_create_model_translations.rb +0 -13
- data/test/generators_test.rb +0 -19
- data/test/records_test.rb +0 -36
@@ -1,12 +0,0 @@
|
|
1
|
-
class <%= "Create#{singular_name.camelize}Translations" %> < ActiveRecord::Migration
|
2
|
-
def change
|
3
|
-
create_table <%= ":#{singular_table_name}_translations" %> do |t|
|
4
|
-
t.integer <%= ":#{singular_table_name}_id" %>
|
5
|
-
t.string :locale
|
6
|
-
|
7
|
-
t.timestamps
|
8
|
-
end
|
9
|
-
add_index <%= ":#{singular_table_name}_translations" %>, <%= ":#{singular_table_name}_id" %>
|
10
|
-
add_index <%= ":#{singular_table_name}_translations" %>, :locale
|
11
|
-
end
|
12
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
require 'rails/generators'
|
2
|
-
|
3
|
-
class TranslationGenerator < Rails::Generators::NamedBase
|
4
|
-
include Rails::Generators::Migration
|
5
|
-
|
6
|
-
source_root File.expand_path('../templates', __FILE__)
|
7
|
-
|
8
|
-
def create_translation_model
|
9
|
-
@model = Object.const_get(class_name)
|
10
|
-
template 'model.rb', "app/models/#{file_name}_translation.rb"
|
11
|
-
end
|
12
|
-
|
13
|
-
def create_translation_migration
|
14
|
-
migration_template 'migration.rb', "db/migrate/create_#{singular_table_name}_translations.rb"
|
15
|
-
end
|
16
|
-
|
17
|
-
def self.next_migration_number(path)
|
18
|
-
Time.now.utc.strftime '%Y%m%d%H%M%S'
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|
@@ -1,56 +0,0 @@
|
|
1
|
-
module TranslatableRecords
|
2
|
-
module ActiveRecord
|
3
|
-
module Base
|
4
|
-
extend ActiveSupport::Concern
|
5
|
-
module ClassMethods
|
6
|
-
|
7
|
-
def translatable?
|
8
|
-
translatable_attrs.any?
|
9
|
-
end
|
10
|
-
|
11
|
-
def translatable_attrs
|
12
|
-
@translatable_attrs ||= []
|
13
|
-
end
|
14
|
-
|
15
|
-
def attr_translatable(*args)
|
16
|
-
make_translatable unless translatable?
|
17
|
-
args.each do |name|
|
18
|
-
define_translatable_attribute_methods name
|
19
|
-
translatable_attrs << name
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
protected
|
24
|
-
|
25
|
-
def make_translatable
|
26
|
-
include TranslatableRecords::ActiveRecord::Translatable
|
27
|
-
default_scope -> { includes(:translations) }
|
28
|
-
has_many :translations, class_name: "#{name}Translation", autosave: true, dependent: :destroy
|
29
|
-
accepts_nested_attributes_for :translations
|
30
|
-
end
|
31
|
-
|
32
|
-
def define_translatable_attribute_methods(attr)
|
33
|
-
%w(setter getter).each { |method| send "define_translatable_attribute_#{method}", attr }
|
34
|
-
end
|
35
|
-
|
36
|
-
def define_translatable_attribute_setter(attr)
|
37
|
-
name = :"#{attr}="
|
38
|
-
define_method name do |value|
|
39
|
-
t = translation_by_locale(current_locale)
|
40
|
-
t ? t.send(name, value) : translations.build(locale: current_locale.to_s, attr.to_sym => value)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
def define_translatable_attribute_getter(attr)
|
45
|
-
%w(_was _changed?).unshift('').each do |suffix|
|
46
|
-
name = :"#{attr}#{suffix}"
|
47
|
-
define_method name do
|
48
|
-
translation_by_locale(current_locale).try(name)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
module TranslatableRecords
|
2
|
-
module ActiveRecord
|
3
|
-
module Translatable
|
4
|
-
extend ActiveSupport::Concern
|
5
|
-
|
6
|
-
def with_locale(locale)
|
7
|
-
@current_locale = locale.to_sym
|
8
|
-
end
|
9
|
-
|
10
|
-
def build_translations
|
11
|
-
I18n.available_locales.each { |locale| translations.build locale: locale.to_s unless translations.find_by_locale(locale) }
|
12
|
-
end
|
13
|
-
|
14
|
-
protected
|
15
|
-
|
16
|
-
def current_locale
|
17
|
-
@current_locale || I18n.locale
|
18
|
-
end
|
19
|
-
|
20
|
-
def translation_by_locale(locale)
|
21
|
-
translations.to_a.find { |t| t.locale == locale.to_s } || translations.find_by_locale(locale)
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
data/test/dummy/README.rdoc
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
== README
|
2
|
-
|
3
|
-
This README would normally document whatever steps are necessary to get the
|
4
|
-
application up and running.
|
5
|
-
|
6
|
-
Things you may want to cover:
|
7
|
-
|
8
|
-
* Ruby version
|
9
|
-
|
10
|
-
* System dependencies
|
11
|
-
|
12
|
-
* Configuration
|
13
|
-
|
14
|
-
* Database creation
|
15
|
-
|
16
|
-
* Database initialization
|
17
|
-
|
18
|
-
* How to run the test suite
|
19
|
-
|
20
|
-
* Services (job queues, cache servers, search engines, etc.)
|
21
|
-
|
22
|
-
* Deployment instructions
|
23
|
-
|
24
|
-
* ...
|
25
|
-
|
26
|
-
|
27
|
-
Please feel free to use a different markup language if you do not plan to run
|
28
|
-
<tt>rake doc:app</tt>.
|
@@ -1,13 +0,0 @@
|
|
1
|
-
# Be sure to restart your server when you modify this file.
|
2
|
-
|
3
|
-
# Your secret key is used for verifying the integrity of signed cookies.
|
4
|
-
# If you change this key, all old signed cookies will become invalid!
|
5
|
-
|
6
|
-
# Make sure the secret is at least 30 characters and all random,
|
7
|
-
# no regular words or you'll be exposed to dictionary attacks.
|
8
|
-
# You can use `rake secret` to generate a secure secret key.
|
9
|
-
|
10
|
-
# Make sure your secret_key_base is kept private
|
11
|
-
# if you're sharing your code publicly.
|
12
|
-
Dummy::Application.config.secret_token = '7df7e2131e8049bf1397fd7586bd3dfa812f69914f258ee99734a62b8cc16896147a9276e35b6349629341cf1a9b62025e277ea721be144377dbc3979e90a629'
|
13
|
-
Dummy::Application.config.secret_key_base = '7df7e2131e8049bf1397fd7586bd3dfa812f69914f258ee99734a62b8cc16896147a9276e35b6349629341cf1a9b62025e277ea721be144377dbc3979e90a629'
|
@@ -1,13 +0,0 @@
|
|
1
|
-
class CreateModelTranslations < ActiveRecord::Migration
|
2
|
-
def change
|
3
|
-
create_table :model_translations do |t|
|
4
|
-
t.integer :model_id
|
5
|
-
t.string :locale
|
6
|
-
t.string :name
|
7
|
-
|
8
|
-
t.timestamps
|
9
|
-
end
|
10
|
-
add_index :model_translations, :model_id
|
11
|
-
add_index :model_translations, :locale
|
12
|
-
end
|
13
|
-
end
|
data/test/generators_test.rb
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'rails/generators'
|
3
|
-
require 'generators/translation_generator'
|
4
|
-
|
5
|
-
class GeneratorsTest < Rails::Generators::TestCase
|
6
|
-
tests TranslationGenerator
|
7
|
-
destination File.expand_path('../tmp', File.dirname(__FILE__))
|
8
|
-
|
9
|
-
teardown do
|
10
|
-
FileUtils.rm_rf self.destination_root
|
11
|
-
end
|
12
|
-
|
13
|
-
test 'files generation' do
|
14
|
-
run_generator %w(model)
|
15
|
-
assert_file 'app/models/model_translation.rb'
|
16
|
-
assert_migration 'db/migrate/create_model_translations.rb'
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
data/test/records_test.rb
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class RecordsTest < ActiveSupport::TestCase
|
4
|
-
|
5
|
-
test 'associated translation creation' do
|
6
|
-
assert_equal 'name', record.name
|
7
|
-
end
|
8
|
-
|
9
|
-
test 'associated translation edition' do
|
10
|
-
record.name = 'new name'
|
11
|
-
record.save!
|
12
|
-
assert_equal 'new name', record.name
|
13
|
-
end
|
14
|
-
|
15
|
-
test 'associated translation deletion' do
|
16
|
-
record.destroy
|
17
|
-
assert_nil ModelTranslation.find_by_model_id(record.id)
|
18
|
-
end
|
19
|
-
|
20
|
-
test 'locale change' do
|
21
|
-
record.with_locale :es
|
22
|
-
assert_nil record.name
|
23
|
-
record.name = 'new name'
|
24
|
-
record.save!
|
25
|
-
assert_equal 'new name', record.name
|
26
|
-
record.with_locale :en
|
27
|
-
assert_equal 'name', record.name
|
28
|
-
end
|
29
|
-
|
30
|
-
private
|
31
|
-
|
32
|
-
def record
|
33
|
-
@record ||= Model.create(name: 'name')
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|