hstore_translate 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ if ENV["EDGE_RAILS"]
4
+ gem 'pg'
5
+ gem 'rails', :git => 'git://github.com/rails/rails'
6
+ else
7
+ gemspec
8
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,99 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ hstore_translate (0.0.3)
5
+ activerecord
6
+ activerecord-postgres-hstore (~> 0.4.0)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ actionmailer (3.2.10)
12
+ actionpack (= 3.2.10)
13
+ mail (~> 2.4.4)
14
+ actionpack (3.2.10)
15
+ activemodel (= 3.2.10)
16
+ activesupport (= 3.2.10)
17
+ builder (~> 3.0.0)
18
+ erubis (~> 2.7.0)
19
+ journey (~> 1.0.4)
20
+ rack (~> 1.4.0)
21
+ rack-cache (~> 1.2)
22
+ rack-test (~> 0.6.1)
23
+ sprockets (~> 2.2.1)
24
+ activemodel (3.2.10)
25
+ activesupport (= 3.2.10)
26
+ builder (~> 3.0.0)
27
+ activerecord (3.2.10)
28
+ activemodel (= 3.2.10)
29
+ activesupport (= 3.2.10)
30
+ arel (~> 3.0.2)
31
+ tzinfo (~> 0.3.29)
32
+ activerecord-postgres-hstore (0.4.1)
33
+ pg
34
+ rails
35
+ rake
36
+ activeresource (3.2.10)
37
+ activemodel (= 3.2.10)
38
+ activesupport (= 3.2.10)
39
+ activesupport (3.2.10)
40
+ i18n (~> 0.6)
41
+ multi_json (~> 1.0)
42
+ arel (3.0.2)
43
+ builder (3.0.4)
44
+ erubis (2.7.0)
45
+ hike (1.2.1)
46
+ i18n (0.6.1)
47
+ journey (1.0.4)
48
+ json (1.7.6)
49
+ mail (2.4.4)
50
+ i18n (>= 0.4.0)
51
+ mime-types (~> 1.16)
52
+ treetop (~> 1.4.8)
53
+ mime-types (1.19)
54
+ multi_json (1.5.0)
55
+ pg (0.14.1)
56
+ polyglot (0.3.3)
57
+ rack (1.4.1)
58
+ rack-cache (1.2)
59
+ rack (>= 0.4)
60
+ rack-ssl (1.3.2)
61
+ rack
62
+ rack-test (0.6.2)
63
+ rack (>= 1.0)
64
+ rails (3.2.10)
65
+ actionmailer (= 3.2.10)
66
+ actionpack (= 3.2.10)
67
+ activerecord (= 3.2.10)
68
+ activeresource (= 3.2.10)
69
+ activesupport (= 3.2.10)
70
+ bundler (~> 1.0)
71
+ railties (= 3.2.10)
72
+ railties (3.2.10)
73
+ actionpack (= 3.2.10)
74
+ activesupport (= 3.2.10)
75
+ rack-ssl (~> 1.3.2)
76
+ rake (>= 0.8.7)
77
+ rdoc (~> 3.4)
78
+ thor (>= 0.14.6, < 2.0)
79
+ rake (10.0.3)
80
+ rdoc (3.12)
81
+ json (~> 1.4)
82
+ sprockets (2.2.2)
83
+ hike (~> 1.2)
84
+ multi_json (~> 1.0)
85
+ rack (~> 1.0)
86
+ tilt (~> 1.1, != 1.3.0)
87
+ thor (0.16.0)
88
+ tilt (1.3.3)
89
+ treetop (1.4.12)
90
+ polyglot
91
+ polyglot (>= 0.3.1)
92
+ tzinfo (0.3.35)
93
+
94
+ PLATFORMS
95
+ ruby
96
+
97
+ DEPENDENCIES
98
+ hstore_translate!
99
+ rake
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Rob Worley
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.md ADDED
@@ -0,0 +1,90 @@
1
+ # hstore_translate
2
+
3
+ Rails I18n library for ActiveRecord model/data translation using PostgreSQL's
4
+ hstore datatype. It provides an interface inspired by
5
+ [Globalize3](https://github.com/svenfuchs/globalize3) but removes the need to
6
+ maintain separate translation tables.
7
+
8
+ [![Build Status](https://api.travis-ci.org/robworley/hstore_translate.png)](https://travis-ci.org/robworley/hstore_translate)
9
+
10
+ ## Requirements
11
+
12
+ * ActiveRecord > 3.1.0
13
+ * I18n
14
+
15
+ ## Installation
16
+
17
+ gem install hstore_translate
18
+
19
+ When using bundler, put it in your Gemfile:
20
+
21
+ ```ruby
22
+ source 'https://rubygems.org'
23
+
24
+ gem 'pg'
25
+ gem 'activerecord'
26
+ gem 'activerecord-postgres-hstore', '~> 0.4.0' # only required for ActiveRecord 3.x
27
+ gem 'hstore_translate'
28
+ ```
29
+
30
+ ## Model translations
31
+
32
+ Model translations allow you to translate your models' attribute values. E.g.
33
+
34
+ ```ruby
35
+ class Post < ActiveRecord::Base
36
+ translates :title, :text
37
+ end
38
+ ```
39
+
40
+ Allows you to translate the attributes :title and :text per locale:
41
+
42
+ ```ruby
43
+ I18n.locale = :en
44
+ post.title # => This database rocks!
45
+
46
+ I18n.locale = :he
47
+ post.title # => אתר זה טוב
48
+ ```
49
+
50
+ You also have locale-specific convenience methods:
51
+
52
+ ```ruby
53
+ I18n.locale = :en
54
+ post.title # => This database rocks!
55
+ post.title_he # => אתר זה טוב
56
+ ```
57
+
58
+ In order to make this work, you'll need to define an hstore column for each of
59
+ your translated attributes, using the suffix "_translations":
60
+
61
+ ```ruby
62
+ class CreatePosts < ActiveRecord::Migration
63
+ def up
64
+ create_table :posts do |t|
65
+ t.column :title_translations, 'hstore'
66
+ t.column :text_translations, 'hstore'
67
+ t.timestamps
68
+ end
69
+ end
70
+ def down
71
+ drop_table :posts
72
+ end
73
+ end
74
+ ```
75
+
76
+ ## I18n fallbacks for missing translations
77
+
78
+ It is possible to enable fallbacks for missing translations. It will depend
79
+ on the configuration setting you have set for I18n translations in your Rails
80
+ config.
81
+
82
+ You can enable them by adding the next line to `config/application.rb` (or
83
+ only `config/environments/production.rb` if you only want them in production)
84
+
85
+ ```ruby
86
+ config.i18n.fallbacks = true
87
+ ```
88
+
89
+ Sven Fuchs wrote a [detailed explanation of the fallback
90
+ mechanism](https://github.com/svenfuchs/i18n/wiki/Fallbacks).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ desc 'Default: run unit tests.'
5
+ task :default => :test
6
+
7
+ desc 'Test the ltree_hierarchy plugin.'
8
+ Rake::TestTask.new(:test) do |t|
9
+ t.libs << 'lib'
10
+ t.pattern = 'test/**/*_test.rb'
11
+ t.verbose = true
12
+ end
@@ -0,0 +1,13 @@
1
+ require "active_record/connection_adapters/postgresql_adapter"
2
+ require "hstore_translate/translates"
3
+ require "hstore_translate/version"
4
+
5
+ module HstoreTranslate
6
+ def self.native_hstore?
7
+ @native_hstore ||= ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES.key?(:hstore)
8
+ end
9
+ end
10
+
11
+ require 'activerecord-postgres-hstore' unless HstoreTranslate::native_hstore?
12
+
13
+ ActiveRecord::Base.extend(HstoreTranslate::Translates)
@@ -0,0 +1,64 @@
1
+ module HstoreTranslate
2
+ module Translates
3
+ def translates(*attrs)
4
+ include InstanceMethods
5
+
6
+ class_attribute :translated_attrs
7
+ self.translated_attrs = attrs
8
+
9
+ attrs.each do |attr_name|
10
+ serialize "#{attr_name}_translations", ActiveRecord::Coders::Hstore unless HstoreTranslate::native_hstore?
11
+
12
+ class_eval <<-RUBY
13
+ def #{attr_name}
14
+ read_hstore_translation('#{attr_name}')
15
+ end
16
+
17
+ def #{attr_name}=(value)
18
+ write_hstore_translation('#{attr_name}', value)
19
+ end
20
+ RUBY
21
+ end
22
+
23
+ alias_method_chain :method_missing, :translates
24
+ end
25
+
26
+ module InstanceMethods
27
+ protected
28
+
29
+ def read_hstore_translation(attr_name, locale = I18n.locale)
30
+ translations = send("#{attr_name}_translations") || {}
31
+ translation = translations[locale.to_s]
32
+
33
+ if translation.nil? && I18n.respond_to?(:fallbacks) && (fallbacks = I18n.fallbacks[locale])
34
+ fallbacks.find { |f| translation = translations[f.to_s] }
35
+ end
36
+
37
+ translation
38
+ end
39
+
40
+ def write_hstore_translation(attr_name, value, locale = I18n.locale)
41
+ translation_store = "#{attr_name}_translations"
42
+ translations = send(translation_store) || {}
43
+ translations[locale.to_s] = value
44
+ send("#{translation_store}=", translations)
45
+ value
46
+ end
47
+
48
+ def method_missing_with_translates(method_name, *args)
49
+ return method_missing_without_translates(method_name, *args) unless
50
+ method_name =~ /\A([a-z_]+)_([a-z]{2})(=?)\z/ &&
51
+ (attr_name = $1.to_sym) && translated_attrs.include?(attr_name)
52
+
53
+ locale = $2.to_sym
54
+ assigning = $3.present?
55
+
56
+ if assigning
57
+ write_hstore_translation(attr_name, args.first, locale)
58
+ else
59
+ read_hstore_translation(attr_name, locale)
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,3 @@
1
+ module HstoreTranslate
2
+ VERSION = "0.0.4"
3
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hstore_translate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rob Worley
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.1.0
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.1.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: activerecord-postgres-hstore
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.4.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.4.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
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: Rails I18n library for ActiveRecord model/data translation using PostgreSQL's
63
+ hstore datatype. Translations are stored directly in the model table rather than
64
+ shadow tables.
65
+ email: robert.worley@gmail.com
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - lib/hstore_translate/translates.rb
71
+ - lib/hstore_translate/version.rb
72
+ - lib/hstore_translate.rb
73
+ - Gemfile
74
+ - Gemfile.lock
75
+ - MIT-LICENSE
76
+ - Rakefile
77
+ - README.md
78
+ homepage: https://github.com/robworley/hstore_translate
79
+ licenses: []
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project: ! '[none]'
98
+ rubygems_version: 1.8.24
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: Rails I18n library for ActiveRecord model/data translation using PostgreSQL's
102
+ hstore datatype.
103
+ test_files: []