concerns_on_rails 1.12.1 → 1.13.0
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 +4 -4
- data/CHANGELOG.md +10 -0
- data/README.md +81 -1
- data/lib/concerns_on_rails/controllers/localizable.rb +96 -0
- data/lib/concerns_on_rails/legacy_aliases.rb +2 -0
- data/lib/concerns_on_rails/models/maskable.rb +79 -0
- data/lib/concerns_on_rails/models/monetizable.rb +89 -0
- data/lib/concerns_on_rails/support/masker.rb +60 -0
- data/lib/concerns_on_rails/support/money.rb +38 -0
- data/lib/concerns_on_rails/version.rb +1 -1
- data/lib/concerns_on_rails.rb +5 -0
- metadata +6 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4b5bc2f3b03563d643ff2e9cffe13038cf90482499d88475ef49fea9d37ea5f9
|
|
4
|
+
data.tar.gz: d4b3228e473a838929c108d548674cb7b17738c62af1e1dee7454651f405e68c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9f860c2f0c0a6d7570985c6a0f13c5b923ddb7aa12bcb08060b9dbbcd7ee6106182679d99ac3e7826835d8ccea708ef8bc82101aeb6c142fe20cbfbac77f636b
|
|
7
|
+
data.tar.gz: 865441dcf91d87c664809919a930ac2aedfa27fce106570a77e0dcf2331cc074313d0f63abecd21691b36c3af5861c72cc21b956ed70e11896ef411737287671
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
<!-- CHANGELOG.md -->
|
|
2
2
|
|
|
3
|
+
## 1.13.0 (2026-06-06)
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
- **Models::Maskable**: non-destructive display masking for sensitive attributes. `maskable :email, with: :email` adds a `masked_<field>` reader and never writes the column (the raw value stays in the DB). Presets `:email` / `:phone` / `:credit_card` / `:last4` / `:all`, a configurable `mask:` character, and a `Proc` escape hatch. Backed by `Support::Masker`; complements `Sanitizable`.
|
|
7
|
+
- **Models::Monetizable**: exact, float-free money handling over an integer subunit column. `monetizable :price_cents` derives `price` / `price=` / `formatted_price` (the `_cents` suffix is stripped, or name them with `as:`). Options `unit:` / `precision:` / `delimiter:` / `separator:` / `subunit_to_unit:`. Uses `BigDecimal` throughout; backed by `Support::Money`.
|
|
8
|
+
- **Controllers::Localizable**: per-request locale selection from `params` and/or the `Accept-Language` header via an `around_action` (`I18n.with_locale`). `localizable available: %i[en fr], default: :en`; options `param:` / `header:`. The resolved locale is always validated against `I18n.available_locales`, so it can never raise `I18n::InvalidLocale`.
|
|
9
|
+
|
|
10
|
+
### Notes
|
|
11
|
+
- All changes are additive and backward-compatible, with zero new runtime dependencies (BigDecimal and I18n already ship with the existing stack). `ConcernsOnRails::Maskable` / `ConcernsOnRails::Monetizable` are aliased to their `Models::*` modules; the controller concern stays namespace-only (`ConcernsOnRails::Controllers::Localizable`).
|
|
12
|
+
|
|
3
13
|
## 1.12.1 (2026-06-06)
|
|
4
14
|
|
|
5
15
|
### Added
|
data/README.md
CHANGED
|
@@ -41,6 +41,8 @@ Article.published.without_deleted.find("hello-world")
|
|
|
41
41
|
- [Addressable](#-addressable) — postal address normalization + format validation
|
|
42
42
|
- [Taggable](#-taggable) — lightweight tagging over a single column
|
|
43
43
|
- [Sanitizable](#-sanitizable) — opt-in HTML sanitization (XSS defense-in-depth)
|
|
44
|
+
- [Maskable](#-maskable) — non-destructive display masking of sensitive fields
|
|
45
|
+
- [Monetizable](#-monetizable) — integer-cents money columns (BigDecimal)
|
|
44
46
|
- **Controller concerns**
|
|
45
47
|
- [Paginatable](#-paginatable) — offset pagination with headers
|
|
46
48
|
- [Filterable](#-filterable) — declarative URL-param filters
|
|
@@ -49,6 +51,7 @@ Article.published.without_deleted.find("hello-world")
|
|
|
49
51
|
- [ErrorHandleable](#-errorhandleable) — JSON `rescue_from` handlers for common controller errors
|
|
50
52
|
- [Includable](#-includable) — whitelisted association sideloading + sparse fieldsets
|
|
51
53
|
- [SecureHeadable](#-secureheadable) — security response headers + native CSP DSL
|
|
54
|
+
- [Localizable](#-localizable) — per-request locale from params / Accept-Language
|
|
52
55
|
- [Module paths & namespacing](#-module-paths--namespacing)
|
|
53
56
|
- [Development](#-development)
|
|
54
57
|
- [Contributing](#-contributing)
|
|
@@ -58,7 +61,7 @@ Article.published.without_deleted.find("hello-world")
|
|
|
58
61
|
|
|
59
62
|
## ✨ Why this gem?
|
|
60
63
|
|
|
61
|
-
- **
|
|
64
|
+
- **Eighteen model concerns + eight controller concerns**, all production-ready
|
|
62
65
|
- **One include, one macro** — no boilerplate, no glue code
|
|
63
66
|
- **Lean dependencies** — only `acts_as_list` (Sortable) and `friendly_id` (Sluggable); controller concerns have zero extra deps
|
|
64
67
|
- **Schema-validated configuration** — every macro checks that the configured column exists and raises `ArgumentError` early
|
|
@@ -871,6 +874,64 @@ article.sanitized_body # => "<b>Hi</b>alert(1)" (script tag
|
|
|
871
874
|
|
|
872
875
|
---
|
|
873
876
|
|
|
877
|
+
## 🙈 Maskable
|
|
878
|
+
|
|
879
|
+
Non-destructive display masking for sensitive attributes. Each declaration adds a `masked_<field>` reader and **never writes the column** — the raw value stays in the database (masking is a presentation concern). Dependency-free.
|
|
880
|
+
|
|
881
|
+
```ruby
|
|
882
|
+
class User < ApplicationRecord
|
|
883
|
+
include ConcernsOnRails::Maskable
|
|
884
|
+
|
|
885
|
+
maskable :email, with: :email # => user.masked_email "j****@example.com"
|
|
886
|
+
maskable :card, with: :credit_card # => user.masked_card "**** **** **** 4242"
|
|
887
|
+
maskable :ssn, with: :last4, mask: "•"
|
|
888
|
+
maskable :token, with: ->(v) { "#{v.to_s[0, 3]}…" }
|
|
889
|
+
end
|
|
890
|
+
```
|
|
891
|
+
|
|
892
|
+
**Presets** (`with:`)
|
|
893
|
+
|
|
894
|
+
| Preset | Result |
|
|
895
|
+
|----------------|---------------------------------------------|
|
|
896
|
+
| `:email` | `j****@example.com` (first char + domain) |
|
|
897
|
+
| `:phone` | `***-2671` (last 4 digits) |
|
|
898
|
+
| `:credit_card` | `**** **** **** 4242` (last 4 digits) |
|
|
899
|
+
| `:last4` | keep the last 4 characters |
|
|
900
|
+
| `:all` | mask every character (the default) |
|
|
901
|
+
| `Proc` | used as-is (you own the non-String guard) |
|
|
902
|
+
|
|
903
|
+
`mask:` sets the mask character (default `*`). Nil and non-string values pass through untouched. To strip dangerous HTML instead, see [Sanitizable](#-sanitizable).
|
|
904
|
+
|
|
905
|
+
---
|
|
906
|
+
|
|
907
|
+
## 💰 Monetizable
|
|
908
|
+
|
|
909
|
+
Money handling for an integer "subunit" column (e.g. cents) — exact and float-free via `BigDecimal`. `monetizable :price_cents` derives three methods (the `_cents` suffix is stripped):
|
|
910
|
+
|
|
911
|
+
```ruby
|
|
912
|
+
class Product < ApplicationRecord
|
|
913
|
+
include ConcernsOnRails::Monetizable
|
|
914
|
+
|
|
915
|
+
monetizable :price_cents # => price / price= / formatted_price
|
|
916
|
+
monetizable :shipping_cents, as: :shipping
|
|
917
|
+
monetizable :total_cents, unit: "€", delimiter: ".", separator: ","
|
|
918
|
+
end
|
|
919
|
+
|
|
920
|
+
product.price = 19.99 # stores price_cents = 1999 (rounded to whole cents)
|
|
921
|
+
product.price # => BigDecimal 19.99
|
|
922
|
+
product.formatted_price # => "$19.99"
|
|
923
|
+
```
|
|
924
|
+
|
|
925
|
+
| Method | Returns |
|
|
926
|
+
|-------------------|-----------------------------------------------|
|
|
927
|
+
| `price` | the amount as a `BigDecimal` (cents ÷ 100) |
|
|
928
|
+
| `price=` | assign in major units; rounded to whole cents |
|
|
929
|
+
| `formatted_price` | a display string (`"$1,234.56"`) |
|
|
930
|
+
|
|
931
|
+
**Options**: `as:` (explicit method name — required when the column does not end in `_cents`), `unit:` (`"$"`), `precision:` (`2`), `delimiter:` (`","`), `separator:` (`"."`), `subunit_to_unit:` (`100`). `nil` stays `nil` across all three methods.
|
|
932
|
+
|
|
933
|
+
---
|
|
934
|
+
|
|
874
935
|
# 🎮 Controller Concerns
|
|
875
936
|
|
|
876
937
|
Pure ActionController + ActiveRecord — **zero extra runtime dependencies** (no Kaminari, Pundit, or Ransack).
|
|
@@ -1134,6 +1195,25 @@ end
|
|
|
1134
1195
|
|
|
1135
1196
|
---
|
|
1136
1197
|
|
|
1198
|
+
## 🌐 Localizable
|
|
1199
|
+
|
|
1200
|
+
Per-request locale selection from the request params and/or the `Accept-Language` header, wrapped in an `around_action` so `I18n.locale` is set for the action and restored afterwards. Dependency-free.
|
|
1201
|
+
|
|
1202
|
+
```ruby
|
|
1203
|
+
class ApplicationController < ActionController::Base
|
|
1204
|
+
include ConcernsOnRails::Controllers::Localizable
|
|
1205
|
+
|
|
1206
|
+
localizable available: %i[en fr de], default: :en
|
|
1207
|
+
# localizable param: :lang, header: false # params[:lang] only
|
|
1208
|
+
end
|
|
1209
|
+
```
|
|
1210
|
+
|
|
1211
|
+
Resolution order: `params[param]` → first match in `Accept-Language` → `default` → `I18n.default_locale`. The chosen locale is always validated against `I18n.available_locales`, so a stray param or a mismatched `available:` list can never raise `I18n::InvalidLocale`.
|
|
1212
|
+
|
|
1213
|
+
**Options**: `available:` (allow-list for matching; defaults to `I18n.available_locales`), `default:`, `param:` (default `:locale`), `header:` (default `true`).
|
|
1214
|
+
|
|
1215
|
+
---
|
|
1216
|
+
|
|
1137
1217
|
## 🗂️ Module paths & namespacing
|
|
1138
1218
|
|
|
1139
1219
|
Every concern is available under two paths:
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
require "active_support/concern"
|
|
2
|
+
|
|
3
|
+
module ConcernsOnRails
|
|
4
|
+
module Controllers
|
|
5
|
+
# Per-request locale selection from the request params and/or the
|
|
6
|
+
# `Accept-Language` header, wrapped in an `around_action` so `I18n.locale`
|
|
7
|
+
# is set for the action and restored afterwards.
|
|
8
|
+
#
|
|
9
|
+
# class ApplicationController < ActionController::Base
|
|
10
|
+
# include ConcernsOnRails::Controllers::Localizable
|
|
11
|
+
#
|
|
12
|
+
# localizable available: %i[en fr de], default: :en
|
|
13
|
+
# # localizable param: :lang, header: false # params[:lang] only
|
|
14
|
+
# end
|
|
15
|
+
#
|
|
16
|
+
# Resolution order: `params[param]` → first match in `Accept-Language` →
|
|
17
|
+
# `default` → `I18n.default_locale`. The chosen locale is always validated
|
|
18
|
+
# against `I18n.available_locales` before use, so a stray param or a
|
|
19
|
+
# mismatched `available:` list can never raise `I18n::InvalidLocale`.
|
|
20
|
+
#
|
|
21
|
+
# Options: `available:` (allow-list for param/header matching; defaults to
|
|
22
|
+
# `I18n.available_locales`), `default:`, `param:` (default `:locale`),
|
|
23
|
+
# `header:` (default `true`).
|
|
24
|
+
module Localizable
|
|
25
|
+
extend ActiveSupport::Concern
|
|
26
|
+
|
|
27
|
+
included do
|
|
28
|
+
class_attribute :localizable_options, instance_accessor: false, default: {}
|
|
29
|
+
around_action :switch_locale
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
class_methods do
|
|
33
|
+
def localizable(available: nil, default: nil, param: :locale, header: true)
|
|
34
|
+
self.localizable_options = {
|
|
35
|
+
available: available&.map(&:to_sym),
|
|
36
|
+
default: default&.to_sym,
|
|
37
|
+
param: param&.to_sym,
|
|
38
|
+
header: header
|
|
39
|
+
}
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Public so subclasses can override; runs the action under the resolved locale.
|
|
44
|
+
def switch_locale(&)
|
|
45
|
+
I18n.with_locale(resolved_locale, &)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# The locale chosen for this request — always one I18n can switch to.
|
|
49
|
+
def resolved_locale
|
|
50
|
+
opts = self.class.localizable_options
|
|
51
|
+
allowed = opts[:available].presence || I18n.available_locales
|
|
52
|
+
candidate = locale_from_param(opts, allowed) || locale_from_header(opts, allowed) || opts[:default]
|
|
53
|
+
|
|
54
|
+
candidate && I18n.available_locales.include?(candidate.to_sym) ? candidate.to_sym : I18n.default_locale
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
def locale_from_param(opts, allowed)
|
|
60
|
+
return nil unless opts[:param] && respond_to?(:params) && params
|
|
61
|
+
|
|
62
|
+
match_locale(params[opts[:param]], allowed)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def locale_from_header(opts, allowed)
|
|
66
|
+
return nil unless opts[:header]
|
|
67
|
+
|
|
68
|
+
header = accept_language_header
|
|
69
|
+
header.blank? ? nil : parse_accept_language(header, allowed)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def accept_language_header
|
|
73
|
+
return nil unless respond_to?(:request)
|
|
74
|
+
|
|
75
|
+
req = request
|
|
76
|
+
req.respond_to?(:headers) ? req.headers["Accept-Language"] : nil
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def parse_accept_language(header, allowed)
|
|
80
|
+
header.split(",").each do |part|
|
|
81
|
+
lang = part.split(";").first.to_s.strip.split("-").first
|
|
82
|
+
match = match_locale(lang, allowed)
|
|
83
|
+
return match if match
|
|
84
|
+
end
|
|
85
|
+
nil
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def match_locale(candidate, allowed)
|
|
89
|
+
return nil if candidate.blank?
|
|
90
|
+
|
|
91
|
+
wanted = candidate.to_s.downcase
|
|
92
|
+
allowed.find { |loc| loc.to_s.downcase == wanted }
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
require "active_support/concern"
|
|
2
|
+
require "concerns_on_rails/support/masker"
|
|
3
|
+
|
|
4
|
+
module ConcernsOnRails
|
|
5
|
+
module Models
|
|
6
|
+
# Non-destructive display masking for sensitive string attributes.
|
|
7
|
+
#
|
|
8
|
+
# Masking is ALWAYS read-only: each declaration adds a `masked_<field>`
|
|
9
|
+
# reader and never writes the stored column (the raw value stays in the DB,
|
|
10
|
+
# because masking is a presentation concern). For stripping dangerous HTML
|
|
11
|
+
# see Models::Sanitizable.
|
|
12
|
+
#
|
|
13
|
+
# class User < ApplicationRecord
|
|
14
|
+
# include ConcernsOnRails::Models::Maskable
|
|
15
|
+
#
|
|
16
|
+
# maskable :email, with: :email # => user.masked_email "j****@example.com"
|
|
17
|
+
# maskable :card, with: :credit_card # => user.masked_card "**** **** **** 4242"
|
|
18
|
+
# maskable :ssn, with: :last4, mask: "•"
|
|
19
|
+
# maskable :token, with: ->(v) { "#{v.to_s[0, 3]}…" }
|
|
20
|
+
# end
|
|
21
|
+
#
|
|
22
|
+
# Presets (the `with:` argument):
|
|
23
|
+
# :email — mask the local part, keep first char + domain
|
|
24
|
+
# :phone — keep the last 4 digits ("***-2671")
|
|
25
|
+
# :credit_card — keep the last 4 digits ("**** **** **** 4242")
|
|
26
|
+
# :last4 — keep the last 4 characters
|
|
27
|
+
# :all — mask every character (the default)
|
|
28
|
+
# Proc — used as-is (the caller owns the non-String guard)
|
|
29
|
+
#
|
|
30
|
+
# `mask:` sets the mask character (default "*") for the preset forms.
|
|
31
|
+
module Maskable
|
|
32
|
+
extend ActiveSupport::Concern
|
|
33
|
+
|
|
34
|
+
PRESETS = %i[email phone credit_card last4 all].freeze
|
|
35
|
+
|
|
36
|
+
included do
|
|
37
|
+
class_attribute :maskable_rules, instance_accessor: false, default: {}
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
class_methods do
|
|
41
|
+
include ConcernsOnRails::Support::ColumnGuard
|
|
42
|
+
|
|
43
|
+
def maskable(*fields, with: :all, mask: "*")
|
|
44
|
+
raise ArgumentError, "ConcernsOnRails::Models::Maskable: at least one field is required" if fields.empty?
|
|
45
|
+
|
|
46
|
+
masker = resolve_masker(with, mask)
|
|
47
|
+
ensure_columns!("ConcernsOnRails::Models::Maskable", fields)
|
|
48
|
+
|
|
49
|
+
fields.each do |field|
|
|
50
|
+
key = field.to_sym
|
|
51
|
+
self.maskable_rules = maskable_rules.merge(key => masker)
|
|
52
|
+
define_method("masked_#{field}") { masker.call(self[key]) }
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
class_methods do
|
|
58
|
+
private
|
|
59
|
+
|
|
60
|
+
def resolve_masker(with, mask)
|
|
61
|
+
case with
|
|
62
|
+
when Symbol
|
|
63
|
+
unless PRESETS.include?(with)
|
|
64
|
+
raise ArgumentError,
|
|
65
|
+
"ConcernsOnRails::Models::Maskable: unknown preset '#{with}'. " \
|
|
66
|
+
"Valid presets: #{PRESETS.join(', ')}"
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
->(v) { ConcernsOnRails::Support::Masker.public_send(with, v, mask: mask) }
|
|
70
|
+
when Proc then with
|
|
71
|
+
else
|
|
72
|
+
raise ArgumentError,
|
|
73
|
+
"ConcernsOnRails::Models::Maskable: :with must be a preset symbol or a Proc/lambda, got #{with.class}"
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
require "active_support/concern"
|
|
2
|
+
require "concerns_on_rails/support/money"
|
|
3
|
+
|
|
4
|
+
module ConcernsOnRails
|
|
5
|
+
module Models
|
|
6
|
+
# Money handling for an integer "subunit" column (e.g. cents) — exact,
|
|
7
|
+
# float-free, via BigDecimal.
|
|
8
|
+
#
|
|
9
|
+
# Declaring `monetizable :price_cents` adds three methods derived from the
|
|
10
|
+
# column name (the `_cents` suffix is stripped):
|
|
11
|
+
# * `price` — the amount as a BigDecimal (cents / 100)
|
|
12
|
+
# * `price=` — assign in major units; rounded to whole cents
|
|
13
|
+
# * `formatted_price` — a display string ("$1,234.56")
|
|
14
|
+
#
|
|
15
|
+
# class Product < ApplicationRecord
|
|
16
|
+
# include ConcernsOnRails::Models::Monetizable
|
|
17
|
+
#
|
|
18
|
+
# monetizable :price_cents # => price / price= / formatted_price
|
|
19
|
+
# monetizable :shipping_cents, as: :shipping
|
|
20
|
+
# monetizable :total_cents, unit: "€", separator: ",", delimiter: "."
|
|
21
|
+
# end
|
|
22
|
+
#
|
|
23
|
+
# product.price = 19.99 # stores price_cents = 1999
|
|
24
|
+
# product.price # => 0.1999e2 (BigDecimal 19.99)
|
|
25
|
+
# product.formatted_price # => "$19.99"
|
|
26
|
+
#
|
|
27
|
+
# Options: `as:` (explicit method name — required when the column does not
|
|
28
|
+
# end in `_cents`), `unit:` ("$"), `precision:` (2), `delimiter:` (","),
|
|
29
|
+
# `separator:` ("."), `subunit_to_unit:` (100).
|
|
30
|
+
module Monetizable
|
|
31
|
+
extend ActiveSupport::Concern
|
|
32
|
+
|
|
33
|
+
included do
|
|
34
|
+
class_attribute :monetizable_rules, instance_accessor: false, default: {}
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
class_methods do
|
|
38
|
+
include ConcernsOnRails::Support::ColumnGuard
|
|
39
|
+
|
|
40
|
+
def monetizable(*fields, as: nil, unit: "$", precision: 2, delimiter: ",", separator: ".", subunit_to_unit: 100)
|
|
41
|
+
raise ArgumentError, "ConcernsOnRails::Models::Monetizable: at least one field is required" if fields.empty?
|
|
42
|
+
|
|
43
|
+
raise ArgumentError, "ConcernsOnRails::Models::Monetizable: :as cannot be combined with multiple fields" if as && fields.size > 1
|
|
44
|
+
|
|
45
|
+
ensure_columns!("ConcernsOnRails::Models::Monetizable", fields)
|
|
46
|
+
config = { unit: unit, precision: precision, delimiter: delimiter, separator: separator, subunit_to_unit: subunit_to_unit }
|
|
47
|
+
fields.each { |cents_field| define_money_accessors(cents_field.to_sym, as, config) }
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
class_methods do # rubocop:disable Metrics/BlockLength
|
|
52
|
+
private
|
|
53
|
+
|
|
54
|
+
def define_money_accessors(cents_field, as, config)
|
|
55
|
+
name = money_name(cents_field, as)
|
|
56
|
+
subunit = config[:subunit_to_unit]
|
|
57
|
+
self.monetizable_rules = monetizable_rules.merge(cents_field => name)
|
|
58
|
+
|
|
59
|
+
define_method(name) do
|
|
60
|
+
cents = self[cents_field]
|
|
61
|
+
cents.nil? ? nil : BigDecimal(cents.to_s) / subunit
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
define_method("#{name}=") do |amount|
|
|
65
|
+
self[cents_field] = amount.nil? ? nil : (BigDecimal(amount.to_s) * subunit).round
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
define_method("formatted_#{name}") do
|
|
69
|
+
cents = self[cents_field]
|
|
70
|
+
cents.nil? ? nil : ConcernsOnRails::Support::Money.format(cents, config)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def money_name(cents_field, as)
|
|
75
|
+
return as.to_sym if as
|
|
76
|
+
|
|
77
|
+
str = cents_field.to_s
|
|
78
|
+
unless str.end_with?("_cents")
|
|
79
|
+
raise ArgumentError,
|
|
80
|
+
"ConcernsOnRails::Models::Monetizable: cannot derive a money method name from '#{cents_field}' " \
|
|
81
|
+
"(it does not end in '_cents'); pass `as:` to name it explicitly"
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
str.delete_suffix("_cents").to_sym
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
require "active_support/concern"
|
|
2
|
+
|
|
3
|
+
module ConcernsOnRails
|
|
4
|
+
module Support
|
|
5
|
+
# Display-only value-masking helpers shared by Models::Maskable.
|
|
6
|
+
#
|
|
7
|
+
# Every method is string-safe: a non-String argument is returned untouched,
|
|
8
|
+
# exactly like the Normalizable / Sanitizable preset lambdas. Masking is for
|
|
9
|
+
# presentation only — callers keep the original value in the database.
|
|
10
|
+
module Masker
|
|
11
|
+
module_function
|
|
12
|
+
|
|
13
|
+
DEFAULT_MASK = "*".freeze
|
|
14
|
+
|
|
15
|
+
# Replace every character with the mask character.
|
|
16
|
+
def all(value, mask: DEFAULT_MASK)
|
|
17
|
+
value.is_a?(String) ? mask * value.length : value
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Keep only the last four characters visible.
|
|
21
|
+
def last4(value, mask: DEFAULT_MASK)
|
|
22
|
+
return value unless value.is_a?(String)
|
|
23
|
+
|
|
24
|
+
value.length <= 4 ? mask * value.length : (mask * (value.length - 4)) + value[-4..]
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Mask the local part of an email, keeping the first character + domain:
|
|
28
|
+
# "john.doe@example.com" => "j*******@example.com"
|
|
29
|
+
def email(value, mask: DEFAULT_MASK)
|
|
30
|
+
return value unless value.is_a?(String)
|
|
31
|
+
|
|
32
|
+
local, at, domain = value.partition("@")
|
|
33
|
+
return value if at.empty? # not an email-shaped string; leave it alone
|
|
34
|
+
|
|
35
|
+
masked_local = local.length <= 1 ? mask : local[0] + (mask * (local.length - 1))
|
|
36
|
+
"#{masked_local}@#{domain}"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Keep the last four digits of a phone number visible: "***-2671".
|
|
40
|
+
def phone(value, mask: DEFAULT_MASK)
|
|
41
|
+
return value unless value.is_a?(String)
|
|
42
|
+
|
|
43
|
+
digits = value.gsub(/\D/, "")
|
|
44
|
+
return value if digits.empty?
|
|
45
|
+
|
|
46
|
+
"#{mask * 3}-#{digits.length <= 4 ? digits : digits[-4..]}"
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Keep the last four digits of a card number: "**** **** **** 4242".
|
|
50
|
+
def credit_card(value, mask: DEFAULT_MASK)
|
|
51
|
+
return value unless value.is_a?(String)
|
|
52
|
+
|
|
53
|
+
digits = value.gsub(/\D/, "")
|
|
54
|
+
return all(value, mask: mask) if digits.length <= 4
|
|
55
|
+
|
|
56
|
+
"#{mask * 4} #{mask * 4} #{mask * 4} #{digits[-4..]}"
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
require "bigdecimal"
|
|
2
|
+
|
|
3
|
+
module ConcernsOnRails
|
|
4
|
+
module Support
|
|
5
|
+
# Formats an integer subunit amount (e.g. cents) as a human-readable money
|
|
6
|
+
# string. Pure and stateless; used by Models::Monetizable. Uses BigDecimal
|
|
7
|
+
# throughout so there is no binary-float rounding drift.
|
|
8
|
+
module Money
|
|
9
|
+
module_function
|
|
10
|
+
|
|
11
|
+
# format(199999) => "$1,999.99"
|
|
12
|
+
# format(-500, unit: "£") => "-£5.00"
|
|
13
|
+
# format(1234, unit: "¥", precision: 0, subunit_to_unit: 1) => "¥1,234"
|
|
14
|
+
def format(cents, options = {})
|
|
15
|
+
unit = options.fetch(:unit, "$")
|
|
16
|
+
precision = options.fetch(:precision, 2)
|
|
17
|
+
delimiter = options.fetch(:delimiter, ",")
|
|
18
|
+
separator = options.fetch(:separator, ".")
|
|
19
|
+
subunit = options.fetch(:subunit_to_unit, 100)
|
|
20
|
+
|
|
21
|
+
decimal = BigDecimal(cents.to_s) / subunit
|
|
22
|
+
# BigDecimal#round returns an Integer for precision <= 0, so re-wrap it
|
|
23
|
+
# in a BigDecimal before #to_s("F") (Integer#to_s would read "F" as a radix).
|
|
24
|
+
rounded = BigDecimal(decimal.abs.round(precision).to_s)
|
|
25
|
+
whole, _, frac = rounded.to_s("F").partition(".")
|
|
26
|
+
whole = delimit(whole, delimiter)
|
|
27
|
+
number = precision.positive? ? "#{whole}#{separator}#{frac.ljust(precision, '0')[0, precision]}" : whole
|
|
28
|
+
|
|
29
|
+
"#{'-' if decimal.negative?}#{unit}#{number}"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Insert the thousands delimiter into a non-negative integer string.
|
|
33
|
+
def delimit(integer_string, delimiter)
|
|
34
|
+
integer_string.reverse.gsub(/(\d{3})(?=\d)/, "\\1#{delimiter}").reverse
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
data/lib/concerns_on_rails.rb
CHANGED
|
@@ -13,6 +13,8 @@ require "concerns_on_rails/support/random_value"
|
|
|
13
13
|
require "concerns_on_rails/support/address_data"
|
|
14
14
|
require "concerns_on_rails/support/sequence_calculator"
|
|
15
15
|
require "concerns_on_rails/support/html_sanitizers"
|
|
16
|
+
require "concerns_on_rails/support/masker"
|
|
17
|
+
require "concerns_on_rails/support/money"
|
|
16
18
|
|
|
17
19
|
# Model concerns
|
|
18
20
|
require "concerns_on_rails/models/sluggable"
|
|
@@ -31,6 +33,8 @@ require "concerns_on_rails/models/addressable"
|
|
|
31
33
|
require "concerns_on_rails/models/sequenceable"
|
|
32
34
|
require "concerns_on_rails/models/taggable"
|
|
33
35
|
require "concerns_on_rails/models/sanitizable"
|
|
36
|
+
require "concerns_on_rails/models/maskable"
|
|
37
|
+
require "concerns_on_rails/models/monetizable"
|
|
34
38
|
|
|
35
39
|
# Controller concerns
|
|
36
40
|
require "concerns_on_rails/controllers/paginatable"
|
|
@@ -40,6 +44,7 @@ require "concerns_on_rails/controllers/respondable"
|
|
|
40
44
|
require "concerns_on_rails/controllers/error_handleable"
|
|
41
45
|
require "concerns_on_rails/controllers/includable"
|
|
42
46
|
require "concerns_on_rails/controllers/secure_headable"
|
|
47
|
+
require "concerns_on_rails/controllers/localizable"
|
|
43
48
|
|
|
44
49
|
# Backwards compatibility (top-level aliases for pre-1.6 module paths)
|
|
45
50
|
require "concerns_on_rails/legacy_aliases"
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: concerns_on_rails
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.13.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ethan Nguyen
|
|
@@ -73,6 +73,7 @@ files:
|
|
|
73
73
|
- lib/concerns_on_rails/controllers/error_handleable.rb
|
|
74
74
|
- lib/concerns_on_rails/controllers/filterable.rb
|
|
75
75
|
- lib/concerns_on_rails/controllers/includable.rb
|
|
76
|
+
- lib/concerns_on_rails/controllers/localizable.rb
|
|
76
77
|
- lib/concerns_on_rails/controllers/paginatable.rb
|
|
77
78
|
- lib/concerns_on_rails/controllers/respondable.rb
|
|
78
79
|
- lib/concerns_on_rails/controllers/secure_headable.rb
|
|
@@ -82,6 +83,8 @@ files:
|
|
|
82
83
|
- lib/concerns_on_rails/models/addressable.rb
|
|
83
84
|
- lib/concerns_on_rails/models/expirable.rb
|
|
84
85
|
- lib/concerns_on_rails/models/hashable.rb
|
|
86
|
+
- lib/concerns_on_rails/models/maskable.rb
|
|
87
|
+
- lib/concerns_on_rails/models/monetizable.rb
|
|
85
88
|
- lib/concerns_on_rails/models/normalizable.rb
|
|
86
89
|
- lib/concerns_on_rails/models/publishable.rb
|
|
87
90
|
- lib/concerns_on_rails/models/sanitizable.rb
|
|
@@ -97,6 +100,8 @@ files:
|
|
|
97
100
|
- lib/concerns_on_rails/support/address_data.rb
|
|
98
101
|
- lib/concerns_on_rails/support/column_guard.rb
|
|
99
102
|
- lib/concerns_on_rails/support/html_sanitizers.rb
|
|
103
|
+
- lib/concerns_on_rails/support/masker.rb
|
|
104
|
+
- lib/concerns_on_rails/support/money.rb
|
|
100
105
|
- lib/concerns_on_rails/support/random_value.rb
|
|
101
106
|
- lib/concerns_on_rails/support/sequence_calculator.rb
|
|
102
107
|
- lib/concerns_on_rails/version.rb
|