active_record_delocalize 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +74 -0
- data/.travis.yml +7 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +176 -0
- data/LICENSE.txt +21 -0
- data/README.md +99 -0
- data/Rakefile +8 -0
- data/active_record_delocalize.gemspec +32 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/active_record_delocalize.rb +19 -0
- data/lib/active_record_delocalize/error.rb +5 -0
- data/lib/active_record_delocalize/parser.rb +49 -0
- data/lib/active_record_delocalize/parsers/date_time.rb +49 -0
- data/lib/active_record_delocalize/parsers/number.rb +22 -0
- data/lib/active_record_delocalize/railtie.rb +14 -0
- data/lib/active_record_delocalize/version.rb +5 -0
- metadata +133 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2a62656628c4172246660bdb3e5cbf9c11e6160a1c36fe0cdc2ace9eab6ba4d8
|
4
|
+
data.tar.gz: c421b30e6f6e6aeb8495d70e7c0e0018ac6ebd2405bedb7736caed9272f5b9ad
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9ad3bf0ec8f7684e8c1693a0462f3a19c510ceb1cc6dcd1f2a6629480fc6dbfc1b8876ab0d577ce97e4c194a5f26a540ff95d36748ddf5db6087841ffa7abd0a
|
7
|
+
data.tar.gz: eba8b7470d421e1143bf2af4809150201974cc50e0fabaca0b07dcaa1050a2fb42438e321670f5b1c6bb6bb5cb363b6f644143db495fa7a87ad8902c396273a6
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.4
|
3
|
+
|
4
|
+
Metrics/BlockLength:
|
5
|
+
Exclude:
|
6
|
+
- "**/*_spec.rb"
|
7
|
+
|
8
|
+
Metrics/MethodLength:
|
9
|
+
Max: 40
|
10
|
+
|
11
|
+
Metrics/ModuleLength:
|
12
|
+
Max: 200
|
13
|
+
Exclude:
|
14
|
+
- "**/*_spec.rb"
|
15
|
+
|
16
|
+
Metrics/AbcSize:
|
17
|
+
Enabled: false
|
18
|
+
|
19
|
+
Metrics/CyclomaticComplexity:
|
20
|
+
Enabled: false
|
21
|
+
|
22
|
+
Metrics/PerceivedComplexity:
|
23
|
+
Enabled: false
|
24
|
+
|
25
|
+
Layout/LineLength:
|
26
|
+
Max: 120
|
27
|
+
|
28
|
+
Layout/ParameterAlignment:
|
29
|
+
EnforcedStyle: with_fixed_indentation
|
30
|
+
|
31
|
+
Layout/CaseIndentation:
|
32
|
+
EnforcedStyle: case
|
33
|
+
SupportedStyles:
|
34
|
+
- case
|
35
|
+
- end
|
36
|
+
IndentOneStep: true
|
37
|
+
|
38
|
+
Layout/EndAlignment:
|
39
|
+
EnforcedStyleAlignWith: variable
|
40
|
+
|
41
|
+
Style/PercentLiteralDelimiters:
|
42
|
+
PreferredDelimiters:
|
43
|
+
'%w': "[]"
|
44
|
+
'%W': "[]"
|
45
|
+
|
46
|
+
Style/StringLiterals:
|
47
|
+
EnforcedStyle: double_quotes
|
48
|
+
|
49
|
+
Style/StringLiteralsInInterpolation:
|
50
|
+
EnforcedStyle: double_quotes
|
51
|
+
|
52
|
+
Style/StructInheritance:
|
53
|
+
Enabled: false
|
54
|
+
|
55
|
+
Style/AndOr:
|
56
|
+
Enabled: false
|
57
|
+
|
58
|
+
Style/Not:
|
59
|
+
Enabled: false
|
60
|
+
|
61
|
+
Style/DoubleNegation:
|
62
|
+
Enabled: false
|
63
|
+
|
64
|
+
Style/HashEachMethods:
|
65
|
+
Enabled: true
|
66
|
+
|
67
|
+
Style/HashTransformKeys:
|
68
|
+
Enabled: true
|
69
|
+
|
70
|
+
Style/HashTransformValues:
|
71
|
+
Enabled: true
|
72
|
+
|
73
|
+
Documentation:
|
74
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
active_record_delocalize (1.0.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
actioncable (6.0.2.2)
|
10
|
+
actionpack (= 6.0.2.2)
|
11
|
+
nio4r (~> 2.0)
|
12
|
+
websocket-driver (>= 0.6.1)
|
13
|
+
actionmailbox (6.0.2.2)
|
14
|
+
actionpack (= 6.0.2.2)
|
15
|
+
activejob (= 6.0.2.2)
|
16
|
+
activerecord (= 6.0.2.2)
|
17
|
+
activestorage (= 6.0.2.2)
|
18
|
+
activesupport (= 6.0.2.2)
|
19
|
+
mail (>= 2.7.1)
|
20
|
+
actionmailer (6.0.2.2)
|
21
|
+
actionpack (= 6.0.2.2)
|
22
|
+
actionview (= 6.0.2.2)
|
23
|
+
activejob (= 6.0.2.2)
|
24
|
+
mail (~> 2.5, >= 2.5.4)
|
25
|
+
rails-dom-testing (~> 2.0)
|
26
|
+
actionpack (6.0.2.2)
|
27
|
+
actionview (= 6.0.2.2)
|
28
|
+
activesupport (= 6.0.2.2)
|
29
|
+
rack (~> 2.0, >= 2.0.8)
|
30
|
+
rack-test (>= 0.6.3)
|
31
|
+
rails-dom-testing (~> 2.0)
|
32
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
33
|
+
actiontext (6.0.2.2)
|
34
|
+
actionpack (= 6.0.2.2)
|
35
|
+
activerecord (= 6.0.2.2)
|
36
|
+
activestorage (= 6.0.2.2)
|
37
|
+
activesupport (= 6.0.2.2)
|
38
|
+
nokogiri (>= 1.8.5)
|
39
|
+
actionview (6.0.2.2)
|
40
|
+
activesupport (= 6.0.2.2)
|
41
|
+
builder (~> 3.1)
|
42
|
+
erubi (~> 1.4)
|
43
|
+
rails-dom-testing (~> 2.0)
|
44
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
45
|
+
activejob (6.0.2.2)
|
46
|
+
activesupport (= 6.0.2.2)
|
47
|
+
globalid (>= 0.3.6)
|
48
|
+
activemodel (6.0.2.2)
|
49
|
+
activesupport (= 6.0.2.2)
|
50
|
+
activerecord (6.0.2.2)
|
51
|
+
activemodel (= 6.0.2.2)
|
52
|
+
activesupport (= 6.0.2.2)
|
53
|
+
activestorage (6.0.2.2)
|
54
|
+
actionpack (= 6.0.2.2)
|
55
|
+
activejob (= 6.0.2.2)
|
56
|
+
activerecord (= 6.0.2.2)
|
57
|
+
marcel (~> 0.3.1)
|
58
|
+
activesupport (6.0.2.2)
|
59
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
60
|
+
i18n (>= 0.7, < 2)
|
61
|
+
minitest (~> 5.1)
|
62
|
+
tzinfo (~> 1.1)
|
63
|
+
zeitwerk (~> 2.2)
|
64
|
+
ast (2.4.0)
|
65
|
+
builder (3.2.4)
|
66
|
+
concurrent-ruby (1.1.6)
|
67
|
+
crass (1.0.6)
|
68
|
+
diff-lcs (1.3)
|
69
|
+
erubi (1.9.0)
|
70
|
+
globalid (0.4.2)
|
71
|
+
activesupport (>= 4.2.0)
|
72
|
+
i18n (1.8.2)
|
73
|
+
concurrent-ruby (~> 1.0)
|
74
|
+
jaro_winkler (1.5.4)
|
75
|
+
loofah (2.4.0)
|
76
|
+
crass (~> 1.0.2)
|
77
|
+
nokogiri (>= 1.5.9)
|
78
|
+
mail (2.7.1)
|
79
|
+
mini_mime (>= 0.1.1)
|
80
|
+
marcel (0.3.3)
|
81
|
+
mimemagic (~> 0.3.2)
|
82
|
+
method_source (1.0.0)
|
83
|
+
mimemagic (0.3.4)
|
84
|
+
mini_mime (1.0.2)
|
85
|
+
mini_portile2 (2.4.0)
|
86
|
+
minitest (5.14.0)
|
87
|
+
nio4r (2.5.2)
|
88
|
+
nokogiri (1.10.9)
|
89
|
+
mini_portile2 (~> 2.4.0)
|
90
|
+
parallel (1.19.1)
|
91
|
+
parser (2.7.0.4)
|
92
|
+
ast (~> 2.4.0)
|
93
|
+
rack (2.2.2)
|
94
|
+
rack-test (1.1.0)
|
95
|
+
rack (>= 1.0, < 3)
|
96
|
+
rails (6.0.2.2)
|
97
|
+
actioncable (= 6.0.2.2)
|
98
|
+
actionmailbox (= 6.0.2.2)
|
99
|
+
actionmailer (= 6.0.2.2)
|
100
|
+
actionpack (= 6.0.2.2)
|
101
|
+
actiontext (= 6.0.2.2)
|
102
|
+
actionview (= 6.0.2.2)
|
103
|
+
activejob (= 6.0.2.2)
|
104
|
+
activemodel (= 6.0.2.2)
|
105
|
+
activerecord (= 6.0.2.2)
|
106
|
+
activestorage (= 6.0.2.2)
|
107
|
+
activesupport (= 6.0.2.2)
|
108
|
+
bundler (>= 1.3.0)
|
109
|
+
railties (= 6.0.2.2)
|
110
|
+
sprockets-rails (>= 2.0.0)
|
111
|
+
rails-dom-testing (2.0.3)
|
112
|
+
activesupport (>= 4.2.0)
|
113
|
+
nokogiri (>= 1.6)
|
114
|
+
rails-html-sanitizer (1.3.0)
|
115
|
+
loofah (~> 2.3)
|
116
|
+
railties (6.0.2.2)
|
117
|
+
actionpack (= 6.0.2.2)
|
118
|
+
activesupport (= 6.0.2.2)
|
119
|
+
method_source
|
120
|
+
rake (>= 0.8.7)
|
121
|
+
thor (>= 0.20.3, < 2.0)
|
122
|
+
rainbow (3.0.0)
|
123
|
+
rake (10.5.0)
|
124
|
+
rexml (3.2.4)
|
125
|
+
rspec (3.8.0)
|
126
|
+
rspec-core (~> 3.8.0)
|
127
|
+
rspec-expectations (~> 3.8.0)
|
128
|
+
rspec-mocks (~> 3.8.0)
|
129
|
+
rspec-core (3.8.0)
|
130
|
+
rspec-support (~> 3.8.0)
|
131
|
+
rspec-expectations (3.8.2)
|
132
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
133
|
+
rspec-support (~> 3.8.0)
|
134
|
+
rspec-mocks (3.8.0)
|
135
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
136
|
+
rspec-support (~> 3.8.0)
|
137
|
+
rspec-support (3.8.0)
|
138
|
+
rubocop (0.80.1)
|
139
|
+
jaro_winkler (~> 1.5.1)
|
140
|
+
parallel (~> 1.10)
|
141
|
+
parser (>= 2.7.0.1)
|
142
|
+
rainbow (>= 2.2.2, < 4.0)
|
143
|
+
rexml
|
144
|
+
ruby-progressbar (~> 1.7)
|
145
|
+
unicode-display_width (>= 1.4.0, < 1.7)
|
146
|
+
ruby-progressbar (1.10.1)
|
147
|
+
sprockets (4.0.0)
|
148
|
+
concurrent-ruby (~> 1.0)
|
149
|
+
rack (> 1, < 3)
|
150
|
+
sprockets-rails (3.2.1)
|
151
|
+
actionpack (>= 4.0)
|
152
|
+
activesupport (>= 4.0)
|
153
|
+
sprockets (>= 3.0.0)
|
154
|
+
thor (1.0.1)
|
155
|
+
thread_safe (0.3.6)
|
156
|
+
tzinfo (1.2.6)
|
157
|
+
thread_safe (~> 0.1)
|
158
|
+
unicode-display_width (1.6.1)
|
159
|
+
websocket-driver (0.7.1)
|
160
|
+
websocket-extensions (>= 0.1.0)
|
161
|
+
websocket-extensions (0.1.4)
|
162
|
+
zeitwerk (2.3.0)
|
163
|
+
|
164
|
+
PLATFORMS
|
165
|
+
ruby
|
166
|
+
|
167
|
+
DEPENDENCIES
|
168
|
+
active_record_delocalize!
|
169
|
+
bundler (~> 2.0)
|
170
|
+
rails (>= 4)
|
171
|
+
rake (~> 10.0)
|
172
|
+
rspec (~> 3.0)
|
173
|
+
rubocop (= 0.80.1)
|
174
|
+
|
175
|
+
BUNDLED WITH
|
176
|
+
2.0.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 heckfy
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
# ActiveRecordDelocalize
|
2
|
+
|
3
|
+
active_record_delocalize provides localized date, time and number parsing functionality for active_record attributes.
|
4
|
+
|
5
|
+
## What does it do?
|
6
|
+
|
7
|
+
Sometimes you want to save localized data (like date or number) to database:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
order = Order.new
|
11
|
+
order.cost = "$ 6 999,99"
|
12
|
+
order.save
|
13
|
+
```
|
14
|
+
|
15
|
+
But first you need to parse them to active-record acceptable format. You may manual override attribute accessors:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
class Order < ActiveRecord::Base
|
19
|
+
def cost=(cost)
|
20
|
+
write_attribute(:cost, cost.gsub(/\$|\s/, "").gsub(',', '.'))
|
21
|
+
end
|
22
|
+
end
|
23
|
+
```
|
24
|
+
|
25
|
+
But active_record_delocalize make it easier:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
class Order < ActiveRecord::Base
|
29
|
+
delocalize cost: :number
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
## Installation
|
34
|
+
|
35
|
+
Add this line to your application's Gemfile:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
gem 'active_record_delocalize'
|
39
|
+
```
|
40
|
+
|
41
|
+
And then execute:
|
42
|
+
|
43
|
+
$ bundle
|
44
|
+
|
45
|
+
## Usage
|
46
|
+
|
47
|
+
You need specify delocalize value per each model using the following declarative DSL:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
class Order < ActiveRecord::Base
|
51
|
+
delocalize cost: :number, delivered_on: :date
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
Where **cost** and **delivered_on** is attribute names and **number** and **date** is data types. Now **active_record_delocalize** support three types: **date**, **time** and **number**. You can also pass a data hash instead of type, where the key is the data type and the value is the format, or format name from your localize file, e.g.:
|
56
|
+
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
class Order < ActiveRecord::Base
|
60
|
+
delocalize cost: { delimiter: ",", separator: " " }, delivered_on: :short
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
## Configuration
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
In addition to your model setup, you also need to configure your locale file(s). Assuming you want to use all of delocalize's parsers (date, time, number), the required keys are:
|
69
|
+
|
70
|
+
* number.default.format.delimiter
|
71
|
+
* number.default.format.separator
|
72
|
+
* date.formats.default
|
73
|
+
* time.formats.default
|
74
|
+
|
75
|
+
For example:
|
76
|
+
|
77
|
+
```yaml
|
78
|
+
ru:
|
79
|
+
number:
|
80
|
+
default:
|
81
|
+
format:
|
82
|
+
delimiter: "."
|
83
|
+
separator: " "
|
84
|
+
time:
|
85
|
+
formats:
|
86
|
+
default: '%d.%m.%Y в %H:%M'
|
87
|
+
date:
|
88
|
+
formats:
|
89
|
+
default: '%d.%m.%Y'
|
90
|
+
```
|
91
|
+
|
92
|
+
|
93
|
+
## Contributing
|
94
|
+
|
95
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/heckfy/active_record_delocalize. 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.
|
96
|
+
|
97
|
+
## License
|
98
|
+
|
99
|
+
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,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path("lib", __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require "active_record_delocalize/version"
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "active_record_delocalize"
|
9
|
+
spec.version = ActiveRecordDelocalize::VERSION
|
10
|
+
spec.authors = ["heckfy"]
|
11
|
+
spec.email = ["heckfyoz@gmail.com"]
|
12
|
+
|
13
|
+
spec.summary = "Delocalize date, time and number model attributes"
|
14
|
+
spec.description = "Delocalize date, time and number model attributes"
|
15
|
+
spec.homepage = "https://github.com/heckfy/active_record_delocalize"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
end
|
23
|
+
spec.bindir = "exe"
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ["lib"]
|
26
|
+
|
27
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
28
|
+
spec.add_development_dependency "rails", ">= 4"
|
29
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
30
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
31
|
+
spec.add_development_dependency "rubocop", "0.80.1"
|
32
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "active_record_delocalize"
|
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__)
|
data/bin/setup
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# require "active_support"
|
4
|
+
# require "active_record"
|
5
|
+
require "active_record_delocalize/error"
|
6
|
+
require "active_record_delocalize/parsers/date_time"
|
7
|
+
require "active_record_delocalize/parsers/number"
|
8
|
+
require "active_record_delocalize/parser"
|
9
|
+
require "active_record_delocalize/railtie"
|
10
|
+
|
11
|
+
if defined?(Rails)
|
12
|
+
class Railtie < Rails::Railtie
|
13
|
+
initializer "active_record_delocalize.active_record" do
|
14
|
+
ActiveSupport.on_load :active_record do
|
15
|
+
ActiveRecord::Base.extend ActiveRecordDelocalize::Railtie
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecordDelocalize
|
4
|
+
class Parser
|
5
|
+
attr_reader :options, :parser
|
6
|
+
|
7
|
+
def initialize(options)
|
8
|
+
options = { options => default_options_for(options) } if options.is_a?(Symbol)
|
9
|
+
|
10
|
+
format, @options = *options.deep_symbolize_keys.flatten
|
11
|
+
|
12
|
+
raise_unknown_format unless %i[number date time].include?(format)
|
13
|
+
|
14
|
+
@parser = parser_for(format)
|
15
|
+
end
|
16
|
+
|
17
|
+
def call(value)
|
18
|
+
parser.call(value)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def default_options_for(format)
|
24
|
+
case format
|
25
|
+
when :number
|
26
|
+
I18n.t("number.default.format", default: {})
|
27
|
+
when :date, :time
|
28
|
+
I18n.t("#{format}.formats.default", default: nil)
|
29
|
+
else
|
30
|
+
raise_unknown_format
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def parser_for(format)
|
35
|
+
case format
|
36
|
+
when :number
|
37
|
+
ActiveRecordDelocalize::Parsers::Number.new(options)
|
38
|
+
when :date, :time
|
39
|
+
ActiveRecordDelocalize::Parsers::DateTime.new(options, format)
|
40
|
+
else
|
41
|
+
raise_unknown_format
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def raise_unknown_format
|
46
|
+
raise ActiveRecordDelocalize::Error("Unknown format")
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecordDelocalize
|
4
|
+
module Parsers
|
5
|
+
class DateTime
|
6
|
+
attr_reader :format, :type
|
7
|
+
|
8
|
+
def initialize(format, type)
|
9
|
+
@format = format
|
10
|
+
@type = type
|
11
|
+
|
12
|
+
@format = I18n.t!("#{type}.formats.#{@format}") if @format.is_a?(Symbol)
|
13
|
+
end
|
14
|
+
|
15
|
+
def call(value)
|
16
|
+
return value unless format
|
17
|
+
|
18
|
+
value = Time.strptime(translate(value), format)
|
19
|
+
type == :date ? value.to_date : value
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def translates
|
25
|
+
{
|
26
|
+
month_names: Date::MONTHNAMES.compact,
|
27
|
+
abbr_month_names: Date::ABBR_MONTHNAMES.compact,
|
28
|
+
day_names: Date::DAYNAMES,
|
29
|
+
abbr_day_names: Date::ABBR_DAYNAMES
|
30
|
+
}
|
31
|
+
.map do |k, v|
|
32
|
+
Hash[(begin
|
33
|
+
I18n.t!("date.#{k}")
|
34
|
+
rescue StandardError
|
35
|
+
[]
|
36
|
+
end).compact.zip(v)]
|
37
|
+
end
|
38
|
+
.reduce({}, :merge)
|
39
|
+
end
|
40
|
+
|
41
|
+
def translate(value)
|
42
|
+
translates.each do |k, v|
|
43
|
+
value = value.gsub(k, v)
|
44
|
+
end
|
45
|
+
value
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecordDelocalize
|
4
|
+
module Parsers
|
5
|
+
class Number
|
6
|
+
attr_reader :format
|
7
|
+
|
8
|
+
def initialize(format)
|
9
|
+
@format = format
|
10
|
+
|
11
|
+
@format = I18n.t!("number.#{@format}.format") if @format.is_a?(Symbol)
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(value = "")
|
15
|
+
delimiter, separator = format.values_at(:delimiter, :separator)
|
16
|
+
return value if !separator || !delimiter
|
17
|
+
|
18
|
+
value.gsub(separator, "").gsub(delimiter, ".")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecordDelocalize
|
4
|
+
module Railtie
|
5
|
+
def delocalize(attrs)
|
6
|
+
attrs.each do |attr, options|
|
7
|
+
parser = ActiveRecordDelocalize::Parser.new(options)
|
8
|
+
define_method("#{attr}=") do |value|
|
9
|
+
write_attribute(attr.to_sym, parser.call(value))
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_record_delocalize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- heckfy
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-04-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.80.1
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.80.1
|
83
|
+
description: Delocalize date, time and number model attributes
|
84
|
+
email:
|
85
|
+
- heckfyoz@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".rubocop.yml"
|
93
|
+
- ".travis.yml"
|
94
|
+
- Gemfile
|
95
|
+
- Gemfile.lock
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- active_record_delocalize.gemspec
|
100
|
+
- bin/console
|
101
|
+
- bin/setup
|
102
|
+
- lib/active_record_delocalize.rb
|
103
|
+
- lib/active_record_delocalize/error.rb
|
104
|
+
- lib/active_record_delocalize/parser.rb
|
105
|
+
- lib/active_record_delocalize/parsers/date_time.rb
|
106
|
+
- lib/active_record_delocalize/parsers/number.rb
|
107
|
+
- lib/active_record_delocalize/railtie.rb
|
108
|
+
- lib/active_record_delocalize/version.rb
|
109
|
+
homepage: https://github.com/heckfy/active_record_delocalize
|
110
|
+
licenses:
|
111
|
+
- MIT
|
112
|
+
metadata: {}
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
requirements: []
|
128
|
+
rubyforge_project:
|
129
|
+
rubygems_version: 2.7.6.2
|
130
|
+
signing_key:
|
131
|
+
specification_version: 4
|
132
|
+
summary: Delocalize date, time and number model attributes
|
133
|
+
test_files: []
|