rails-translate 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/rails-translate.rb +58 -0
  2. metadata +64 -0
@@ -0,0 +1,58 @@
1
+ module Rails
2
+ module Translate
3
+
4
+ def self.included(base)
5
+ base.extend(ActsMethods)
6
+ end
7
+
8
+ module ActsMethods
9
+
10
+ def translate(*args)
11
+
12
+ args.each do |arg|
13
+
14
+ define_method(arg.to_s) do
15
+ attribute = "#{arg}_#{I18n.locale}"
16
+ default_attribute = "#{arg}_#{I18n.default_locale}"
17
+ begin
18
+ return send(attribute).empty? ? send(default_attribute) : send(attribute)
19
+ rescue
20
+ return send(default_attribute)
21
+ end
22
+ end
23
+
24
+ define_method(arg.to_s + '=') do |data|
25
+ attribute = "#{arg}_#{I18n.locale}"
26
+ write_attribute attribute, data if self.respond_to? attribute
27
+ end
28
+
29
+ end
30
+
31
+ extend ClassMethods
32
+
33
+ end # translate
34
+
35
+ module ClassMethods
36
+
37
+ def method_missing(method, *args)
38
+ if method.to_s =~ /^find_(all_by|by)_([_a-zA-Z]\w*)$/
39
+ if column_methods_hash.include?($2.to_sym)
40
+ super
41
+ else
42
+ modifier = $1
43
+ attribute = "#{$2}_#{I18n.locale.to_s}"
44
+ send("find_#{modifier}_#{attribute}".to_sym, *args)
45
+ end
46
+ else
47
+ super
48
+ end
49
+ end
50
+
51
+ end
52
+
53
+ end
54
+
55
+ end
56
+ end
57
+
58
+ ActiveRecord::Base.send :include, Rails::Translate
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-translate
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.0
6
+ platform: ruby
7
+ authors:
8
+ - Francesc Esplugas
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-05 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 3.0.0
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ description:
27
+ email: contact@francescesplugas.com
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files: []
33
+
34
+ files:
35
+ - lib/rails-translate.rb
36
+ homepage: http://github.com/fesplugas/rails-translate
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.8.5
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Simplest translation mechanism for models.
63
+ test_files: []
64
+