hstore_translations 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f8d5248b78e634c9887040589d24b67f7868c314
4
+ data.tar.gz: d3e96e929ab59b2eca0372015f35a5b3cd7ffa45
5
+ SHA512:
6
+ metadata.gz: 94da578c51984fc472806d573bf4733db73bfadfaf99e071dc8b10c7f35ada57ee9fb645021a719ea2317bad691631c6a1873a18db23a5424dd5043bec830142
7
+ data.tar.gz: 79d325668efadb4b8c4373d19627a2eb9d4b6a03f778ab19f886028d77d3b4aeb85e67ced5a023aea8522e4ec1824f261092aa8934e5f8d22eaf2a67ef0f460c
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rspec
19
+ spec/support/database.yml
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hstore_translations.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Aleksey Demidov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,27 @@
1
+ # HstoreTranslations
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ gem 'hstore_translations'
8
+
9
+ And then execute:
10
+
11
+ $ bundle
12
+
13
+ Or install it yourself as:
14
+
15
+ $ gem install hstore_translations
16
+
17
+ ## Usage
18
+
19
+ TODO: Write usage instructions here
20
+
21
+ ## Contributing
22
+
23
+ 1. Fork it
24
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
25
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
26
+ 4. Push to the branch (`git push origin my-new-feature`)
27
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hstore_translations/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "hstore_translations"
8
+ s.version = HstoreTranslations::VERSION
9
+ s.authors = ["Aleksey Demidov"]
10
+ s.email = ["aleksey.dem@gmail.com"]
11
+ s.summary = 'Translations for Rails 4 model columns, stored in hstore'
12
+ s.homepage = ""
13
+ s.license = "MIT"
14
+
15
+ s.files = `git ls-files`.split($/)
16
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_dependency "activerecord", "4.0.0.rc1"
21
+
22
+ s.add_development_dependency "bundler", "~> 1.3"
23
+ s.add_development_dependency "rake"
24
+ s.add_development_dependency "rspec"
25
+ s.add_development_dependency "pg"
26
+ end
@@ -0,0 +1,22 @@
1
+ require 'hstore_translations/version'
2
+ require 'hstore_translations/macro'
3
+ require 'hstore_translations/access_methods'
4
+ require 'hstore_translations/translations_lookup'
5
+
6
+ module HstoreTranslations
7
+ class << self
8
+ delegate :locale, :default_locale, :available_locales, to: :I18n
9
+
10
+ def fallbacks(for_locale = locale)
11
+ I18n.respond_to?(:fallbacks) ? I18n.fallbacks[for_locale] : default_fallbacks(for_locale)
12
+ end
13
+
14
+ private
15
+
16
+ def default_fallbacks(for_locale)
17
+ [for_locale.to_sym, default_locale].uniq
18
+ end
19
+ end
20
+ end
21
+
22
+ ActiveRecord::Base.send :extend, HstoreTranslations::Macro
@@ -0,0 +1,31 @@
1
+ module HstoreTranslations
2
+ module AccessMethods
3
+ extend ActiveSupport::Concern
4
+
5
+ def read_translated_attribute(name, locale)
6
+ read_store_attribute("#{name}_translations", locale)
7
+ end
8
+
9
+ def write_translated_attribute(name, locale, value)
10
+ write_store_attribute("#{name}_translations", locale, value)
11
+ end
12
+
13
+ module ClassMethods
14
+ def with_translated(name, value, locales = HstoreTranslations.fallbacks)
15
+ hstore_query_parts = Array(locales).map do |locale|
16
+ "#{table_name}.#{name}_translations @> '#{locale}=>#{value}'"
17
+ end
18
+ where(hstore_query_parts.join(' OR '))
19
+ end
20
+
21
+ def order_by_translated(arg, locale = HstoreTranslations.locale)
22
+ name, mode = if arg.is_a?(Hash)
23
+ [arg.keys.first, arg.values.first]
24
+ else
25
+ [arg, :asc]
26
+ end
27
+ order("#{table_name}.#{name}_translations->'#{locale}' #{mode}")
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,58 @@
1
+ module HstoreTranslations
2
+ module Macro
3
+
4
+ def translates(*attributes)
5
+ setup_hstore_translations
6
+ self.translatable_attributes |= attributes.map(&:to_sym)
7
+
8
+ attributes.each do |attribute|
9
+ define_translations_reader(attribute)
10
+ define_translations_writer(attribute)
11
+
12
+ HstoreTranslations.available_locales.each do |locale|
13
+ define_translations_reader(attribute, locale)
14
+ define_translations_writer(attribute, locale)
15
+ end
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def setup_hstore_translations
22
+ return if respond_to?(:translatable_attributes)
23
+
24
+ class_attribute :translatable_attributes
25
+ class_attribute :translations_methods
26
+
27
+ self.translatable_attributes = []
28
+ self.translations_methods = Module.new
29
+ translations_methods.define_singleton_method :inspect do
30
+ "HstoreTranslations::TranslationsMethods"
31
+ end
32
+
33
+ include translations_methods
34
+ include AccessMethods
35
+ end
36
+
37
+ def define_translations_reader(attribute, locale = nil)
38
+ name = locale ? "#{attribute}_#{locale}" : attribute
39
+ translations_methods.module_eval do
40
+ define_method(name) do
41
+ @translations_readers ||= {}
42
+ @translations_readers[name] ||= TranslationsLookup.new(self, attribute, locale)
43
+ @translations_readers[name].value
44
+ end
45
+ end
46
+ end
47
+
48
+ def define_translations_writer(attribute, locale = nil)
49
+ name = locale ? "#{attribute}_#{locale}=" : "#{attribute}="
50
+ translations_methods.module_eval do
51
+ define_method(name) do |value|
52
+ write_locale = locale || HstoreTranslations.locale
53
+ write_translated_attribute(attribute, write_locale, value)
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,28 @@
1
+ module HstoreTranslations
2
+ class TranslationsLookup
3
+ attr_reader :model, :attribute
4
+
5
+ def initialize(model, attribute, locale = nil)
6
+ @model, @attribute, @locale = model, attribute, locale
7
+ @locale = locale || HstoreTranslations.locale
8
+ end
9
+
10
+ def value
11
+ fallbacks.each do |locale|
12
+ value = try_locale(locale)
13
+ return value if value.present?
14
+ end
15
+ nil
16
+ end
17
+
18
+ private
19
+
20
+ def fallbacks
21
+ HstoreTranslations.fallbacks(@locale)
22
+ end
23
+
24
+ def try_locale(locale)
25
+ model.read_translated_attribute(attribute, locale)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module HstoreTranslations
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,85 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe HstoreTranslations::AccessMethods do
5
+ before { Post.send :include, HstoreTranslations::AccessMethods }
6
+ let(:post) { Post.new }
7
+
8
+ before do
9
+ configure_i18n locale: :ru,
10
+ default_locale: :en,
11
+ available_locales: [:ru, :en, :es]
12
+ Post.translates :title
13
+ end
14
+
15
+ describe '#write_translated_attribute' do
16
+ it 'writes value to hstore' do
17
+ post.write_translated_attribute(:title, :en, 'hello')
18
+ post.save!
19
+ post.reload.title_translations.should eq 'en' => 'hello'
20
+ end
21
+ end
22
+
23
+ describe '#read_translated_attribute' do
24
+ it 'reads value from hstore' do
25
+ post.title_translations = { :en => 'hello' }
26
+ post.save!
27
+ post.reload.read_translated_attribute(:title, :en).should eq 'hello'
28
+ end
29
+ end
30
+
31
+ describe '.with_translated' do
32
+ let!(:post_1) { Post.create!(title_ru: 'hello') }
33
+ let!(:post_2) { Post.create!(title_en: 'hello') }
34
+ let!(:post_3) { Post.create!(title_es: 'hello') }
35
+ let!(:post_4) { Post.create!(title_ru: 'привет') }
36
+
37
+ subject { Post.with_translated(:title, 'hello') }
38
+
39
+ context 'without locales given' do
40
+ it 'includes records with translated attribute from current locale' do
41
+ should include(post_1)
42
+ end
43
+ it 'includes records with translated attribute from default locale' do
44
+ should include(post_2)
45
+ end
46
+ it 'does not include records with another locales' do
47
+ should_not include(post_3)
48
+ end
49
+ it 'does not include records with another value' do
50
+ should_not include(post_4)
51
+ end
52
+ end
53
+ context 'with locale' do
54
+ subject { Post.with_translated(:title, 'hello', :ru) }
55
+
56
+ it 'includes matches from that locale' do
57
+ should include(post_1)
58
+ end
59
+ it 'does not include matches from another locales' do
60
+ should_not include(post_2, post_3, post_4)
61
+ end
62
+ end
63
+ end
64
+
65
+ describe '.order_by_translated' do
66
+ let!(:post_1) { Post.create!(title_ru: 'a', title_en: 'c') }
67
+ let!(:post_2) { Post.create!(title_ru: 'b', title_en: 'b') }
68
+ let!(:post_3) { Post.create!(title_ru: 'c', title_en: 'a') }
69
+
70
+ def ordered(*a)
71
+ Post.order_by_translated(*a).select { |p| [post_1, post_2, post_3].include?(p) }
72
+ end
73
+
74
+ it 'accepts hash' do
75
+ ordered(title: :desc).should eq [post_3, post_2, post_1]
76
+ end
77
+ it 'accepts symbol' do
78
+ ordered(:title).should eq [post_1, post_2, post_3]
79
+ end
80
+ it 'accepts locale' do
81
+ ordered(:title, :en).should eq [post_3, post_2, post_1]
82
+ end
83
+ end
84
+ end
85
+
@@ -0,0 +1,103 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe HstoreTranslations::Macro, '.translates' do
5
+ before do
6
+ Post.new.should_not respond_to :title, :body
7
+ end
8
+
9
+ it 'is available' do
10
+ Post.should respond_to :translates
11
+ end
12
+ it 'adds getters' do
13
+ Post.translates :title
14
+ Post.new.should respond_to :title
15
+ end
16
+ it 'adds setters' do
17
+ Post.translates :title
18
+ Post.new.should respond_to :title=
19
+ end
20
+ it 'adds locale-accessors' do
21
+ methods = [:title_en, :title_ru, :title_en=, :title_ru=]
22
+ Post.new.should_not respond_to *methods
23
+ Post.translates :title
24
+ Post.new.should respond_to *methods
25
+ end
26
+ it 'can be called multiple times' do
27
+ Post.translates :title
28
+ Post.new.should respond_to :title
29
+ Post.new.should_not respond_to :body
30
+
31
+ Post.translates :title, :body
32
+ Post.new.should respond_to :title, :body
33
+ end
34
+ it 'can be called with multiple attributes' do
35
+ Post.translates :title, :body
36
+ Post.new.should respond_to :title, :body
37
+ end
38
+ it 'adds attributes to list' do
39
+ Post.translates :title
40
+ Post.translatable_attributes.should eq [:title]
41
+ Post.translates :body
42
+ Post.translatable_attributes.should eq [:title, :body]
43
+ end
44
+ end
45
+
46
+ describe HstoreTranslations::Macro, 'generated accessors' do
47
+ before { Post.translates :title }
48
+
49
+ describe '#title=' do
50
+ let(:post) { Post.new }
51
+ before do
52
+ I18n.locale = :en
53
+ post.title = 'hello'
54
+ end
55
+
56
+ it 'assigns new value to current locale' do
57
+ post.title.should eq 'hello'
58
+ post.title_en.should eq 'hello'
59
+ end
60
+ it 'does not touch other locales' do
61
+ post.title_ru.should be_nil
62
+ end
63
+ end
64
+
65
+ describe '#title' do
66
+ let(:post) { Post.new(title_en: 'hello', title_ru: 'привет', title_es: 'hola') }
67
+
68
+ before do
69
+ I18n.locale = :en
70
+ I18n.default_locale = :ru
71
+ end
72
+
73
+ it 'is a title in current locale' do
74
+ post.title.should eq 'hello'
75
+ end
76
+
77
+ it 'returns nil when there is no translations' do
78
+ Post.new.title.should be_nil
79
+ end
80
+
81
+ context 'when title from current locale is empty' do
82
+ before { post.title_en = nil }
83
+
84
+ context 'and i18n fallbacks not enabled' do
85
+ it 'tries to load defaut locale' do
86
+ post.title.should eq 'привет'
87
+ end
88
+ end
89
+ context 'and i18n fallbacks enabled' do
90
+ before do
91
+ configure_i18n fallbacks: { en: [:es, :ru] }
92
+ end
93
+ after { I18n.fallbacks = false }
94
+
95
+ it 'looks for translation in fallback chain' do
96
+ post.title.should eq 'hola'
97
+ post.title_es = nil
98
+ post.title.should eq 'привет'
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,2 @@
1
+ require 'spec_helper'
2
+
@@ -0,0 +1,33 @@
1
+ require 'active_record'
2
+ require 'hstore_translations'
3
+ require 'i18n/backend/fallbacks'
4
+
5
+ db_config = YAML.load_file(File.expand_path('../support/database.yml', __FILE__))['test']
6
+ ActiveRecord::Base.establish_connection(db_config)
7
+ require 'support/schema'
8
+ require 'support/models'
9
+
10
+ RSpec.configure do |config|
11
+ config.treat_symbols_as_metadata_keys_with_true_values = true
12
+ config.run_all_when_everything_filtered = true
13
+ config.filter_run :focus
14
+
15
+ config.order = 'random'
16
+
17
+ config.before :each do
18
+ Object.send(:remove_const, :Post)
19
+ load 'support/models.rb'
20
+ end
21
+ config.after :each do
22
+ configure_i18n available_locales: [:ru, :en, :es],
23
+ default_locale: :ru,
24
+ locale: :ru,
25
+ fallbacks: false
26
+ end
27
+ end
28
+
29
+ def configure_i18n(options)
30
+ options.each do |key, value|
31
+ I18n.send("#{key}=", value)
32
+ end
33
+ end
@@ -0,0 +1,7 @@
1
+ test:
2
+ adapter: postgresql
3
+ host: localhost
4
+ username: postgres
5
+ encoding: utf8
6
+ database: hstore_translations_test
7
+ min_messages: warning
@@ -0,0 +1,2 @@
1
+ class Post < ActiveRecord::Base
2
+ end
@@ -0,0 +1,8 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ enable_extension 'hstore'
3
+
4
+ create_table :posts, :force => true do |t|
5
+ t.hstore :title_translations
6
+ t.hstore :body_translations
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hstore_translations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Aleksey Demidov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0.rc1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0.rc1
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pg
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - aleksey.dem@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - hstore_translations.gemspec
96
+ - lib/hstore_translations.rb
97
+ - lib/hstore_translations/access_methods.rb
98
+ - lib/hstore_translations/macro.rb
99
+ - lib/hstore_translations/translations_lookup.rb
100
+ - lib/hstore_translations/version.rb
101
+ - spec/hstore_translations/access_methods_spec.rb
102
+ - spec/hstore_translations/macro_spec.rb
103
+ - spec/hstore_translations_spec.rb
104
+ - spec/spec_helper.rb
105
+ - spec/support/database.yml.example
106
+ - spec/support/models.rb
107
+ - spec/support/schema.rb
108
+ homepage: ''
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.0.0
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Translations for Rails 4 model columns, stored in hstore
132
+ test_files:
133
+ - spec/hstore_translations/access_methods_spec.rb
134
+ - spec/hstore_translations/macro_spec.rb
135
+ - spec/hstore_translations_spec.rb
136
+ - spec/spec_helper.rb
137
+ - spec/support/database.yml.example
138
+ - spec/support/models.rb
139
+ - spec/support/schema.rb
140
+ has_rdoc: