tolken 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ env:
6
+ global:
7
+ - CC_TEST_REPORTER_ID=38cf6fe42113efd6306798c2189499f6ff9da87a92dd548e8339a1bd98d2a7e6
8
+ addons:
9
+ postgresql: "9.6"
10
+ before_install: gem install bundler -v 1.16.1
11
+ before_script:
12
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
13
+ - chmod +x ./cc-test-reporter
14
+ - ./cc-test-reporter before-build
15
+ - psql -c 'create database travis_ci_test;' -U postgres
16
+ script:
17
+ - bundle exec rspec
18
+ after_script:
19
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in tolken.gemspec
8
+ gemspec
@@ -0,0 +1,181 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ tolken (0.1.0)
5
+ rails (= 5.2.0.rc2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actioncable (5.2.0.rc2)
11
+ actionpack (= 5.2.0.rc2)
12
+ nio4r (~> 2.0)
13
+ websocket-driver (>= 0.6.1)
14
+ actionmailer (5.2.0.rc2)
15
+ actionpack (= 5.2.0.rc2)
16
+ actionview (= 5.2.0.rc2)
17
+ activejob (= 5.2.0.rc2)
18
+ mail (~> 2.5, >= 2.5.4)
19
+ rails-dom-testing (~> 2.0)
20
+ actionpack (5.2.0.rc2)
21
+ actionview (= 5.2.0.rc2)
22
+ activesupport (= 5.2.0.rc2)
23
+ rack (~> 2.0)
24
+ rack-test (>= 0.6.3)
25
+ rails-dom-testing (~> 2.0)
26
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
27
+ actionview (5.2.0.rc2)
28
+ activesupport (= 5.2.0.rc2)
29
+ builder (~> 3.1)
30
+ erubi (~> 1.4)
31
+ rails-dom-testing (~> 2.0)
32
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
33
+ activejob (5.2.0.rc2)
34
+ activesupport (= 5.2.0.rc2)
35
+ globalid (>= 0.3.6)
36
+ activemodel (5.2.0.rc2)
37
+ activesupport (= 5.2.0.rc2)
38
+ activerecord (5.2.0.rc2)
39
+ activemodel (= 5.2.0.rc2)
40
+ activesupport (= 5.2.0.rc2)
41
+ arel (>= 9.0)
42
+ activestorage (5.2.0.rc2)
43
+ actionpack (= 5.2.0.rc2)
44
+ activerecord (= 5.2.0.rc2)
45
+ marcel (~> 0.3.1)
46
+ activesupport (5.2.0.rc2)
47
+ concurrent-ruby (~> 1.0, >= 1.0.2)
48
+ i18n (>= 0.7, < 2)
49
+ minitest (~> 5.1)
50
+ tzinfo (~> 1.1)
51
+ arel (9.0.0)
52
+ ast (2.4.0)
53
+ builder (3.2.3)
54
+ coderay (1.1.2)
55
+ concurrent-ruby (1.0.5)
56
+ crass (1.0.4)
57
+ diff-lcs (1.3)
58
+ docile (1.3.1)
59
+ erubi (1.7.1)
60
+ globalid (0.4.1)
61
+ activesupport (>= 4.2.0)
62
+ i18n (1.1.0)
63
+ concurrent-ruby (~> 1.0)
64
+ json (2.1.0)
65
+ loofah (2.2.2)
66
+ crass (~> 1.0.2)
67
+ nokogiri (>= 1.5.9)
68
+ mail (2.7.0)
69
+ mini_mime (>= 0.1.1)
70
+ marcel (0.3.2)
71
+ mimemagic (~> 0.3.2)
72
+ method_source (0.9.0)
73
+ mimemagic (0.3.2)
74
+ mini_mime (1.0.0)
75
+ mini_portile2 (2.3.0)
76
+ minitest (5.11.3)
77
+ nio4r (2.3.1)
78
+ nokogiri (1.8.4)
79
+ mini_portile2 (~> 2.3.0)
80
+ parallel (1.12.1)
81
+ parser (2.5.1.2)
82
+ ast (~> 2.4.0)
83
+ pg (1.0.0)
84
+ powerpack (0.1.2)
85
+ pry (0.11.3)
86
+ coderay (~> 1.1.0)
87
+ method_source (~> 0.9.0)
88
+ rack (2.0.5)
89
+ rack-test (1.1.0)
90
+ rack (>= 1.0, < 3)
91
+ rails (5.2.0.rc2)
92
+ actioncable (= 5.2.0.rc2)
93
+ actionmailer (= 5.2.0.rc2)
94
+ actionpack (= 5.2.0.rc2)
95
+ actionview (= 5.2.0.rc2)
96
+ activejob (= 5.2.0.rc2)
97
+ activemodel (= 5.2.0.rc2)
98
+ activerecord (= 5.2.0.rc2)
99
+ activestorage (= 5.2.0.rc2)
100
+ activesupport (= 5.2.0.rc2)
101
+ bundler (>= 1.3.0)
102
+ railties (= 5.2.0.rc2)
103
+ sprockets-rails (>= 2.0.0)
104
+ rails-dom-testing (2.0.3)
105
+ activesupport (>= 4.2.0)
106
+ nokogiri (>= 1.6)
107
+ rails-html-sanitizer (1.0.4)
108
+ loofah (~> 2.2, >= 2.2.2)
109
+ railties (5.2.0.rc2)
110
+ actionpack (= 5.2.0.rc2)
111
+ activesupport (= 5.2.0.rc2)
112
+ method_source
113
+ rake (>= 0.8.7)
114
+ thor (>= 0.18.1, < 2.0)
115
+ rainbow (3.0.0)
116
+ rake (10.5.0)
117
+ rspec (3.8.0)
118
+ rspec-core (~> 3.8.0)
119
+ rspec-expectations (~> 3.8.0)
120
+ rspec-mocks (~> 3.8.0)
121
+ rspec-core (3.8.0)
122
+ rspec-support (~> 3.8.0)
123
+ rspec-expectations (3.8.1)
124
+ diff-lcs (>= 1.2.0, < 2.0)
125
+ rspec-support (~> 3.8.0)
126
+ rspec-mocks (3.8.0)
127
+ diff-lcs (>= 1.2.0, < 2.0)
128
+ rspec-support (~> 3.8.0)
129
+ rspec-support (3.8.0)
130
+ rubocop (0.52.1)
131
+ parallel (~> 1.10)
132
+ parser (>= 2.4.0.2, < 3.0)
133
+ powerpack (~> 0.1)
134
+ rainbow (>= 2.2.2, < 4.0)
135
+ ruby-progressbar (~> 1.7)
136
+ unicode-display_width (~> 1.0, >= 1.0.1)
137
+ rubocop-rspec (1.23.0)
138
+ rubocop (>= 0.52.1)
139
+ ruby-progressbar (1.10.0)
140
+ simple_form (1.4.1)
141
+ simplecov (0.16.1)
142
+ docile (~> 1.1)
143
+ json (>= 1.8, < 3)
144
+ simplecov-html (~> 0.10.0)
145
+ simplecov-html (0.10.2)
146
+ sprockets (3.7.2)
147
+ concurrent-ruby (~> 1.0)
148
+ rack (> 1, < 3)
149
+ sprockets-rails (3.2.1)
150
+ actionpack (>= 4.0)
151
+ activesupport (>= 4.0)
152
+ sprockets (>= 3.0.0)
153
+ thor (0.20.0)
154
+ thread_safe (0.3.6)
155
+ tzinfo (1.2.5)
156
+ thread_safe (~> 0.1)
157
+ unicode-display_width (1.4.0)
158
+ websocket-driver (0.7.0)
159
+ websocket-extensions (>= 0.1.0)
160
+ websocket-extensions (0.1.3)
161
+ with_model (2.0.0)
162
+ activerecord (>= 4.2)
163
+
164
+ PLATFORMS
165
+ ruby
166
+
167
+ DEPENDENCIES
168
+ bundler (~> 1.16)
169
+ pg
170
+ pry
171
+ rake (~> 10.0)
172
+ rspec (~> 3.0)
173
+ rubocop (= 0.52.1)
174
+ rubocop-rspec
175
+ simple_form
176
+ simplecov
177
+ tolken!
178
+ with_model
179
+
180
+ BUNDLED WITH
181
+ 1.16.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Nicklas Ramhöj
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,147 @@
1
+ # Tolken
2
+ Tolken is a Ruby on Rails Gem that allows you to translate database fields using Postgres' jsob data type.
3
+
4
+ Tolken's API is more verbose than most similar gems. The philosophy is that you should be aware of when you're dealing with translatable fields and what language you're interested in in any given moment. This comes from experience working with gems such as [Globalize](https://github.com/globalize/globalize), while it might fit some projects we've found that the magic that starts out as a convenience quickly becomes a liability.
5
+
6
+ In Tolken a translatable field is just a Ruby hash which makes it easy to reason about. See *Usage* for details.
7
+
8
+ [![Build Status](https://travis-ci.org/varvet/tolken.svg?branch=master)](https://travis-ci.org/varvet/tolken)
9
+ [![Maintainability](https://api.codeclimate.com/v1/badges/72c772179a8baa586f7f/maintainability)](https://codeclimate.com/github/varvet/tolken/maintainability)
10
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/72c772179a8baa586f7f/test_coverage)](https://codeclimate.com/github/varvet/tolken/test_coverage)
11
+
12
+ ## Installation
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem "tolken"
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install tolken
26
+
27
+ ## Usage
28
+ Make sure you're running Postgres and that the column you want to translate is of the `jsonb` type:
29
+
30
+ ### Setup
31
+ ```rb
32
+ class CreatePosts < ActiveRecord::Migration[5.2]
33
+ def change
34
+ create_table :posts do |t|
35
+ t.jsonb :title, :jsonb, null: false
36
+ t.timestamps
37
+ end
38
+
39
+ execute "CREATE INDEX posts_title_index ON posts USING gin (title)"
40
+ end
41
+ end
42
+ ```
43
+
44
+ Next you need to configure `I18n` if you haven't already (usually in *config/initializers/locale.rb*):
45
+
46
+ ```rb
47
+ I18n.available_locales = %i[en sv de]
48
+ ```
49
+
50
+ Tolken expects you to only use translations for the locales in `I18n.available_locales`.
51
+
52
+ ### Persistence
53
+ Extend your model with `Tolken` and tell it what column(s) you want to translate:
54
+
55
+ ```rb
56
+ class Post < ApplicationRecord
57
+ extend Tolken
58
+ translates :title
59
+ end
60
+ ```
61
+
62
+ You can now work with `title` as follows:
63
+
64
+ ```rb
65
+ post = Post.create(title: { en: "News", sv: "Nyheter" })
66
+
67
+ post.title # => { en: "News", sv: "Nyheter" }
68
+ post.title(:en) # => "News"
69
+ post.title(:sv) # => "Nyheter"
70
+ post.title(:dk) # ArgumentError, "Invalid locale dk"
71
+ post.title(:de) # => nil
72
+
73
+ post.title = { en: "News", sv: "Nyheter" }
74
+ post.title[:en] = "News"
75
+ ```
76
+
77
+ ### Validation
78
+ Tolken comes with support for the *presence* validator:
79
+
80
+ ```rb
81
+ class Post < ApplicationRecord
82
+ extend Tolken
83
+ translates :title, presence: true
84
+ end
85
+
86
+ post = Post.create(title: { en: "News", sv: "" })
87
+ post.errors.messages # => { name_sv: ["can't be blank"] }
88
+ ```
89
+
90
+ Tolken checks that all `I18n.available_locales` has present values.
91
+
92
+ ### View Forms
93
+ Tolken has opt-in support for integrating with [SimpleForm](https://github.com/plataformatec/simple_form). To opt-in update your Gemfile:
94
+
95
+ ```ruby
96
+ gem "tolken", require: "tolken/simle_form"
97
+ ```
98
+
99
+ Now if you add a simple_form field for a translatable field SimpleForm will generate an input per language version:
100
+
101
+ ```erb
102
+ <%= simple_form_for(@post) do |form| %>
103
+ <%= form.input :title %>
104
+ <%= form.submit %>
105
+ <% end %>
106
+ ```
107
+
108
+ By default a text input field is generated. If you want another type you can override with:
109
+
110
+ ```erb
111
+ <%= form.input :title, type: :text %>
112
+ ```
113
+
114
+ This will instead render one text area per language version.
115
+
116
+ The specs for [translates](spec/tolken/translates_spec.rb) is a good resource of additional usage examples.
117
+
118
+ ## Development
119
+
120
+ ### Native dependencies
121
+ You need to have Postgres installed on your system. This can be done on Mac OS with `brew install postgres`.
122
+
123
+ ### Ruby setup
124
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
125
+
126
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
127
+
128
+ ## Contributing
129
+ Bug reports and pull requests are welcome on GitHub at https://github.com/varvet/tolken. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. To create a pull request follow these steps:
130
+
131
+ $ git clone git@github.com:varvet/tolken.git
132
+ $ cd tolken
133
+ $ git checkout my-feature-branch
134
+ Make changes and commits, consider squashing commits that doesn't add anything by themselves
135
+ $ rubocop
136
+ $ rspec
137
+ $ open coverage/index.html
138
+ If test coverage is missing address it or add a description to your PR why it is lacking.
139
+ Commit, if possible squash in original locations, changes required by rubocop and or failing tests.
140
+ $ git checkout master
141
+ $ git pull --rebase
142
+ $ git checkout my-feature-branch
143
+ $ git rebase master
144
+ $ git push
145
+
146
+ ## License
147
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "tolken"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+ createdb tolken_test
8
+
9
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "i18n"
4
+
5
+ require "tolken/version"
6
+ require "tolken/hash_serializer"
7
+ require "tolken/translates"
8
+
9
+ module Tolken
10
+ include Tolken::Translates
11
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tolken
4
+ class HashSerializer
5
+ class << self
6
+ def dump(hash)
7
+ hash # ActiveRecord handles formatting this from a Ruby hash to psql jsonb
8
+ end
9
+
10
+ def load(hash)
11
+ hash = JSON.parse(hash) if hash.is_a?(String)
12
+ (hash || {}).with_indifferent_access
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails"
4
+ require "simple_form"
5
+ require_relative "../tolken"
6
+
7
+ module Tolken
8
+ module SimpleForm
9
+ class JsonbInput < ::SimpleForm::Inputs::StringInput
10
+ include ActionView::Helpers::OutputSafetyHelper
11
+
12
+ def input(_wrapper_options = {})
13
+ safe_join(
14
+ I18n.available_locales.map do |locale|
15
+ options = {
16
+ input_html: {
17
+ name: "#{object.class.to_s.downcase}[#{attribute_name}][#{locale}]",
18
+ value: object.public_send(attribute_name)[locale]
19
+ },
20
+ as: input_options[:type].presence || :string
21
+ }
22
+
23
+ @builder.input(:"#{attribute_name}_#{locale}", options)
24
+ end
25
+ )
26
+ end
27
+
28
+ def label(_wrapper_options = {})
29
+ # label is rendered per translatable field
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ JsonbInput = Tolken::SimpleForm::JsonbInput