utf8_translatable 0.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/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,14 @@
1
+ Quick ActiveRecord Extension.
2
+ Adds self translating methods to translated ActiveRecord models
3
+ (inspired by https://github.com/citizencast/acts_as_translatable)
4
+
5
+ Example:
6
+ Given an ActiveRecord model with columns name_en and name_fr
7
+ Adding ensures_translated_and_utf8 to the model
8
+ will provide a _name method
9
+ that returns the correct (column) attribute value given current I18n.locale
10
+
11
+ Rspec : require 'utf8_translatable/spec_support' => provides you with a
12
+ shares example. To use it simply add it_behaves_like "utf8_translatable" to
13
+ your ActiveRecord models tests.
14
+ NB: this shared example only checks if the model is_utf8_translatable? == true.
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Utf8Translatable'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :utf8_translatable do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,6 @@
1
+ module Utf8Translatable
2
+ require_relative './utf8_translatable/base'
3
+
4
+ # include the extension
5
+ ActiveRecord::Base.send(:include, Utf8Translatable::Base)
6
+ end
@@ -0,0 +1,94 @@
1
+ require 'active_record'
2
+ require 'active_support/all'
3
+
4
+ # translates translatable columns (ex: title_en / title_fr => _title)
5
+ # ensures db queries return utf8 safe strings (monkey patching linked to ruby encoding issues in 2010)
6
+ module Utf8Translatable::Base
7
+ extend ActiveSupport::Concern
8
+
9
+ # Defines Methods to extend ActiveRecord::Base
10
+ module ClassMethods
11
+
12
+ # default state
13
+ def is_utf8_translatable?
14
+ false
15
+ end
16
+
17
+ # Defines '_#{column_name}' prefixed methods for each column of the table
18
+ # that returns the corresponding attribute value translated in the correct
19
+ # locale and utf8 safe encoded
20
+ #
21
+ # * *Warning*
22
+ # Documentation to be detailed
23
+ #
24
+ # * *NB*:
25
+ # Is defined in lib/model_helpers/ut8_translatable.rb,
26
+ # which is included in initializers/active_record_extensions.rb
27
+ #
28
+ # * *Examples*
29
+ # 1. Composer has columns firstname and lastname.
30
+ # Calling @composer._firstname will return the firstname ut8 encoded
31
+ # 2. Work has columns title_en and title_fr
32
+ # Calling @work._title will return @work.title_#{I18n.locale} utf8 encoded
33
+ #
34
+ def ensures_translated_and_utf8
35
+ if ActiveRecord::Base.connection.table_exists? table_name
36
+ columns.map{|c| {:name => c.name.to_s, :type => c.type.to_s}}.each do |col|
37
+ col_type, col_name = col[:type].try(:to_s), col[:name].try(:to_s)
38
+
39
+ if col_type == 'string' || col_type == 'text'
40
+ if is_translatable_attr?(col_type, col_name)
41
+ _attr = col_name.gsub("_#{I18n.default_locale}", '')
42
+
43
+ define_method _universal_method(col_name) do
44
+ if send("#{_attr}_#{I18n.locale}").nil? then nil else send("#{_attr}_#{I18n.locale}").force_encoding(Encoding::UTF_8) end
45
+ end
46
+
47
+ else
48
+ # ensure encoded in utf-8
49
+ _attr = col_name
50
+ _utf8_method = "_#{col_name}".to_sym
51
+
52
+ define_method _utf8_method do
53
+ if send(_attr).nil? then nil else send(_attr).force_encoding(Encoding::UTF_8) end
54
+ end
55
+
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ define_singleton_method :is_utf8_translatable? do
62
+ true
63
+ end
64
+
65
+ end
66
+
67
+ private
68
+ # Defines is the attribute corresponds to a translatable attributes
69
+ #
70
+ # To be a translatable attribute it must
71
+ # * be of type 'string' or 'text'
72
+ # * have a structure of type [attr_name]_[locale]
73
+ # * end with a locale that is in available_locales
74
+ # * correspond to the default locale (ensures universal method is built only once
75
+ # /+ that when it is built it has a fallback)
76
+ #
77
+ def is_translatable_attr?(col_type, col_name)
78
+ (col_type == 'string' || col_type == 'text') and col_name.split('_').last && I18n.available_locales.map(&:to_s).include?(col_name.split('_').last) && col_name.split('_').last.to_sym == I18n.default_locale
79
+ end
80
+
81
+ # Returns the universal method name for a translatable attr
82
+ #
83
+ # * *Example*
84
+ # if col[:name] == 'name_en'
85
+ # and is_translatable_attr?(col)
86
+ # _universal_method returns
87
+ # _name
88
+ #
89
+ def _universal_method(col_name)
90
+ # create universal translated attr
91
+ "_#{col_name.gsub("_#{I18n.default_locale}", '').to_sym}"
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,3 @@
1
+ shared_examples "utf8_translatable" do
2
+ it { described_class.is_utf8_translatable?.should eql true }
3
+ end
@@ -0,0 +1,3 @@
1
+ module Utf8Translatable
2
+ VERSION = "0.0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: utf8_translatable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nico Arbogast
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.8
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.8
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec-rails
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: activerecord-nulldb-adapter
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: ! 'Example: Given an ActiveRecord model with columns name_en and name_fr,
63
+ adding ensures_translated_and_utf8 to the model will provide a _name method returning
64
+ the correct column given current I18n.locale'
65
+ email:
66
+ - nicolas.arbogast@gmail.com
67
+ executables: []
68
+ extensions: []
69
+ extra_rdoc_files: []
70
+ files:
71
+ - lib/tasks/utf8_translatable_tasks.rake
72
+ - lib/utf8_translatable/base.rb
73
+ - lib/utf8_translatable/spec_support.rb
74
+ - lib/utf8_translatable/version.rb
75
+ - lib/utf8_translatable.rb
76
+ - MIT-LICENSE
77
+ - Rakefile
78
+ - README.rdoc
79
+ homepage: https://github.com/NicoArbogast/utf8_translatable
80
+ licenses: []
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 1.8.24
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: Adds self translating methods to translated ActiveRecord models
103
+ test_files: []