translatable_records 1.0.7 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 05af5734e652d0b785cd48ba53f395430fc4e248
4
- data.tar.gz: 467134ed6c5695932738185040c0cb504a5f0944
3
+ metadata.gz: 853fe418c0b123941ce33d64f67872df5045cc6b
4
+ data.tar.gz: a2cc5c10f08c42a2eb29eebaafbabd035fad64eb
5
5
  SHA512:
6
- metadata.gz: ed349d8c6bc19c14b1df8cdcb15127ff26ac8cb7a3f8eedfa7ecda32cb1c21e54b9eeef17d762a425333b16724a781f9528a7f41186f4746cedb627f1c1f077d
7
- data.tar.gz: f010ff700fe1d44cb3a1703f8223850fc2b65a4652180cd08c59b05ac32f7050ca9702491a4a0db2b82f7ffd7763d06233a16f555f60e02a6dbdd0d2b9f771ca
6
+ metadata.gz: 166139b74c3c6bde0cfb6a34efc762c230914338b303ce5252a43ffa46d87f3ccd1dbe2112785295da21aa3c7fe38e4eba1d27343a5924650e380b6e3ab45238
7
+ data.tar.gz: ed33fe2d31a05cff0ec299e2ad1d11fcd052a7e92036ec381da566053f8287c1141ae0294a106525cc12ddfb52fa54192799f29aa416eef7e74d4446dfb86b9d
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2013 Museways
1
+ Copyright 2014 Museways
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.rdoc CHANGED
@@ -17,26 +17,26 @@ Then bundle:
17
17
  First define the model and set the name of the fields you want to translate with the attr_translatable method:
18
18
  attr_translatable :attr
19
19
 
20
+ (If you are using rails 3 you should add attr_accessible :attr to pass the atrr to create, update, etc)
21
+
20
22
  Generate the translation model and migration for that model:
21
23
  rails g translation Model
22
-
24
+
23
25
  Update the translation migration with the column for that field and remove the column from the original table:
24
26
  t.string :attr
25
-
27
+
26
28
  Update your db:
27
29
  rake db:migrate
28
-
29
- (Now you can use that field of the model as usual and any change would be saved using the current I18n.locale)
30
-
30
+
31
31
  If you want to change the locale to something different than I18n.locale:
32
- model_instance.with_locale :es
32
+ record.with_locale :es
33
33
 
34
34
  If you want to build a translation for each available locale:
35
- model_instance.build_translations
35
+ record.build_translations
36
36
 
37
37
  If you want to save multiple translations:
38
38
  <%= f.fields_for 'translations_attributes[]', t do |ff| %>
39
- <%= field_set_tag t("languages.#{t.locale}") do %>
39
+ <%= field_set_tag t("locales.#{t.locale}") do %>
40
40
  <% unless t.new_record? %>
41
41
  <%= ff.hidden_field :id %>
42
42
  <% end %>
@@ -0,0 +1,8 @@
1
+ class <%= "#{class_name}Translation" %> < ActiveRecord::Base
2
+
3
+ belongs_to <%= ":#{table_name}" %>
4
+
5
+ validates :locale, presence: true
6
+ validates <%= ":#{singular_table_name}_id" %>, uniqueness: { scope: :locale }
7
+
8
+ end
@@ -5,13 +5,13 @@ class TranslationGenerator < Rails::Generators::NamedBase
5
5
 
6
6
  source_root File.expand_path('../templates', __FILE__)
7
7
 
8
- def create_model
8
+ def create_translation_model
9
9
  @model = Object.const_get(class_name)
10
- template 'model.rb.erb', "app/models/#{file_name}_translation.rb"
10
+ template 'model.rb', "app/models/#{file_name}_translation.rb"
11
11
  end
12
12
 
13
- def create_migration
14
- migration_template 'migration.rb.erb', "db/migrate/create_#{singular_table_name}_translations.rb"
13
+ def create_translation_migration
14
+ migration_template 'migration.rb', "db/migrate/create_#{singular_table_name}_translations.rb"
15
15
  end
16
16
 
17
17
  def self.next_migration_number(path)
@@ -8,18 +8,18 @@ module TranslatableRecords
8
8
  def translatable?
9
9
  translatable_attrs.any?
10
10
  end
11
-
11
+
12
12
  def translatable_attrs
13
13
  @translatable_attrs ||= []
14
14
  end
15
15
 
16
16
  def attr_translatable(*args)
17
17
  make_translatable unless translatable?
18
- args.each do |name|
18
+ args.each do |name|
19
19
  define_translatable_attribute_methods name
20
20
  translatable_attrs << name
21
21
  end
22
- end
22
+ end
23
23
 
24
24
  protected
25
25
 
@@ -27,29 +27,28 @@ module TranslatableRecords
27
27
  include TranslatableRecords::ActiveRecord::Translatable
28
28
  default_scope -> { includes(:translations) }
29
29
  attr_accessible :translations_attributes if Rails::VERSION::MAJOR < 4
30
- has_many :translations, class_name: "#{name}Translation", autosave: true, dependent: :destroy
31
- accepts_nested_attributes_for :translations
30
+ has_many :translations, class_name: "#{name}Translation", autosave: true, dependent: :destroy
31
+ accepts_nested_attributes_for :translations
32
32
  end
33
33
 
34
34
  def define_translatable_attribute_methods(attr)
35
- ['setter', 'getter'].each { |method| send "define_translatable_attribute_#{method}", attr }
35
+ %w(setter getter).each { |method| send "define_translatable_attribute_#{method}", attr }
36
36
  end
37
37
 
38
38
  def define_translatable_attribute_setter(attr)
39
39
  name = :"#{attr}="
40
- define_method name do |value|
40
+ define_method name do |value|
41
41
  t = translation_by_locale(current_locale)
42
42
  t ? t.send(name, value) : translations.build(locale: current_locale.to_s, attr.to_sym => value)
43
- end
43
+ end
44
44
  end
45
45
 
46
46
  def define_translatable_attribute_getter(attr)
47
- ['', '_was', '_changed?'].each do |suffix|
47
+ %w(_was _changed?).unshift('').each do |suffix|
48
48
  name = :"#{attr}#{suffix}"
49
49
  define_method name do
50
- t = translation_by_locale(current_locale)
51
- t ? t.send(name) : nil
52
- end
50
+ translation_by_locale(current_locale).try(name)
51
+ end
53
52
  end
54
53
  end
55
54
 
@@ -4,7 +4,7 @@ module TranslatableRecords
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  def with_locale(locale)
7
- @locale = locale.to_sym
7
+ @current_locale = locale.to_sym
8
8
  end
9
9
 
10
10
  def build_translations
@@ -14,11 +14,11 @@ module TranslatableRecords
14
14
  protected
15
15
 
16
16
  def current_locale
17
- @locale || I18n.locale
17
+ @current_locale || I18n.locale
18
18
  end
19
19
 
20
20
  def translation_by_locale(locale)
21
- translations.find { |t| t.locale == locale.to_s } || translations.find_by_locale(locale)
21
+ translations.to_a.find { |t| t.locale == locale.to_s } || translations.find_by_locale(locale)
22
22
  end
23
23
 
24
24
  end
@@ -1,5 +1,5 @@
1
1
  module TranslatableRecords
2
2
 
3
- VERSION = '1.0.7'
3
+ VERSION = '1.1.1'
4
4
 
5
5
  end
@@ -1,3 +1,4 @@
1
1
  class Model < ActiveRecord::Base
2
+ attr_accessible :name if Rails::VERSION::MAJOR < 4
2
3
  attr_translatable :name
3
4
  end
@@ -1,9 +1,8 @@
1
1
  class ModelTranslation < ActiveRecord::Base
2
-
2
+
3
3
  belongs_to :models
4
-
5
- validates :locale, presence: true
6
- validates :locale, inclusion: { in: I18n.available_locales.map(&:to_s) }
7
- validates :model_id, uniqueness: { scope: :locale }
8
-
4
+
5
+ validates :locale, presence: true
6
+ validates :model_id, uniqueness: { scope: :locale }
7
+
9
8
  end
@@ -3,7 +3,7 @@ require File.expand_path('../boot', __FILE__)
3
3
  require 'rails/all'
4
4
 
5
5
  Bundler.require(*Rails.groups)
6
- require "translatable_records"
6
+ require 'translatable_records'
7
7
 
8
8
  module Dummy
9
9
  class Application < Rails::Application
@@ -18,6 +18,5 @@ module Dummy
18
18
  # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
19
19
  # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
20
20
  # config.i18n.default_locale = :de
21
- I18n.enforce_available_locales = false
22
21
  end
23
22
  end
@@ -9,4 +9,5 @@
9
9
 
10
10
  # Make sure your secret_key_base is kept private
11
11
  # if you're sharing your code publicly.
12
+ Dummy::Application.config.secret_token = '7df7e2131e8049bf1397fd7586bd3dfa812f69914f258ee99734a62b8cc16896147a9276e35b6349629341cf1a9b62025e277ea721be144377dbc3979e90a629'
12
13
  Dummy::Application.config.secret_key_base = '7df7e2131e8049bf1397fd7586bd3dfa812f69914f258ee99734a62b8cc16896147a9276e35b6349629341cf1a9b62025e277ea721be144377dbc3979e90a629'
@@ -1,7 +1,6 @@
1
1
  class CreateModels < ActiveRecord::Migration
2
2
  def change
3
3
  create_table :models do |t|
4
- t.string :name
5
4
 
6
5
  t.timestamps
7
6
  end
@@ -9,11 +9,11 @@
9
9
  # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10
10
  # you'll amass, the slower it'll run and the greater likelihood for issues).
11
11
  #
12
- # It's strongly recommended that you check this file into your version control system.
12
+ # It's strongly recommended to check this file into your version control system.
13
13
 
14
- ActiveRecord::Schema.define(version: 20130819165249) do
14
+ ActiveRecord::Schema.define(:version => 20130819165249) do
15
15
 
16
- create_table "model_translations", force: true do |t|
16
+ create_table "model_translations", :force => true do |t|
17
17
  t.integer "model_id"
18
18
  t.string "locale"
19
19
  t.string "name"
@@ -21,11 +21,10 @@ ActiveRecord::Schema.define(version: 20130819165249) do
21
21
  t.datetime "updated_at"
22
22
  end
23
23
 
24
- add_index "model_translations", ["locale"], name: "index_model_translations_on_locale"
25
- add_index "model_translations", ["model_id"], name: "index_model_translations_on_model_id"
24
+ add_index "model_translations", ["locale"], :name => "index_model_translations_on_locale"
25
+ add_index "model_translations", ["model_id"], :name => "index_model_translations_on_model_id"
26
26
 
27
- create_table "models", force: true do |t|
28
- t.string "name"
27
+ create_table "models", :force => true do |t|
29
28
  t.datetime "created_at"
30
29
  t.datetime "updated_at"
31
30
  end