Skip to content

Commit

Permalink
Merge pull request #13 from DRBragg/redactor_types/add_phone_number_type
Browse files Browse the repository at this point in the history
  • Loading branch information
DRBragg authored Apr 1, 2022
2 parents ae597d2 + 127341b commit d6b6481
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions lib/redaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions lib/redaction/types/phone.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions test/dummy/app/models/account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Account < ApplicationRecord
belongs_to :user

redacts :phone_number, with: :phone
end
11 changes: 10 additions & 1 deletion test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.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
Expand Down Expand Up @@ -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"
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/accounts.yml
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions test/redaction_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d6b6481

Please sign in to comment.