acts_as_simple_translatable 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fc2edabeeef11fc661d785b3f3b02f4415188102
4
+ data.tar.gz: d7ee7d21b707caff96c9f9ad2ce1a8039ce9821d
5
+ SHA512:
6
+ metadata.gz: cd80ac0e02184eaf932af60455864a25fef46f79f5083a9e1c2cd827ced7dd37cbdc9286aff019933283944326f4e1de4a8cc491e795e6c6608cd10ab972055d
7
+ data.tar.gz: f2f6c18c88d352eb9529a1f909ee869915a0b647ab403cbce180a4bbc9aac35ed33f59964a0d859cc78518b6734ef3d91c64cd5012cd77ee8415d4bdbda351c5
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in acts_as_simple_translatable.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Stjepan Hadjic
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # ActsAsSimpleTranslatable
2
+
3
+ Adds translation table, and helpers to work with model translations
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'acts_as_simple_translatable'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install acts_as_simple_translatable
18
+
19
+ ## Configuration
20
+
21
+ This will create a translations table
22
+
23
+ $ rails generate acts_as_simple_translatable:install
24
+
25
+ To add translation to your model just add to your model:
26
+
27
+ acts_as_simple_translatable_on :name, :description
28
+
29
+ Add to application rb
30
+
31
+ I18n.avaliable_locales = ['en', 'de']
32
+
33
+ ## Usage
34
+
35
+ m = Model.create(name: 'name', descritpion: 'description' )
36
+ m.translations.create(locale: 'de', tanslatable_field: 'name', content: 'name_in_de')
37
+
38
+ I18n.locale = 'de'
39
+ m.name #=> 'name_in_de'
40
+ m.description #=> ''
41
+
42
+ Default locale content will be saved in original field in table (e.g. in table `Model`, attribute `name`) while for other locales content will be saved in `translations` table.
43
+
44
+ You also have the access to list of translatable fields:
45
+
46
+ Model.locale_fields #=> [:name, :description]
47
+
48
+ You can access to original field by prepending `_original` to attribute name:
49
+
50
+ description_original
51
+
52
+ Also, when you accessing to attribute and there is no translation for it, you can define custom message or default message will be returned.
53
+
54
+ my_model.name # => 'NO TRANSLATION' <-- default message
55
+
56
+ my_model.name('You need translate this') # => 'You need translate this' <-- custom message
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it ( http://github.com/d4be4st/acts_as_simple_translatable/fork )
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'acts_as_simple_translatable/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "acts_as_simple_translatable"
8
+ spec.version = ActsAsSimpleTranslatable::VERSION
9
+ spec.authors = ["Stjepan Hadjic"]
10
+ spec.email = ["d4be4st@gmail.com"]
11
+ spec.summary = %q{ acts as simple translatable}
12
+ spec.description = %q{ Took acts_as_translatable and stripped it down}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,5 @@
1
+ require 'acts_as_simple_translatable/class_methods'
2
+ require 'acts_as_simple_translatable/translation'
3
+ require 'acts_as_simple_translatable/version'
4
+
5
+ ActiveRecord::Base.extend ActsAsSimpleTranslatable::ClassMethods
@@ -0,0 +1,43 @@
1
+ module ActsAsSimpleTranslatable
2
+ module ClassMethods
3
+ def acts_as_simple_translatable_on(*fields)
4
+ # scope :with_translations, -> (locale) { joins(:translations).where(translations: {locale: locale}).preload(:translations) }
5
+ scope :with_translations, -> { includes(:translations) }
6
+ has_many :translations, as: :translatable, dependent: :destroy
7
+ accepts_nested_attributes_for :translations, reject_if: :all_blank, allow_destroy: :true
8
+
9
+ @locale_fields = fields
10
+ class << self
11
+ attr_accessor :locale_fields
12
+ end
13
+
14
+ # Loop through fields to define methods such as "name" and "description"
15
+ fields.each do |field|
16
+ define_method "#{field}" do |default_message = 'NO TRANSLATION'|
17
+ content = (I18n.locale == I18n.default_locale) ? super() : locale_translations[field]
18
+ content.present? ? content : default_message
19
+ end
20
+
21
+ define_method "#{field}_original" do
22
+ super()
23
+ end
24
+
25
+ define_method "#{field}?" do
26
+ !send("#{field}").blank?
27
+ end
28
+ end
29
+
30
+ define_method :locale_translations do
31
+ # load translations
32
+ unless @locale_translations
33
+ @locale_translations = {}
34
+ translations.select { |t| t.locale == I18n.locale.to_s }.each do |translation|
35
+ @locale_translations ||= {}
36
+ @locale_translations[translation.translatable_field.to_sym] = translation.content
37
+ end
38
+ end
39
+ @locale_translations
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,7 @@
1
+ class Translation < ActiveRecord::Base
2
+ belongs_to :translatable, polymorphic: true
3
+
4
+ scope :field, -> (field) { where(translatable_field: field) }
5
+ scope :ordered, -> { order(locale: :asc) }
6
+ scope :locale, -> (locale) { where(locale: locale) }
7
+ end
@@ -0,0 +1,3 @@
1
+ module ActsAsSimpleTranslatable
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,9 @@
1
+ Description:
2
+ Generates migration for acts_as_simple_translatable.
3
+
4
+ Example:
5
+ rails generate acts_as_simple_translatable en category name description
6
+
7
+ This will create a migration to translate the name and description fields in the categories table.
8
+ It will insert the existing category and description values in the record_translations table using the 'en' (English) locale.
9
+ Note: You cannot roll back this migration.
@@ -0,0 +1,19 @@
1
+ require 'rails/generators'
2
+
3
+ module ActsAsSimpleTranslatable
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+
8
+ source_root File.expand_path('../templates', __FILE__)
9
+
10
+ def install
11
+ migration_template "migration.rb", "db/migrate/create_translations.rb"
12
+ end
13
+
14
+ def self.next_migration_number(path)
15
+ @migration_number = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i.to_s
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,16 @@
1
+ class CreateTranslations < ActiveRecord::Migration
2
+ def change
3
+ # create translations table if it doesn't exist
4
+ create_table :translations do |t|
5
+ t.integer :translatable_id, required: true
6
+ t.string :translatable_type, required: true
7
+ t.string :translatable_field, required: true
8
+ t.string :locale, length: 5, required: true
9
+ t.text :content
10
+ t.timestamps
11
+ end
12
+
13
+ # add index
14
+ add_index :translations, [:translatable_id, :translatable_type, :locale], name: 'record_translations_index'
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_simple_translatable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Stjepan Hadjic
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: " Took acts_as_translatable and stripped it down"
42
+ email:
43
+ - d4be4st@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - acts_as_simple_translatable.gemspec
54
+ - lib/acts_as_simple_translatable.rb
55
+ - lib/acts_as_simple_translatable/class_methods.rb
56
+ - lib/acts_as_simple_translatable/translation.rb
57
+ - lib/acts_as_simple_translatable/version.rb
58
+ - lib/generators/acts_as_simple_translatable/USAGE
59
+ - lib/generators/acts_as_simple_translatable/install_generator.rb
60
+ - lib/generators/acts_as_simple_translatable/templates/migration.rb
61
+ homepage: ''
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.4.5
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: acts as simple translatable
85
+ test_files: []
86
+ has_rdoc: