activerecord_globalize 1.0.0

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.
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 4.2.0"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,153 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ activerecord_globalize (0.1.0)
5
+ activerecord (>= 4.0.0)
6
+ railties (>= 4.0.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionmailer (4.2.5.1)
12
+ actionpack (= 4.2.5.1)
13
+ actionview (= 4.2.5.1)
14
+ activejob (= 4.2.5.1)
15
+ mail (~> 2.5, >= 2.5.4)
16
+ rails-dom-testing (~> 1.0, >= 1.0.5)
17
+ actionpack (4.2.5.1)
18
+ actionview (= 4.2.5.1)
19
+ activesupport (= 4.2.5.1)
20
+ rack (~> 1.6)
21
+ rack-test (~> 0.6.2)
22
+ rails-dom-testing (~> 1.0, >= 1.0.5)
23
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
24
+ actionview (4.2.5.1)
25
+ activesupport (= 4.2.5.1)
26
+ builder (~> 3.1)
27
+ erubis (~> 2.7.0)
28
+ rails-dom-testing (~> 1.0, >= 1.0.5)
29
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
30
+ activejob (4.2.5.1)
31
+ activesupport (= 4.2.5.1)
32
+ globalid (>= 0.3.0)
33
+ activemodel (4.2.5.1)
34
+ activesupport (= 4.2.5.1)
35
+ builder (~> 3.1)
36
+ activerecord (4.2.5.1)
37
+ activemodel (= 4.2.5.1)
38
+ activesupport (= 4.2.5.1)
39
+ arel (~> 6.0)
40
+ activesupport (4.2.5.1)
41
+ i18n (~> 0.7)
42
+ json (~> 1.7, >= 1.7.7)
43
+ minitest (~> 5.1)
44
+ thread_safe (~> 0.3, >= 0.3.4)
45
+ tzinfo (~> 1.1)
46
+ appraisal (2.1.0)
47
+ bundler
48
+ rake
49
+ thor (>= 0.14.0)
50
+ arel (6.0.3)
51
+ builder (3.2.2)
52
+ byebug (9.0.6)
53
+ codeclimate-test-reporter (1.0.3)
54
+ simplecov
55
+ concurrent-ruby (1.0.2)
56
+ database_cleaner (1.5.3)
57
+ diff-lcs (1.2.5)
58
+ docile (1.1.5)
59
+ erubis (2.7.0)
60
+ globalid (0.3.6)
61
+ activesupport (>= 4.1.0)
62
+ i18n (0.7.0)
63
+ json (1.8.3)
64
+ loofah (2.0.3)
65
+ nokogiri (>= 1.5.9)
66
+ mail (2.6.4)
67
+ mime-types (>= 1.16, < 4)
68
+ mime-types (3.1)
69
+ mime-types-data (~> 3.2015)
70
+ mime-types-data (3.2016.0521)
71
+ mini_portile2 (2.1.0)
72
+ minitest (5.9.0)
73
+ nokogiri (1.6.8)
74
+ mini_portile2 (~> 2.1.0)
75
+ pkg-config (~> 1.1.7)
76
+ pg (0.18.4)
77
+ pkg-config (1.1.7)
78
+ rack (1.6.4)
79
+ rack-test (0.6.3)
80
+ rack (>= 1.0)
81
+ rails (4.2.5.1)
82
+ actionmailer (= 4.2.5.1)
83
+ actionpack (= 4.2.5.1)
84
+ actionview (= 4.2.5.1)
85
+ activejob (= 4.2.5.1)
86
+ activemodel (= 4.2.5.1)
87
+ activerecord (= 4.2.5.1)
88
+ activesupport (= 4.2.5.1)
89
+ bundler (>= 1.3.0, < 2.0)
90
+ railties (= 4.2.5.1)
91
+ sprockets-rails
92
+ rails-deprecated_sanitizer (1.0.3)
93
+ activesupport (>= 4.2.0.alpha)
94
+ rails-dom-testing (1.0.7)
95
+ activesupport (>= 4.2.0.beta, < 5.0)
96
+ nokogiri (~> 1.6.0)
97
+ rails-deprecated_sanitizer (>= 1.0.1)
98
+ rails-html-sanitizer (1.0.3)
99
+ loofah (~> 2.0)
100
+ railties (4.2.5.1)
101
+ actionpack (= 4.2.5.1)
102
+ activesupport (= 4.2.5.1)
103
+ rake (>= 0.8.7)
104
+ thor (>= 0.18.1, < 2.0)
105
+ rake (10.5.0)
106
+ rspec (3.5.0)
107
+ rspec-core (~> 3.5.0)
108
+ rspec-expectations (~> 3.5.0)
109
+ rspec-mocks (~> 3.5.0)
110
+ rspec-core (3.5.4)
111
+ rspec-support (~> 3.5.0)
112
+ rspec-expectations (3.5.0)
113
+ diff-lcs (>= 1.2.0, < 2.0)
114
+ rspec-support (~> 3.5.0)
115
+ rspec-mocks (3.5.0)
116
+ diff-lcs (>= 1.2.0, < 2.0)
117
+ rspec-support (~> 3.5.0)
118
+ rspec-support (3.5.0)
119
+ simplecov (0.12.0)
120
+ docile (~> 1.1.0)
121
+ json (>= 1.8, < 3)
122
+ simplecov-html (~> 0.10.0)
123
+ simplecov-html (0.10.0)
124
+ sprockets (3.7.0)
125
+ concurrent-ruby (~> 1.0)
126
+ rack (> 1, < 3)
127
+ sprockets-rails (3.1.1)
128
+ actionpack (>= 4.0)
129
+ activesupport (>= 4.0)
130
+ sprockets (>= 3.0.0)
131
+ thor (0.19.1)
132
+ thread_safe (0.3.5)
133
+ tzinfo (1.2.2)
134
+ thread_safe (~> 0.1)
135
+
136
+ PLATFORMS
137
+ ruby
138
+
139
+ DEPENDENCIES
140
+ activerecord_globalize!
141
+ appraisal
142
+ bundler (~> 1.8)
143
+ byebug
144
+ codeclimate-test-reporter
145
+ database_cleaner
146
+ pg
147
+ rails (~> 4.2.0)
148
+ rake (~> 10.0)
149
+ rspec
150
+ simplecov
151
+
152
+ BUNDLED WITH
153
+ 1.12.5
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "5.0"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,157 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ activerecord_globalize (0.1.0)
5
+ activerecord (>= 4.0.0)
6
+ railties (>= 4.0.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actioncable (5.0.0)
12
+ actionpack (= 5.0.0)
13
+ nio4r (~> 1.2)
14
+ websocket-driver (~> 0.6.1)
15
+ actionmailer (5.0.0)
16
+ actionpack (= 5.0.0)
17
+ actionview (= 5.0.0)
18
+ activejob (= 5.0.0)
19
+ mail (~> 2.5, >= 2.5.4)
20
+ rails-dom-testing (~> 2.0)
21
+ actionpack (5.0.0)
22
+ actionview (= 5.0.0)
23
+ activesupport (= 5.0.0)
24
+ rack (~> 2.0)
25
+ rack-test (~> 0.6.3)
26
+ rails-dom-testing (~> 2.0)
27
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
28
+ actionview (5.0.0)
29
+ activesupport (= 5.0.0)
30
+ builder (~> 3.1)
31
+ erubis (~> 2.7.0)
32
+ rails-dom-testing (~> 2.0)
33
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
34
+ activejob (5.0.0)
35
+ activesupport (= 5.0.0)
36
+ globalid (>= 0.3.6)
37
+ activemodel (5.0.0)
38
+ activesupport (= 5.0.0)
39
+ activerecord (5.0.0)
40
+ activemodel (= 5.0.0)
41
+ activesupport (= 5.0.0)
42
+ arel (~> 7.0)
43
+ activesupport (5.0.0)
44
+ concurrent-ruby (~> 1.0, >= 1.0.2)
45
+ i18n (~> 0.7)
46
+ minitest (~> 5.1)
47
+ tzinfo (~> 1.1)
48
+ appraisal (2.1.0)
49
+ bundler
50
+ rake
51
+ thor (>= 0.14.0)
52
+ arel (7.1.4)
53
+ builder (3.2.2)
54
+ byebug (9.0.6)
55
+ codeclimate-test-reporter (1.0.3)
56
+ simplecov
57
+ concurrent-ruby (1.0.2)
58
+ database_cleaner (1.5.3)
59
+ diff-lcs (1.2.5)
60
+ docile (1.1.5)
61
+ erubis (2.7.0)
62
+ globalid (0.3.7)
63
+ activesupport (>= 4.1.0)
64
+ i18n (0.7.0)
65
+ json (1.8.3)
66
+ loofah (2.0.3)
67
+ nokogiri (>= 1.5.9)
68
+ mail (2.6.4)
69
+ mime-types (>= 1.16, < 4)
70
+ method_source (0.8.2)
71
+ mime-types (3.1)
72
+ mime-types-data (~> 3.2015)
73
+ mime-types-data (3.2016.0521)
74
+ mini_portile2 (2.1.0)
75
+ minitest (5.9.1)
76
+ nio4r (1.2.1)
77
+ nokogiri (1.6.8.1)
78
+ mini_portile2 (~> 2.1.0)
79
+ pg (0.18.4)
80
+ rack (2.0.1)
81
+ rack-test (0.6.3)
82
+ rack (>= 1.0)
83
+ rails (5.0.0)
84
+ actioncable (= 5.0.0)
85
+ actionmailer (= 5.0.0)
86
+ actionpack (= 5.0.0)
87
+ actionview (= 5.0.0)
88
+ activejob (= 5.0.0)
89
+ activemodel (= 5.0.0)
90
+ activerecord (= 5.0.0)
91
+ activesupport (= 5.0.0)
92
+ bundler (>= 1.3.0, < 2.0)
93
+ railties (= 5.0.0)
94
+ sprockets-rails (>= 2.0.0)
95
+ rails-dom-testing (2.0.1)
96
+ activesupport (>= 4.2.0, < 6.0)
97
+ nokogiri (~> 1.6.0)
98
+ rails-html-sanitizer (1.0.3)
99
+ loofah (~> 2.0)
100
+ railties (5.0.0)
101
+ actionpack (= 5.0.0)
102
+ activesupport (= 5.0.0)
103
+ method_source
104
+ rake (>= 0.8.7)
105
+ thor (>= 0.18.1, < 2.0)
106
+ rake (10.5.0)
107
+ rspec (3.5.0)
108
+ rspec-core (~> 3.5.0)
109
+ rspec-expectations (~> 3.5.0)
110
+ rspec-mocks (~> 3.5.0)
111
+ rspec-core (3.5.4)
112
+ rspec-support (~> 3.5.0)
113
+ rspec-expectations (3.5.0)
114
+ diff-lcs (>= 1.2.0, < 2.0)
115
+ rspec-support (~> 3.5.0)
116
+ rspec-mocks (3.5.0)
117
+ diff-lcs (>= 1.2.0, < 2.0)
118
+ rspec-support (~> 3.5.0)
119
+ rspec-support (3.5.0)
120
+ simplecov (0.12.0)
121
+ docile (~> 1.1.0)
122
+ json (>= 1.8, < 3)
123
+ simplecov-html (~> 0.10.0)
124
+ simplecov-html (0.10.0)
125
+ sprockets (3.7.0)
126
+ concurrent-ruby (~> 1.0)
127
+ rack (> 1, < 3)
128
+ sprockets-rails (3.2.0)
129
+ actionpack (>= 4.0)
130
+ activesupport (>= 4.0)
131
+ sprockets (>= 3.0.0)
132
+ thor (0.19.1)
133
+ thread_safe (0.3.5)
134
+ tzinfo (1.2.2)
135
+ thread_safe (~> 0.1)
136
+ websocket-driver (0.6.4)
137
+ websocket-extensions (>= 0.1.0)
138
+ websocket-extensions (0.1.2)
139
+
140
+ PLATFORMS
141
+ ruby
142
+
143
+ DEPENDENCIES
144
+ activerecord_globalize!
145
+ appraisal
146
+ bundler (~> 1.8)
147
+ byebug
148
+ codeclimate-test-reporter
149
+ database_cleaner
150
+ pg
151
+ rails (= 5.0)
152
+ rake (~> 10.0)
153
+ rspec
154
+ simplecov
155
+
156
+ BUNDLED WITH
157
+ 1.12.5
@@ -0,0 +1,11 @@
1
+ require 'active_record'
2
+ require 'active_record/connection_adapters/postgresql_adapter'
3
+ require 'activerecord_globalize/version'
4
+ require 'activerecord_globalize/hstore_checker'
5
+ require 'activerecord_globalize/translates'
6
+ require 'activerecord-postgres-hstore' unless ActiverecordGlobalize::HstoreChecker::native_hstore?
7
+
8
+ ##
9
+ # Just the definition of Activerecord Globalize main module
10
+ module ActiverecordGlobalize
11
+ end
@@ -0,0 +1,9 @@
1
+ module ActiverecordGlobalize
2
+ module HstoreChecker # :nodoc:
3
+ ##
4
+ # Check if database is supporting hstore data type by default
5
+ def self.native_hstore?
6
+ @native_hstore ||= ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES.key?(:hstore)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,91 @@
1
+ module ActiverecordGlobalize
2
+ module Translates # :nodoc:
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ # holds all translated fields
6
+ base.class_attribute :translated_attrs
7
+ base.send :include, InstanceMethods
8
+ end
9
+
10
+ module InstanceMethods
11
+ ##
12
+ # set value to a specific/current locale and fire ActiveRecord attribute_will_change!
13
+ # if the current locale equals the default locale update value of original field
14
+ #
15
+ # +attr_name+ field name
16
+ # +value+ locale value
17
+ # +locale+ desired locale
18
+ def write_translation(attr_name, value, locale = I18n.locale)
19
+ translation_store = translated_attr_name(attr_name)
20
+ translations = send(translation_store) || {}
21
+ send("#{translation_store}_will_change!") unless translations[locale.to_s] == value
22
+ translations[locale.to_s] = value
23
+ send("#{translation_store}=", translations)
24
+ self[attr_name] = value if I18n.default_locale == I18n.locale
25
+ value
26
+ end
27
+
28
+ ##
29
+ # get translations hstore of the field then
30
+ # if the current locale in translations return it's value
31
+ # if not return the default locale's value
32
+ # if current/default locale not found return actual field value
33
+ #
34
+ # +attr_name+ field name
35
+ # +locale+ desired locale
36
+ def read_translation(attr_name, locale = I18n.locale)
37
+ translations = send(translated_attr_name(attr_name)) || {}
38
+ return translations[locale] if translations.key?(locale)
39
+ return translations[I18n.default_locale] if translations.key?(I18n.default_locale)
40
+ self[attr_name]
41
+ end
42
+
43
+ # return translated field name of a field
44
+ def translated_attr_name(attr_name)
45
+ "#{attr_name}_translations"
46
+ end
47
+
48
+ # Overrides to_json to exclude translated_attrs
49
+ def as_json(*)
50
+ attributes.delete_if { |k, _v| translated_attrs.map { |f| translated_attr_name(f) }.include?(k) }
51
+ end
52
+
53
+ # activerecord update attributes in specific locale
54
+ def update_with_locale!(locale, *attrs)
55
+ I18n.with_locale locale do
56
+ update!(*attrs)
57
+ end
58
+ end
59
+ end
60
+
61
+ # Patches ActiveRecord models
62
+ module ClassMethods
63
+ # Overrides ActiveRecord getters and setters to consider I18n locales
64
+ #
65
+ # +attrs+ translated fields names
66
+ def translates(*attrs)
67
+ raise(ArgumentError, 'must have at least one record to localize') if attrs.empty?
68
+
69
+ self.translated_attrs = attrs
70
+ attrs.each do |attr_name|
71
+ define_method "#{attr_name}=" do |value|
72
+ write_translation(attr_name, value)
73
+ end
74
+
75
+ define_method attr_name do
76
+ read_translation(attr_name)
77
+ end
78
+
79
+ define_method "#{attr_name}_translations" do
80
+ self["#{attr_name}_translations"].try(:with_indifferent_access)
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ # include Translates on ActiveRecord models
88
+ ActiveSupport.on_load(:active_record) do
89
+ ActiveRecord::Base.send(:include, Translates)
90
+ end
91
+ end
@@ -0,0 +1,5 @@
1
+ module ActiverecordGlobalize
2
+ ##
3
+ # ActiverecordGlobalize gem version
4
+ VERSION = '1.0.0'.freeze
5
+ end