From 0f9065fc1843cefd9d4e7cbcf5479b230f3b5806 Mon Sep 17 00:00:00 2001 From: Drew Bragg Date: Fri, 1 Apr 2022 12:42:17 -0400 Subject: [PATCH 1/4] Add phone number type --- lib/redaction.rb | 1 + lib/redaction/types/phone.rb | 13 +++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 lib/redaction/types/phone.rb diff --git a/lib/redaction.rb b/lib/redaction.rb index 46d392f..ca02947 100644 --- a/lib/redaction.rb +++ b/lib/redaction.rb @@ -9,6 +9,7 @@ module Types autoload :Email, "redaction/types/email" autoload :Html, "redaction/types/html" autoload :Name, "redaction/types/name" + autoload :Phone, "redaction/types/phone" autoload :Text, "redaction/types/text" end diff --git a/lib/redaction/types/phone.rb b/lib/redaction/types/phone.rb new file mode 100644 index 0000000..1ae2371 --- /dev/null +++ b/lib/redaction/types/phone.rb @@ -0,0 +1,13 @@ +require "faker" + +module Redaction + module Types + class Phone < Base + def content + # Use cell_phone so no extension is added + # see: https://github.com/faker-ruby/faker/blob/master/doc/default/phone_number.md + Faker::PhoneNumber.cell_phone + end + end + end +end From ee7db971673a6877a9e6c566b9e24fcf2fa27ba8 Mon Sep 17 00:00:00 2001 From: Drew Bragg Date: Fri, 1 Apr 2022 12:42:25 -0400 Subject: [PATCH 2/4] Add tests --- test/dummy/app/models/account.rb | 5 +++++ test/dummy/db/schema.rb | 11 ++++++++++- test/fixtures/accounts.yml | 9 +++++++++ test/redaction_test.rb | 8 ++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 test/dummy/app/models/account.rb create mode 100644 test/fixtures/accounts.yml diff --git a/test/dummy/app/models/account.rb b/test/dummy/app/models/account.rb new file mode 100644 index 0000000..85a21d1 --- /dev/null +++ b/test/dummy/app/models/account.rb @@ -0,0 +1,5 @@ +class Account < ApplicationRecord + belongs_to :user + + redacts :phone_number, with: :phone +end diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb index abbf27a..de60986 100644 --- a/test/dummy/db/schema.rb +++ b/test/dummy/db/schema.rb @@ -10,7 +10,15 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_03_09_194044) do +ActiveRecord::Schema[7.0].define(version: 2022_04_01_163109) do + create_table "accounts", force: :cascade do |t| + t.integer "user_id", null: false + t.string "phone_number" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["user_id"], name: "index_accounts_on_user_id" + end + create_table "comments", force: :cascade do |t| t.integer "post_id", null: false t.integer "user_id", null: false @@ -45,6 +53,7 @@ t.datetime "updated_at", precision: nil, null: false end + add_foreign_key "accounts", "users" add_foreign_key "comments", "posts" add_foreign_key "comments", "users" add_foreign_key "posts", "users" diff --git a/test/fixtures/accounts.yml b/test/fixtures/accounts.yml new file mode 100644 index 0000000..af1d15f --- /dev/null +++ b/test/fixtures/accounts.yml @@ -0,0 +1,9 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + user: one + phone_number: (111) 111-1111 + +two: + user: two + phone_number: (111) 111-1111 diff --git a/test/redaction_test.rb b/test/redaction_test.rb index 78af675..ba518d9 100644 --- a/test/redaction_test.rb +++ b/test/redaction_test.rb @@ -194,4 +194,12 @@ class RedactionTest < ActiveSupport::TestCase assert_not_equal user.phone, "(111) 111-1111" end + + test "it generates a redacted phone number" do + account = accounts(:one) + account.redact! + + assert_not_equal "(111) 111-1111", account.phone_number + assert_match(/\d?.?\(?\d{3}\)?\s?.?\d{3}.?\d{4}/, account.phone_number) + end end From c88a10aa6332fd93ede64d887f07c022141e4a45 Mon Sep 17 00:00:00 2001 From: Drew Bragg Date: Fri, 1 Apr 2022 12:48:58 -0400 Subject: [PATCH 3/4] Correct schema --- test/dummy/db/schema.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb index de60986..e3bd18d 100644 --- a/test/dummy/db/schema.rb +++ b/test/dummy/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2022_04_01_163109) do +ActiveRecord::Schema.define(version: 2022_04_01_163109) do create_table "accounts", force: :cascade do |t| t.integer "user_id", null: false t.string "phone_number" From 127341b9a2eae4174b37655fa6a447cb016d7683 Mon Sep 17 00:00:00 2001 From: Drew Bragg Date: Fri, 1 Apr 2022 12:49:04 -0400 Subject: [PATCH 4/4] Update README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 8fb46eb..98695b7 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ end | `:email` | A safe (will not send) email address | | `:html` | Multiple HTML Paragraphs with a random amount of link tags, `strong` tags, and `em` tags | | `:name` | A person first/last name | +| `:phone` | A phone number | | `:text` | Multiple paragraphs | To use a built in redactor type set the `with:` option of a `redacts` call to the appropriate symbol.