has_foreign_language 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/lib/form_fix.rb +19 -0
- data/lib/has_foreign_language.rb +44 -0
- data/spec/models/has_foreign_language_spec.rb +91 -0
- data/spec/spec_helper.rb +7 -0
- metadata +65 -0
data/lib/form_fix.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
module ActionView
|
2
|
+
module Helpers
|
3
|
+
class InstanceTag
|
4
|
+
class << self
|
5
|
+
def value(object, method_name)
|
6
|
+
method_name += "_#{I18n.locale}" if I18n.locale != I18n.default_locale && object.class.columns.select {|c| c.name == "#{method_name}_#{I18n.locale}"}.length > 0
|
7
|
+
object.send method_name unless object.nil?
|
8
|
+
end
|
9
|
+
|
10
|
+
def value_before_type_cast(object, method_name)
|
11
|
+
unless object.nil?
|
12
|
+
method_name += "_#{I18n.locale}" if I18n.locale != I18n.default_locale && object.class.columns.select {|c| c.name == "#{method_name}_#{I18n.locale}"}.length > 0
|
13
|
+
return object.respond_to?(method_name + "_before_type_cast") ? object.send(method_name + "_before_type_cast") :object.send(method_name)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# HasForeignLanguage
|
2
|
+
module Factore
|
3
|
+
module HasForeignLanguage
|
4
|
+
def self.included(mod)
|
5
|
+
mod.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
def has_foreign_language(*args)
|
11
|
+
args.each do |field|
|
12
|
+
# Define the Getter
|
13
|
+
define_method(field.to_s) do
|
14
|
+
if I18n.locale != I18n.default_locale && self.class.columns.select {|c| c.name == "#{field}_#{I18n.locale}"}.length > 0
|
15
|
+
result = self.send("#{field}_#{I18n.locale}".to_sym)
|
16
|
+
result.blank? ? super : self.send("#{field}_#{I18n.locale}".to_sym) #falling back to default if requested locale is nil
|
17
|
+
else
|
18
|
+
super
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Define the Setter
|
23
|
+
define_method("#{field}=") do |val|
|
24
|
+
if I18n.locale != I18n.default_locale && self.class.columns.select {|c| c.name == "#{field}_#{I18n.locale}"}.length > 0
|
25
|
+
self["#{field}_#{I18n.locale}".to_sym] = val
|
26
|
+
else
|
27
|
+
self[field.to_sym] = val
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Define the Default Getter
|
32
|
+
define_method("#{field}_#{I18n.default_locale}") do
|
33
|
+
self.send(field.to_sym)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Define the Default Setter
|
37
|
+
define_method("#{field}_#{I18n.default_locale}=") do |val|
|
38
|
+
self[field.to_sym] = val
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'i18n'
|
3
|
+
|
4
|
+
# Define some classes so we don't have to load Rails
|
5
|
+
class Object
|
6
|
+
def blank?
|
7
|
+
respond_to?(:empty?) ? empty? : !self
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class Column
|
12
|
+
attr_accessor :name
|
13
|
+
def initialize(name)
|
14
|
+
@name = name
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class FakeAR
|
19
|
+
include Factore::HasForeignLanguage
|
20
|
+
attr_accessor :title, :title_fr, :title_de
|
21
|
+
|
22
|
+
def initialize(title, title_fr, title_de)
|
23
|
+
@title, @title_fr, @title_de = title, title_fr, title_de
|
24
|
+
end
|
25
|
+
|
26
|
+
def []=(key,val)
|
27
|
+
self.send("#{key}=", val)
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.columns
|
31
|
+
%w{title title_de title_fr}.inject([]) {|sum, memo| sum << Column.new(memo) }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class Company < FakeAR
|
36
|
+
has_foreign_language :title
|
37
|
+
end
|
38
|
+
|
39
|
+
describe Company do
|
40
|
+
describe "With a default locale of English" do
|
41
|
+
before do
|
42
|
+
I18n.default_locale = :en
|
43
|
+
@company = Company.new("English", "French", "German")
|
44
|
+
end
|
45
|
+
|
46
|
+
it ".title should return 'English'" do
|
47
|
+
@company.title.should == "English"
|
48
|
+
end
|
49
|
+
|
50
|
+
it ".title should return 'French' when the locale is set to :fr" do
|
51
|
+
I18n.locale = :fr
|
52
|
+
@company.title.should == "French"
|
53
|
+
end
|
54
|
+
|
55
|
+
it ".title should return 'German' when the locale is set to :de" do
|
56
|
+
I18n.locale = :de
|
57
|
+
@company.title.should == "German"
|
58
|
+
end
|
59
|
+
|
60
|
+
it ".title_en should be the same as .title" do
|
61
|
+
@company.title_en.should == @company.title
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "With a default locale of Swedish" do
|
66
|
+
before do
|
67
|
+
I18n.default_locale = :sw
|
68
|
+
Company.send(:has_foreign_language, :title) # Have to send it again since default_locale has changed
|
69
|
+
@company = Company.new("Swedish", "French", "German")
|
70
|
+
end
|
71
|
+
|
72
|
+
it ".title should return 'Swedish'" do
|
73
|
+
@company.title = "Swedish"
|
74
|
+
@company.title.should == "Swedish"
|
75
|
+
end
|
76
|
+
|
77
|
+
it ".title should return 'French' when the locale is set to :fr" do
|
78
|
+
I18n.locale = :fr
|
79
|
+
@company.title.should == "French"
|
80
|
+
end
|
81
|
+
|
82
|
+
it ".title should return 'German' when the locale is set to :de" do
|
83
|
+
I18n.locale = :de
|
84
|
+
@company.title.should == "German"
|
85
|
+
end
|
86
|
+
|
87
|
+
it ".title_sw should be the same as .title" do
|
88
|
+
@company.title_sw.should == @company.title
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: has_foreign_language
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Sean Roberts
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2012-04-11 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Easy database internationalization gem for Ruby on Rails
|
22
|
+
email:
|
23
|
+
- sean@factore.ca
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- lib/form_fix.rb
|
32
|
+
- lib/has_foreign_language.rb
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://github.com/factore/has_foreign_language
|
35
|
+
licenses: []
|
36
|
+
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
version: "0"
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
requirements: []
|
57
|
+
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 1.3.6
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: Easy database internationalization gem for Ruby on Rails
|
63
|
+
test_files:
|
64
|
+
- spec/models/has_foreign_language_spec.rb
|
65
|
+
- spec/spec_helper.rb
|