has_multilingual_attributes 0.0.1
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.
- data/.gitignore +1 -0
- data/LICENSE +20 -0
- data/README.md +40 -0
- data/has_multilingual_attributes.gemspec +17 -0
- data/lib/has_multilingual_attributes.rb +2 -0
- data/lib/has_multilingual_attributes/multilingual_attributes.rb +21 -0
- data/lib/has_multilingual_attributes/railtie.rb +14 -0
- data/lib/has_multilingual_attributes/version.rb +3 -0
- metadata +55 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.gem
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 Martin Carel
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# has_multilingual_attributes
|
2
|
+
|
3
|
+
## Intro
|
4
|
+
* This gem is a very simple monkey patch into the ActiveRecord class.
|
5
|
+
* It gives you a locale-aware model attribute, provided you appropriately named your columns in db.
|
6
|
+
* The convention for the field naming is: [field name] + '_' + [locale], e.g. 'description_en'.
|
7
|
+
* Only supports 2 locales (:fr and :en) for now.
|
8
|
+
|
9
|
+
## How It Works
|
10
|
+
Suppose you want a locale-aware `description` field for your ActiveRecord model `Item`. With this:
|
11
|
+
|
12
|
+
```
|
13
|
+
class Item < ActiveRecord::Base
|
14
|
+
# that implies you have those 2 database fields in your items table:
|
15
|
+
# 'description_fr' and 'description_en'
|
16
|
+
has_multilingual_attributes :description
|
17
|
+
end
|
18
|
+
```
|
19
|
+
|
20
|
+
You'll get a virtual attribute `description` which content will vary according to the current locale:
|
21
|
+
|
22
|
+
```
|
23
|
+
> item = Item.new :description_fr => 'desc fr', :description_en => 'desc en'
|
24
|
+
=> #<Item id: nil, description_fr: "desc fr", description_en: "desc en">
|
25
|
+
|
26
|
+
> I18n.locale = :fr
|
27
|
+
=> :fr
|
28
|
+
|
29
|
+
> item.description
|
30
|
+
=> "desc fr"
|
31
|
+
|
32
|
+
> I18n.locale = :en
|
33
|
+
=> :en
|
34
|
+
|
35
|
+
> item.description
|
36
|
+
=> "desc en"
|
37
|
+
```
|
38
|
+
|
39
|
+
## Todo
|
40
|
+
* Read supported locales from config.
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/has_multilingual_attributes/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.name = 'has_multilingual_attributes'
|
6
|
+
gem.summary = "Access ActiveRecord model attributes regardless of the current locale."
|
7
|
+
gem.description = "If your model Item has both the description_fr and a description_en attributes, you can access your item's description in the current locale using 'item.description'."
|
8
|
+
|
9
|
+
gem.homepage = 'https://github.com/cawel/has_multilingual_attributes'
|
10
|
+
gem.version = HasMultilingualAttributes::VERSION
|
11
|
+
gem.date = '2013-09-13'
|
12
|
+
gem.authors = ["Martin Carel", "Hugo Frappier"]
|
13
|
+
gem.email = 'cawel00@yahoo.com'
|
14
|
+
gem.files = `git ls-files`.split("\n")
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.license = 'MIT'
|
17
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module MultilingualAttributes
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
module ClassMethods
|
5
|
+
# defines virtual attribute getters for the given attribute names
|
6
|
+
def has_multilingual_attributes(*names)
|
7
|
+
names.each do |name|
|
8
|
+
['fr', 'en'].each do |locale|
|
9
|
+
define_singleton_method "find_by_#{name.to_s}" do |name_param|
|
10
|
+
send("find_by_#{name}_#{locale}".to_sym, name_param)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
define_method "#{name}" do
|
15
|
+
send(name.to_s+"_#{I18n.locale.to_s}")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'has_multilingual_attributes/multilingual_attributes'
|
2
|
+
|
3
|
+
module HasMultilingualAttributes
|
4
|
+
class Railtie < ::Rails::Railtie
|
5
|
+
initializer "has_multilingual_attributes.initialize" do |app|
|
6
|
+
|
7
|
+
ActiveRecord::Base.class_eval do
|
8
|
+
include ::MultilingualAttributes
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: has_multilingual_attributes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Martin Carel
|
9
|
+
- Hugo Frappier
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2013-09-13 00:00:00.000000000 Z
|
14
|
+
dependencies: []
|
15
|
+
description: If your model Item has both the description_fr and a description_en attributes,
|
16
|
+
you can access your item's description in the current locale using 'item.description'.
|
17
|
+
email: cawel00@yahoo.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- has_multilingual_attributes.gemspec
|
26
|
+
- lib/has_multilingual_attributes.rb
|
27
|
+
- lib/has_multilingual_attributes/multilingual_attributes.rb
|
28
|
+
- lib/has_multilingual_attributes/railtie.rb
|
29
|
+
- lib/has_multilingual_attributes/version.rb
|
30
|
+
homepage: https://github.com/cawel/has_multilingual_attributes
|
31
|
+
licenses:
|
32
|
+
- MIT
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.8.25
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: Access ActiveRecord model attributes regardless of the current locale.
|
55
|
+
test_files: []
|