essay-globalize 1.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: fe9c42ef2e78fe78928acc97f347965fd6ba025b
4
+ data.tar.gz: 73d708616a1a4fe058183ef634a65db3500dac89
5
+ SHA512:
6
+ metadata.gz: c27d27b0dd7d9c438fc7e03b6a4f78f1e814bdec7d492d59c56bf72c715a002cfc6946447de90644fecdb6e109208ef6894b9fc7310b54b544951a88e3d5b27f
7
+ data.tar.gz: 0431f46916b9a27aefaf5c9c139f1f14a471a7a6933f0e9f978e6dabd8789e1f7c686ed89484294824ba72ee39f1f145c9387051095fd14fcb8808c3d3dc1874
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ source 'https://rubygems.org'
5
+
6
+ # Specify your gem's dependencies in confo.gemspec
7
+ gemspec
8
+
9
+ gem 'essay', path: '~/Development/essay'
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Yaroslav Konoplov
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,22 @@
1
+ ```ruby
2
+ class Article < ActiveRecord::Base
3
+ belongs_to :poster
4
+ translates :name, :poster_id
5
+ end
6
+
7
+ Article.features.translates_with_globalize? # => true
8
+
9
+ Article.features.globalize.translated_attribute_names # => [:name, :poster_id]
10
+ Article.features.globalize.model_class_for_translations # => Article::Translation
11
+ Article.features.globalize.association_for_translations.name # => :translations
12
+
13
+ Article.attribute_features[:name].translates_with_globalize? # => true
14
+ Article.association_features[:poster].translates_with_globalize? # => true
15
+ Article.association_features[:translations].translates_with_globalize? # => false
16
+ Article.association_features[:translations].translation_for_globalize? # => true
17
+ ```
18
+
19
+ ## Gemfile
20
+ ```ruby
21
+ gem 'essay-globalize', '~> 1.0'
22
+ ```
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'essay-globalize'
6
+ s.version = '1.0.1'
7
+ s.authors = ['Yaroslav Konoplov']
8
+ s.email = ['eahome00@gmail.com']
9
+ s.summary = 'Essay writer for globalize'
10
+ s.description = 'Essay writer for globalize'
11
+ s.homepage = 'http://github.com/yivo/essay-globalize'
12
+ s.license = 'MIT'
13
+
14
+ s.executables = `git ls-files -z -- bin/*`.split("\x0").map{ |f| File.basename(f) }
15
+ s.files = `git ls-files -z`.split("\x0")
16
+ s.test_files = `git ls-files -z -- {test,spec,features}/*`.split("\x0")
17
+ s.require_paths = ['lib']
18
+
19
+ s.add_dependency 'globalize', '~> 0'
20
+ s.add_dependency 'essay', '~> 1.0'
21
+ end
@@ -0,0 +1,9 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ require 'globalize'
5
+ require 'essay'
6
+
7
+ require 'essay-globalize/model'
8
+ require 'essay-globalize/attribute'
9
+ require 'essay-globalize/association'
@@ -0,0 +1,118 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ class Essay::AssociationFeatures
5
+
6
+ def translation?
7
+ translation_for_globalize?
8
+ end
9
+
10
+ # class Article < ActiveRecord::Base
11
+ # belongs_to :poster
12
+ # translates :poster_id
13
+ # end
14
+ #
15
+ # Article.association_features[:poster].translation_for_globalize? => false
16
+ # Article.association_features[:translations].translation_for_globalize? => true
17
+ def translation_for_globalize?
18
+ !!model_features.with(:globalize) { |g| g.association_for_translations == this_association }
19
+ end
20
+
21
+ def globalize_translation
22
+ @globalize_translation || begin
23
+ @globalize_translation = GlobalizeTranslation.new(env) if translation_for_globalize?
24
+ end
25
+ end
26
+
27
+ class GlobalizeTranslation < Base
28
+ def top_feature
29
+ model_features.globalize
30
+ end
31
+
32
+ protected :top_feature
33
+ end
34
+
35
+ def translates?
36
+ translates_with_globalize?
37
+ end
38
+
39
+ # class Article < ActiveRecord::Base
40
+ # belongs_to :poster
41
+ # translates :poster_id
42
+ # end
43
+ #
44
+ # Article.association_features[:poster].translates_with_globalize? => true
45
+ # Article.association_features[:translations].translates_with_globalize? => false
46
+ def translates_with_globalize?
47
+ !!model_features.with(:globalize) { |g| g.translated_association_names.include?(association_name) }
48
+ end
49
+
50
+ def globalize_translatable
51
+ @globalize_translatable || begin
52
+ @globalize_translatable = GlobalizeTranslatable.new(env) if translates_with_globalize?
53
+ end
54
+ end
55
+
56
+ class GlobalizeTranslatable < Base
57
+ def top_feature
58
+ model_features.globalize
59
+ end
60
+
61
+ # class Article < ActiveRecord::Base
62
+ # belongs_to :poster
63
+ # translates :poster_id
64
+ # end
65
+ #
66
+ # Article.association_features[:poster].translation_table => arel table for 'article_translations'
67
+ def translation_table
68
+ top_feature.association_for_translations.to.arel
69
+ end
70
+
71
+ # class Article < ActiveRecord::Base
72
+ # belongs_to :poster
73
+ # translates :poster_id
74
+ # end
75
+ #
76
+ # Article.association_features[:poster].translation_table_name => 'article_translations'
77
+ def translation_table_name
78
+ translation_table.name.to_sym
79
+ end
80
+
81
+ # class Article < ActiveRecord::Base
82
+ # belongs_to :poster
83
+ # translates :poster_id
84
+ # end
85
+ #
86
+ # Article.association_features[:poster].translation_from_key_name => :poster_id
87
+ def translation_from_key_name
88
+ this_association.reflection.foreign_key.to_sym
89
+ end
90
+
91
+ # class Article < ActiveRecord::Base
92
+ # belongs_to :poster
93
+ # translates :poster_id
94
+ # end
95
+ #
96
+ # Article.association_features[:poster].translation_to_key_name => :id
97
+ def translation_to_key_name
98
+ top_feature.association_for_translations.from_key_name
99
+ end
100
+
101
+ serialize do
102
+ {
103
+ translation_table_name: translation_table_name,
104
+ translation_from_key_name: translation_from_key_name,
105
+ translation_to_key_name: translation_to_key_name
106
+ }
107
+ end
108
+ end
109
+
110
+ serialize do
111
+ {
112
+ is_translation_for_globalize: translation_for_globalize?,
113
+ translates_with_globalize: translates_with_globalize?,
114
+ globalize_translation: globalize_translation.try(:to_hash),
115
+ globalize_translatable: globalize_translatable.try(:to_hash)
116
+ }
117
+ end
118
+ end
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ class Essay::AttributeFeatures
5
+ def translates?
6
+ translates_with_globalize?
7
+ end
8
+
9
+ def translates_with_globalize?
10
+ !!model_features.with(:globalize) { |g| g.translated_attribute_names.include?(attribute_name) }
11
+ end
12
+
13
+ serialize do
14
+ { translates_with_globalize: translates_with_globalize? }
15
+ end
16
+ end
@@ -0,0 +1,82 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ class Essay::ModelFeatures
5
+ def translates?
6
+ translates_with_globalize?
7
+ end
8
+
9
+ def translates_with_globalize?
10
+ model_class.included_modules.include?(Globalize::ActiveRecord::InstanceMethods)
11
+ end
12
+
13
+ def globalize
14
+ @globalize || begin
15
+ @globalize = TranslatesWithGlobalize.new(env) if translates_with_globalize?
16
+ end
17
+ end
18
+
19
+ serialize do
20
+ {
21
+ translates_with_globalize: translates_with_globalize?,
22
+ globalize: globalize.try(:to_hash)
23
+ }
24
+ end
25
+
26
+ class TranslatesWithGlobalize < Base
27
+ def translated_attributes
28
+ model_class.translated_attribute_names.map { |el| model_associations[el] }
29
+ end
30
+
31
+ # class Article < ActiveRecord::Base
32
+ # belongs_to :poster
33
+ # translates :name, :poster_id
34
+ # end
35
+ #
36
+ # Article.features.globalize.translated_attribute_names => [:name, :poster_id]
37
+ def translated_attribute_names
38
+ model_class.translated_attribute_names.map { |el| el.kind_of?(Symbol) ? el : el.to_sym }
39
+ end
40
+
41
+ def translated_associations
42
+ tr_attrs = translated_attribute_names
43
+ model_associations.select do |assoc|
44
+ assoc.belongs_to? && tr_attrs.include?(assoc.reflection.foreign_key.to_sym)
45
+ end
46
+ end
47
+
48
+ # class Article < ActiveRecord::Base
49
+ # belongs_to :poster
50
+ # translates :name, :poster_id
51
+ # end
52
+ #
53
+ # Article.features.globalize.translated_association_names => [:poster]
54
+ def translated_association_names
55
+ translated_associations.map(&:name)
56
+ end
57
+
58
+ # class Article < ActiveRecord::Base
59
+ # belongs_to :poster
60
+ # translates :name, :poster_id
61
+ # end
62
+ #
63
+ # Article.features.globalize.model_class_for_translations => Article::Translation
64
+ def model_class_for_translations
65
+ model_class.translation_class
66
+ end
67
+
68
+ # class Article < ActiveRecord::Base
69
+ # belongs_to :poster
70
+ # translates :name, :poster_id
71
+ # end
72
+ #
73
+ # Article.features.globalize.association_for_translations => traits for association named 'translations'
74
+ def association_for_translations
75
+ globalize_base = Globalize::ActiveRecord::Translation
76
+ model_associations.find do |assoc|
77
+ to_class = assoc.to_class
78
+ to_class && to_class.ancestors.include?(globalize_base)
79
+ end
80
+ end
81
+ end
82
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: essay-globalize
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yaroslav Konoplov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: globalize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: essay
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ description: Essay writer for globalize
42
+ email:
43
+ - eahome00@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - essay-globalize.gemspec
53
+ - lib/essay-globalize.rb
54
+ - lib/essay-globalize/association.rb
55
+ - lib/essay-globalize/attribute.rb
56
+ - lib/essay-globalize/model.rb
57
+ homepage: http://github.com/yivo/essay-globalize
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.5.1
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Essay writer for globalize
81
+ test_files: []