booletania 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +10 -1
- data/README.md +18 -7
- data/config/database.travis.yml +4 -0
- data/lib/booletania/attribute.rb +7 -7
- data/lib/booletania/method.rb +69 -0
- data/lib/booletania/version.rb +1 -1
- data/lib/booletania.rb +3 -3
- data/spec/booletania_spec.rb +151 -27
- data/spec/fixtures/locales/en.yml +12 -1
- data/spec/fixtures/locales/ja.yml +12 -1
- data/spec/support/setup_database.rb +10 -3
- metadata +5 -4
- data/lib/booletania/column.rb +0 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ede554f41aa09745752edb4854169d4963e1ed95
|
4
|
+
data.tar.gz: 5a30b3dafe3f9c28f9219ada50ac9010e0b5fdde
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f21d26ce0ae7bad0621cd7845387ab7cfa14d808d49a08a61650385d358f9f851ff5388503f49eef44e3db7bce76362f830091f985ea6a61ae399f65ee45da0
|
7
|
+
data.tar.gz: aca04acd22d101420233411e5cd15fe5c5e9f476b7b7a45622b3d48ba19c3f9b219b53bfe7e956e5b3288425335d8800e5d980bd60787316b1c49d85c86b9a4e
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# Booletania
|
2
2
|
|
3
|
-
|
3
|
+
[![Build Status](https://travis-ci.org/ryoff/booletania.svg?branch=master)](https://travis-ci.org/ryoff/booletania)
|
4
|
+
|
5
|
+
Translate ActiveRecord's boolean column
|
4
6
|
|
5
7
|
## Installation
|
6
8
|
|
@@ -22,7 +24,7 @@ Or install it yourself as:
|
|
22
24
|
|
23
25
|
migration
|
24
26
|
|
25
|
-
|
27
|
+
```ruby
|
26
28
|
create_table(:invitations) do |t|
|
27
29
|
t.boolean :accepted
|
28
30
|
end
|
@@ -30,7 +32,7 @@ end
|
|
30
32
|
|
31
33
|
model
|
32
34
|
|
33
|
-
|
35
|
+
```ruby
|
34
36
|
class Invitation < ActiveRecord::Base
|
35
37
|
include Booletania
|
36
38
|
end
|
@@ -38,7 +40,16 @@ end
|
|
38
40
|
|
39
41
|
i18n
|
40
42
|
|
41
|
-
|
43
|
+
```ruby
|
44
|
+
ja:
|
45
|
+
activerecord:
|
46
|
+
attributes:
|
47
|
+
invitation/accepted:
|
48
|
+
'true': 承諾
|
49
|
+
'false': 拒否
|
50
|
+
```
|
51
|
+
or
|
52
|
+
```ruby
|
42
53
|
ja:
|
43
54
|
booletania:
|
44
55
|
invitation:
|
@@ -53,7 +64,7 @@ ja:
|
|
53
64
|
- https://groups.google.com/forum/#!topic/rails-i18n/aL-Ed1Y1KGo
|
54
65
|
|
55
66
|
### #xxx_text
|
56
|
-
|
67
|
+
```ruby
|
57
68
|
invitation.accepted = true
|
58
69
|
invitation.accepted_text # => "承諾"
|
59
70
|
|
@@ -68,7 +79,7 @@ invitation.accepted_text # => "accept"
|
|
68
79
|
```
|
69
80
|
|
70
81
|
### .xxx_options
|
71
|
-
|
82
|
+
```ruby
|
72
83
|
Invitation.accepted_options # => [['accept', true], ['deny', false]]
|
73
84
|
|
74
85
|
I18n.locale = :ja
|
@@ -76,7 +87,7 @@ Invitation.accepted_options # => [["承諾", true], ["拒否", false]]
|
|
76
87
|
```
|
77
88
|
|
78
89
|
for use in form
|
79
|
-
|
90
|
+
```ruby
|
80
91
|
f.collection_radio_buttons :accepted, Invitation.accepted_options, :last, :first
|
81
92
|
```
|
82
93
|
|
data/lib/booletania/attribute.rb
CHANGED
@@ -3,22 +3,22 @@ module Booletania
|
|
3
3
|
class << self
|
4
4
|
def define_methods!(klass, boolean_columns)
|
5
5
|
boolean_columns.each do |boolean_column|
|
6
|
-
|
6
|
+
method_obj = Booletania::Method.new(klass, boolean_column)
|
7
7
|
|
8
|
-
define_attribute_text(
|
8
|
+
define_attribute_text(method_obj)
|
9
9
|
|
10
|
-
define_attribute_options(
|
10
|
+
define_attribute_options(method_obj)
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
14
|
private
|
15
15
|
|
16
|
-
def define_attribute_text(
|
17
|
-
|
16
|
+
def define_attribute_text(method_obj)
|
17
|
+
method_obj.klass.class_eval method_obj._text, __FILE__, __LINE__ + 1
|
18
18
|
end
|
19
19
|
|
20
|
-
def define_attribute_options(
|
21
|
-
|
20
|
+
def define_attribute_options(method_obj)
|
21
|
+
method_obj.klass.instance_eval method_obj._options, __FILE__, __LINE__ + 1
|
22
22
|
end
|
23
23
|
end
|
24
24
|
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module Booletania
|
2
|
+
class Method
|
3
|
+
attr_reader :klass, :boolean_column
|
4
|
+
|
5
|
+
def initialize(klass, boolean_column)
|
6
|
+
@klass = klass
|
7
|
+
@boolean_column = boolean_column
|
8
|
+
end
|
9
|
+
|
10
|
+
def _text
|
11
|
+
i18n_true_keys = i18n_keys('true') + [true.to_s.humanize]
|
12
|
+
i18n_false_keys = i18n_keys('false') + [false.to_s.humanize]
|
13
|
+
<<-RUBY
|
14
|
+
def #{boolean_column.name}_text
|
15
|
+
keys = #{boolean_column.name}? ? #{i18n_true_keys} : #{i18n_false_keys}
|
16
|
+
|
17
|
+
I18n.t(keys[0], default: keys[1..-1])
|
18
|
+
end
|
19
|
+
RUBY
|
20
|
+
end
|
21
|
+
|
22
|
+
def _options
|
23
|
+
path_keys = i18n_path_keys + [{}]
|
24
|
+
<<-RUBY
|
25
|
+
def #{boolean_column.name}_options
|
26
|
+
I18n.t("#{path_keys[0]}", default: #{path_keys[1..-1]}).invert.map { |k, v| [k, v.to_b] }
|
27
|
+
end
|
28
|
+
RUBY
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
# For example,
|
34
|
+
# [
|
35
|
+
# :booletania.invitation.accepted.true,
|
36
|
+
# :activerecord.attributes.invitation/accepted.true
|
37
|
+
# ]
|
38
|
+
# For example,
|
39
|
+
# [
|
40
|
+
# :booletania.invitation.accepted.false,
|
41
|
+
# :activerecord.attributes.invitation/accepted.false
|
42
|
+
# ]
|
43
|
+
def i18n_keys(true_or_false)
|
44
|
+
i18n_path_keys.map do |i18n_path_key|
|
45
|
+
(i18n_path_key.to_s + '.' + true_or_false.to_b.to_s).to_sym
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def i18n_path_keys
|
50
|
+
@i18n_path_keys ||= begin
|
51
|
+
[].tap do |list|
|
52
|
+
list << booletania_i18n_path_key
|
53
|
+
list << activerecord_i18n_path_key
|
54
|
+
list
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# For example, :booletania.invitation.accepted
|
60
|
+
def booletania_i18n_path_key
|
61
|
+
:"booletania.#{klass.name.underscore}.#{boolean_column.name}"
|
62
|
+
end
|
63
|
+
|
64
|
+
# For example, :activerecord.attributes.invitation/accepted
|
65
|
+
def activerecord_i18n_path_key
|
66
|
+
:"activerecord.attributes.#{klass.name.underscore}/#{boolean_column.name}"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/booletania/version.rb
CHANGED
data/lib/booletania.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
require "booletania/version"
|
2
|
-
require "booletania/
|
2
|
+
require "booletania/method"
|
3
3
|
require "booletania/attribute"
|
4
4
|
|
5
5
|
module Booletania
|
6
6
|
extend ActiveSupport::Concern
|
7
7
|
|
8
8
|
included do
|
9
|
-
|
10
|
-
|
9
|
+
raise ArgumentError, "booletania only support ActiveRecord" unless ancestors.include? ActiveRecord::Base
|
10
|
+
raise ArgumentError, "not found .columns method" unless respond_to? :columns
|
11
11
|
|
12
12
|
Booletania::Attribute.define_methods!(self, columns.select{ |column| column.type == :boolean })
|
13
13
|
end
|
data/spec/booletania_spec.rb
CHANGED
@@ -25,63 +25,187 @@ describe Booletania do
|
|
25
25
|
end
|
26
26
|
|
27
27
|
describe "#_text" do
|
28
|
-
let!(:invitation) { Invitation.create(
|
28
|
+
let!(:invitation) { Invitation.create(accepted1: accepted1, accepted2: accepted2, accepted3: accepted3, accepted4: accepted4) }
|
29
|
+
let(:accepted1) { true }
|
30
|
+
let(:accepted2) { true }
|
31
|
+
let(:accepted3) { true }
|
32
|
+
let(:accepted4) { true }
|
29
33
|
after { Invitation.delete_all }
|
30
34
|
|
31
|
-
|
35
|
+
shared_examples_for 'translated by :booletania i18n key' do
|
36
|
+
context "lang is ja" do
|
37
|
+
before { I18n.locale = :ja }
|
32
38
|
|
33
|
-
|
34
|
-
|
39
|
+
context "column is true" do
|
40
|
+
let(:accepted) { true }
|
41
|
+
it { is_expected.to eq '承諾' }
|
42
|
+
end
|
35
43
|
|
36
|
-
|
37
|
-
|
44
|
+
context "column is false" do
|
45
|
+
let(:accepted) { false }
|
46
|
+
it { is_expected.to eq '拒否' }
|
47
|
+
end
|
48
|
+
end
|
38
49
|
|
39
|
-
|
50
|
+
context "lang is en" do
|
51
|
+
before { I18n.locale = :en }
|
52
|
+
|
53
|
+
context "column is true" do
|
54
|
+
let(:accepted) { true }
|
55
|
+
it { is_expected.to eq 'accept' }
|
56
|
+
end
|
57
|
+
|
58
|
+
context "column is false" do
|
59
|
+
let(:accepted) { false }
|
60
|
+
it { is_expected.to eq 'deny' }
|
61
|
+
end
|
40
62
|
end
|
41
63
|
|
42
|
-
context "
|
43
|
-
|
64
|
+
context "lans is invalid" do
|
65
|
+
before { I18n.locale = :xx }
|
66
|
+
let(:accepted) { true }
|
44
67
|
|
45
|
-
it { is_expected.to eq '
|
68
|
+
it { is_expected.to eq 'True' }
|
46
69
|
end
|
47
70
|
end
|
48
71
|
|
49
|
-
|
50
|
-
|
72
|
+
shared_examples_for 'translated by :activerecord i18n key' do
|
73
|
+
context "lang is ja" do
|
74
|
+
before { I18n.locale = :ja }
|
51
75
|
|
52
|
-
|
76
|
+
context "column is true" do
|
77
|
+
let(:accepted) { true }
|
78
|
+
it { is_expected.to eq '許可' }
|
79
|
+
end
|
80
|
+
|
81
|
+
context "column is false" do
|
82
|
+
let(:accepted) { false }
|
83
|
+
it { is_expected.to eq '不可' }
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context "lang is en" do
|
88
|
+
before { I18n.locale = :en }
|
89
|
+
|
90
|
+
context "column is true" do
|
91
|
+
let(:accepted) { true }
|
92
|
+
it { is_expected.to eq 'ok' }
|
93
|
+
end
|
94
|
+
|
95
|
+
context "column is false" do
|
96
|
+
let(:accepted) { false }
|
97
|
+
it { is_expected.to eq 'ng' }
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
context "lans is invalid" do
|
102
|
+
before { I18n.locale = :xx }
|
53
103
|
let(:accepted) { true }
|
54
104
|
|
55
|
-
it { is_expected.to eq '
|
105
|
+
it { is_expected.to eq 'True' }
|
56
106
|
end
|
107
|
+
end
|
57
108
|
|
58
|
-
|
59
|
-
|
109
|
+
describe "#accepted1_text. has only a :booletania i18n key" do
|
110
|
+
subject { invitation.accepted1_text }
|
111
|
+
let(:accepted1) { accepted }
|
112
|
+
it_behaves_like 'translated by :booletania i18n key'
|
113
|
+
end
|
114
|
+
|
115
|
+
describe "#accepted2_text. has :booletania and :activerecord i18n keys" do
|
116
|
+
subject { invitation.accepted2_text }
|
117
|
+
let(:accepted2) { accepted }
|
118
|
+
it_behaves_like 'translated by :booletania i18n key'
|
119
|
+
end
|
120
|
+
|
121
|
+
describe "#accepted3_text. has only a :activerecord i18n key" do
|
122
|
+
subject { invitation.accepted3_text }
|
123
|
+
let(:accepted3) { accepted }
|
124
|
+
it_behaves_like 'translated by :activerecord i18n key'
|
125
|
+
end
|
126
|
+
|
127
|
+
describe "#accepted4_text. do not have any i18n keys" do
|
128
|
+
subject { invitation.accepted4_text }
|
60
129
|
|
61
|
-
|
130
|
+
context "column is true" do
|
131
|
+
let(:accepted4) { true }
|
132
|
+
it { is_expected.to eq 'True' }
|
133
|
+
end
|
134
|
+
|
135
|
+
context "column is false" do
|
136
|
+
let(:accepted4) { false }
|
137
|
+
it { is_expected.to eq 'False' }
|
62
138
|
end
|
63
139
|
end
|
64
140
|
end
|
65
141
|
|
66
142
|
describe "._options" do
|
67
|
-
|
143
|
+
shared_examples_for 'translated by :booletania i18n key' do
|
144
|
+
context "lang is ja" do
|
145
|
+
before { I18n.locale = :ja }
|
146
|
+
it { is_expected.to eq [['承諾', true], ['拒否', false]] }
|
147
|
+
end
|
148
|
+
|
149
|
+
context "lang is en" do
|
150
|
+
before { I18n.locale = :en }
|
151
|
+
it { is_expected.to eq [['accept', true], ['deny', false]] }
|
152
|
+
end
|
153
|
+
|
154
|
+
context "lans is invalid" do
|
155
|
+
before { I18n.locale = :xx }
|
156
|
+
it { is_expected.to eq [] }
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
shared_examples_for 'translated by :activerecord i18n key' do
|
161
|
+
context "lang is ja" do
|
162
|
+
before { I18n.locale = :ja }
|
163
|
+
it { is_expected.to eq [['許可', true], ['不可', false]] }
|
164
|
+
end
|
165
|
+
|
166
|
+
context "lang is en" do
|
167
|
+
before { I18n.locale = :en }
|
168
|
+
it { is_expected.to eq [['ok', true], ['ng', false]] }
|
169
|
+
end
|
68
170
|
|
69
|
-
|
70
|
-
|
171
|
+
context "lans is invalid" do
|
172
|
+
before { I18n.locale = :xx }
|
173
|
+
it { is_expected.to eq [] }
|
174
|
+
end
|
175
|
+
end
|
71
176
|
|
72
|
-
|
177
|
+
describe ".accepted1_options. has only a :booletania i18n key" do
|
178
|
+
subject { Invitation.accepted1_options }
|
179
|
+
it_behaves_like 'translated by :booletania i18n key'
|
73
180
|
end
|
74
181
|
|
75
|
-
|
76
|
-
|
182
|
+
describe ".accepted2_options. has :booletania and :activerecord i18n keys" do
|
183
|
+
subject { Invitation.accepted2_options }
|
184
|
+
it_behaves_like 'translated by :booletania i18n key'
|
185
|
+
end
|
77
186
|
|
78
|
-
|
187
|
+
describe ".accepted3_options. has only a :activerecord i18n key" do
|
188
|
+
subject { Invitation.accepted3_options }
|
189
|
+
it_behaves_like 'translated by :activerecord i18n key'
|
79
190
|
end
|
80
191
|
|
81
|
-
|
82
|
-
|
192
|
+
describe ".accepted4_options. do not have any i18n keys" do
|
193
|
+
subject { Invitation.accepted4_options }
|
83
194
|
|
84
|
-
|
195
|
+
context "lang is ja" do
|
196
|
+
before { I18n.locale = :ja }
|
197
|
+
it { is_expected.to eq [] }
|
198
|
+
end
|
199
|
+
|
200
|
+
context "lang is en" do
|
201
|
+
before { I18n.locale = :en }
|
202
|
+
it { is_expected.to eq [] }
|
203
|
+
end
|
204
|
+
|
205
|
+
context "lans is invalid" do
|
206
|
+
before { I18n.locale = :xx }
|
207
|
+
it { is_expected.to eq [] }
|
208
|
+
end
|
85
209
|
end
|
86
210
|
end
|
87
211
|
end
|
@@ -1,6 +1,17 @@
|
|
1
1
|
en:
|
2
|
+
activerecord:
|
3
|
+
attributes:
|
4
|
+
invitation/accepted2:
|
5
|
+
'true': ok
|
6
|
+
'false': ng
|
7
|
+
invitation/accepted3:
|
8
|
+
'true': ok
|
9
|
+
'false': ng
|
2
10
|
booletania:
|
3
11
|
invitation:
|
4
|
-
|
12
|
+
accepted1:
|
13
|
+
'true': accept
|
14
|
+
'false': deny
|
15
|
+
accepted2:
|
5
16
|
'true': accept
|
6
17
|
'false': deny
|
@@ -1,6 +1,17 @@
|
|
1
1
|
ja:
|
2
|
+
activerecord:
|
3
|
+
attributes:
|
4
|
+
invitation/accepted2:
|
5
|
+
'true': 許可
|
6
|
+
'false': 不可
|
7
|
+
invitation/accepted3:
|
8
|
+
'true': 許可
|
9
|
+
'false': 不可
|
2
10
|
booletania:
|
3
11
|
invitation:
|
4
|
-
|
12
|
+
accepted1:
|
13
|
+
'true': 承諾
|
14
|
+
'false': 拒否
|
15
|
+
accepted2:
|
5
16
|
'true': 承諾
|
6
17
|
'false': 拒否
|
@@ -1,10 +1,17 @@
|
|
1
|
-
ActiveRecord::Base.configurations =
|
1
|
+
ActiveRecord::Base.configurations = if ENV['TRAVIS']
|
2
|
+
YAML.load_file("config/database.yml")
|
3
|
+
else
|
4
|
+
{ 'test' => { 'adapter' => 'sqlite3', 'database' => ':memory:' } }
|
5
|
+
end
|
2
6
|
ActiveRecord::Base.establish_connection :test
|
3
7
|
|
4
|
-
class CreateAllTables < ActiveRecord::Migration
|
8
|
+
class CreateAllTables < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[5.0] : ActiveRecord::Migration[4.2]
|
5
9
|
def self.up
|
6
10
|
create_table(:invitations) do |t|
|
7
|
-
t.boolean :
|
11
|
+
t.boolean :accepted1
|
12
|
+
t.boolean :accepted2
|
13
|
+
t.boolean :accepted3
|
14
|
+
t.boolean :accepted4
|
8
15
|
end
|
9
16
|
end
|
10
17
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: booletania
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ryoff
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-02-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -137,9 +137,10 @@ files:
|
|
137
137
|
- README.md
|
138
138
|
- Rakefile
|
139
139
|
- booletania.gemspec
|
140
|
+
- config/database.travis.yml
|
140
141
|
- lib/booletania.rb
|
141
142
|
- lib/booletania/attribute.rb
|
142
|
-
- lib/booletania/
|
143
|
+
- lib/booletania/method.rb
|
143
144
|
- lib/booletania/version.rb
|
144
145
|
- spec/booletania_spec.rb
|
145
146
|
- spec/fixtures/locales/en.yml
|
@@ -166,7 +167,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
166
167
|
version: '0'
|
167
168
|
requirements: []
|
168
169
|
rubyforge_project:
|
169
|
-
rubygems_version: 2.
|
170
|
+
rubygems_version: 2.5.1
|
170
171
|
signing_key:
|
171
172
|
specification_version: 4
|
172
173
|
summary: translating AR booleans in I18n files
|
data/lib/booletania/column.rb
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
module Booletania
|
2
|
-
class Column
|
3
|
-
attr_accessor :klass, :boolean_column
|
4
|
-
|
5
|
-
def initialize(klass, boolean_column)
|
6
|
-
@klass = klass
|
7
|
-
@boolean_column = boolean_column
|
8
|
-
end
|
9
|
-
|
10
|
-
def booletania_i18n_path
|
11
|
-
"booletania.#{@klass.name.underscore}.#{@boolean_column.name}"
|
12
|
-
end
|
13
|
-
|
14
|
-
def _text
|
15
|
-
<<-RUBY
|
16
|
-
def #{@boolean_column.name}_text
|
17
|
-
I18n.t "#{booletania_i18n_path}." + #{@boolean_column.name}.__send__(:to_s), default: #{@boolean_column.name}.__send__(:to_s)
|
18
|
-
end
|
19
|
-
RUBY
|
20
|
-
end
|
21
|
-
|
22
|
-
def _options
|
23
|
-
<<-RUBY
|
24
|
-
def self.#{@boolean_column.name}_options
|
25
|
-
(I18n.t "#{booletania_i18n_path}", default: {}).invert.map { |k, v| [k, v.to_b] }
|
26
|
-
end
|
27
|
-
RUBY
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|