utranslate 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 21d1d8968c67f1287d0b77a3830d54d7756cbec2491d43b9e31aa5c5c6ae02fa
4
+ data.tar.gz: 6466bd35ff8908cb00a02658bfa6178dfc8410961565f478777598d6f0e91f8a
5
+ SHA512:
6
+ metadata.gz: ede1c320a0a3bfeb0708c1785f98d6401c979c4bbb9c160b3c058f61df8b6b3d574ec4a703a7e6be52e45cd36f1afaafdad0e2e924606c68a597bb3be7ea3240
7
+ data.tar.gz: de7b2fe6406de26f562857c57060ac0b4123758b0f4dbfe0771801e1a662b032b395726157a7fc804d29a0b0d7566a4adbe6ce6fa4fb03e3e013bf393c914c80
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2020 jcollazos
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,95 @@
1
+ # Utranslate
2
+ A very easy to use and configure gem to add I18n support to Activerecord models using postgresql JSONB columns.
3
+
4
+ # Usage
5
+
6
+ ## Installation
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'utranslate'
11
+ ```
12
+
13
+ And then execute:
14
+ ```bash
15
+ $ bundle
16
+ ```
17
+
18
+ Or install it yourself as:
19
+ ```bash
20
+ $ gem install utranslate
21
+ ```
22
+
23
+ ## Configuration
24
+
25
+ Include utranslate in all the models that you want to translate:
26
+
27
+ ```ruby
28
+ class Post
29
+ include Utranslate
30
+ end
31
+ ```
32
+
33
+ Add translations for every attribute that you want to translate.
34
+
35
+ ```ruby
36
+ translate :title
37
+ ```
38
+
39
+ You can specify to check for presence of all I18n.available_locales
40
+
41
+ ```ruby
42
+ translate :title, null: false
43
+ ```
44
+
45
+ Or you can define what locales you want to check
46
+
47
+ ```ruby
48
+ translate :title, null: false, locales: [:es, :en]
49
+ ```
50
+
51
+ ## Adding a translation for current locale
52
+
53
+ To add a translation for current locale you just have to assign as usual
54
+
55
+ ```ruby
56
+ post.title = 'translation'
57
+ ```
58
+
59
+ ## Adding a translation for a given locale
60
+
61
+ To add a translation for a given locale you must pass the locale and the translation
62
+
63
+ ```ruby
64
+ post.title(:es, 'traducción')
65
+ ```
66
+
67
+ ## Mass assigning translations
68
+
69
+ To mass assign translations you can do it as usual
70
+
71
+ ```ruby
72
+ post = Post.new(title: { es: 'traducción', en: 'translation'})
73
+ ```
74
+
75
+ ## Retriving a translation for current locale
76
+
77
+ To retrive a translation for current locale you can do it as usual
78
+
79
+ ```ruby
80
+ post.title
81
+ ```
82
+
83
+ ## Retriving a translation for given locale
84
+
85
+ To retrive a translation for current locale you can do it as usual
86
+
87
+ ```ruby
88
+ post.title(:es)
89
+ ```
90
+
91
+ ## Contributing
92
+ Feel free to contribute to this project or to contact me through klatooine@gmail.com
93
+
94
+ ## License
95
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ require 'rdoc/task'
10
+
11
+ RDoc::Task.new(:rdoc) do |rdoc|
12
+ rdoc.rdoc_dir = 'rdoc'
13
+ rdoc.title = 'Utranslate'
14
+ rdoc.options << '--line-numbers'
15
+ rdoc.rdoc_files.include('README.md')
16
+ rdoc.rdoc_files.include('lib/**/*.rb')
17
+ end
18
+
19
+ require 'bundler/gem_tasks'
20
+
21
+ require 'rake/testtask'
22
+
23
+ Rake::TestTask.new(:test) do |t|
24
+ t.libs << 'test'
25
+ t.pattern = 'test/**/*_test.rb'
26
+ t.verbose = false
27
+ end
28
+
29
+ task default: :test
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ # desc "Explaining what the task does"
3
+ # task :utranslate do
4
+ # # Task goes here
5
+ # end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Utranslate
4
+ class ColumnNotPresentError < StandardError
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Utranslate
4
+ class InvalidFormatError < StandardError
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Utranslate
4
+ class Railtie < ::Rails::Railtie
5
+ end
6
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Utranslate
4
+ class AllTranslationsValidator < ActiveModel::EachValidator
5
+ def validate_each(record, attribute, _)
6
+ locales = options.fetch(:locales)
7
+
8
+ return unless missing_translations?(locales)
9
+
10
+ record.errors[attribute] << "#{missing_translations} translations not present for #{attribute}"
11
+ end
12
+
13
+ private
14
+
15
+ def missing_translations?(locales)
16
+ locales.each do |locale|
17
+ missing_translations << locale unless locale_present?
18
+ end
19
+
20
+ !missing_translations.length.zero?
21
+ end
22
+
23
+ def missing_translations
24
+ @_missing_translations ||= []
25
+ end
26
+
27
+ def locale_present?
28
+ false
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Utranslate
4
+ class HashValidator < ActiveModel::EachValidator
5
+ def validate_each(record, attribute, value)
6
+ return if value.is_a?(Hash)
7
+
8
+ record.errors[attribute] << 'Incorrect format for translated column, should be a hash.'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Utranslate
4
+ VERSION = '0.1.1'
5
+ end
data/lib/utranslate.rb ADDED
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'utranslate/railtie'
4
+ require 'utranslate/errors/column_not_present_error'
5
+ require 'utranslate/errors/invalid_format_error'
6
+ require 'utranslate/validators/all_translations_validator'
7
+ require 'utranslate/validators/hash_validator'
8
+
9
+ module Utranslate
10
+ extend ActiveSupport::Concern
11
+
12
+ class_methods do
13
+ def translate(attr_name, null: true, locales: I18n.available_locales)
14
+ raise Utranslate::ColumnNotPresentError, "ERROR: #{attr_name} not present in #{name}." unless column_names.include?(attr_name.to_s)
15
+ raise Utranslate::InvalidFormatError, "ERROR: #{attr_name} not a valid JSONB column." unless type_for_attribute(attr_name).type == :jsonb
16
+
17
+ validates :"#{attr_name}",
18
+ "Utranslate::Hash": true
19
+ validates :"#{attr_name}",
20
+ "Utranslate::AllTranslations": { locales: locales },
21
+ unless: -> { null }
22
+
23
+ define_method "utranslate_#{attr_name}" do |locale, value|
24
+ self[:"#{attr_name}"][locale.to_s] = value
25
+ end
26
+
27
+ define_method attr_name.to_s do |locale = I18n.locale, value = nil|
28
+ if value.present?
29
+ send(:"utranslate_#{attr_name}", locale, value)
30
+ else
31
+ self[:"#{attr_name}"][locale.to_s]
32
+ end
33
+ end
34
+
35
+ define_method "#{attr_name}=" do |value|
36
+ if value.is_a?(Hash)
37
+ self[:"#{attr_name}"] = value
38
+ else
39
+ send(:"utranslate_#{attr_name}", I18n.locale, value)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: utranslate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - jcollazos
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-05-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 6.0.2
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 6.0.2.2
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 6.0.2
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 6.0.2.2
33
+ - !ruby/object:Gem::Dependency
34
+ name: overcommit
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: pg
49
+ requirement: !ruby/object:Gem::Requirement
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
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rubocop
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ description: A simple model translation gem using postgresql JSONB columns
76
+ email:
77
+ - julianjaviercollazos@gmail.com
78
+ executables: []
79
+ extensions: []
80
+ extra_rdoc_files: []
81
+ files:
82
+ - MIT-LICENSE
83
+ - README.md
84
+ - Rakefile
85
+ - lib/tasks/utranslate_tasks.rake
86
+ - lib/utranslate.rb
87
+ - lib/utranslate/errors/column_not_present_error.rb
88
+ - lib/utranslate/errors/invalid_format_error.rb
89
+ - lib/utranslate/railtie.rb
90
+ - lib/utranslate/validators/all_translations_validator.rb
91
+ - lib/utranslate/validators/hash_validator.rb
92
+ - lib/utranslate/version.rb
93
+ homepage: https://github.com/klatooine/utranslate
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubygems_version: 3.1.2
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: A simple model translation gem.
116
+ test_files: []