Skip to content

Commit

Permalink
Merge pull request #5 from keshavbiswa/readme
Browse files Browse the repository at this point in the history
Added readme
  • Loading branch information
keshavbiswa authored Mar 6, 2024
2 parents 266636d + 1e95e48 commit e143343
Showing 1 changed file with 121 additions and 13 deletions.
134 changes: 121 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,142 @@
# ActiveRecord::Anonymizer
# ActiveRecordAnonymizer

TODO: Delete this and the text below, and describe your gem
Anonymize your ActiveRecord models with ease :sunglasses:

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/active_record_anonymizer`. To experiment with that code, run `bin/console` for an interactive prompt.
`ActiveRecordAnonymizer` uses `faker` to anonymize your ActiveRecord model's attributes without the need to write custom anonymization logic for each model.

Using `ActiveRecordAnonymizer`, you can:
- Anonymize specific attributes of your model (uses `faker` under the hood)
- Provide custom anonymization logic for specific attributes
- Provide custom anonymized columns for each attributes
- Encrypt anonymized data using `ActiveRecord::Encryption` (Requires Rails 7+)
- Environment dependent, so you can decide whether you want to view original data in development and anonymized data in production

## Installation

TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
Add this line to your application's Gemfile:

```ruby
gem 'active_record_anonymizer'
```

And then execute:

$ bundle install

Or install it yourself as:

$ gem install active_record_anonymizer

Install the gem using the following command:

$ bin/rails generate active_record_anonymizer:install

Install the gem and add to the application's Gemfile by executing:
You must have anonymized columns in your Model to store the anonymized data.
You can use the following migration generator to add anonymized columns to your existing table:

$ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
$ bin/rails generate anonymize User first_name last_name
This will generate a migration file similar to the following:

If bundler is not being used to manage dependencies, install the gem by executing:
```ruby
class AddAnonymizedColumnsToUser < ActiveRecord::Migration[6.0]
def change
add_column :users, :anonymized_first_name, :string
add_column :users, :anonymized_last_name, :string
end
end
```

$ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
Add the following line to your model to enable anonymization:

```ruby
class User < ApplicationRecord
# There are other options available, please refer to the Usage section
anonymize :first_name, :last_name
end
```
To populate the anonymized columns, run the following command:

$ bin/rails anonymize:populate CLASS=User

## Usage

TODO: Write usage instructions here
Attributes can be anonymized using the `anonymize` method. The `anonymize` method takes the following options:

## Development
- `:column_name` - The name of the column to store the anonymized data. ("anonymized_#{column_name}" by default)
- `:with` - The custom logic to anonymize the attribute. (Optional, uses `faker` by default)
- `:encrypt` - Encrypt the anonymized data using `ActiveRecord::Encryption` (Requires Rails 7+)

```ruby
class User < ApplicationRecord
anonymize :first_name, :last_name
anonymize :email, with: ->(email) { Faker::Internet.email }
anonymize :age, with: ->(age) { age + 5 }
anonymize :phone, column_name: :fake_phone_number, with: ->(phone) { phone.gsub(/\d/, 'X') }
anonymize :address, encrypt: true
end
```

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
## Configuration

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
You can configure the gem using the following options:

- `:environments` - The environments in which the anonymized data should be used. (Defaults to `[:staging]`)
- `:skip_update` - Skip updating the anonymized data when the record is updated. This ensures your anonymized data remains the same even if it's updated. (Defaults to `false`)
- `:alias_original_columns` - Alias the original columns to the anonymized columns. You can still access the original value of the attribute using the alias `original_#{attribute_name}`(Defaults to `false`)
- `:alias_column_name` - The name of the alias column. (Defaults to `original_#{column_name}`)

```ruby
ActiveRecordAnonymizer.configure do |config|
config.environments = [:staging, :production] # The environments in which the anonymized data should be used
config.skip_update = true # Skip updating the anonymized data when the record is updated
config.alias_original_columns = true # Alias the original columns to the anonymized columns
config.alias_column_name = "original" # The original column will be aliased to "original_#{column_name}
end
```

## Development

After checking out the repo, run `bin/setup` to install dependencies.
Then, run `rake test` to run the tests.
You can also run `bin/console` for an interactive prompt that will allow you to experiment.

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/active_record_anonymizer.
Bug reports are welcome on GitHub at https://github.com/keshavbiswa/active_record_anonymizer/issues.

### Getting Started
- Fork the Repository: Start by forking this [repo](https://github.com/keshavbiswa/active_record_anonymizer.git) on GitHub.

- Set Up Your Local Environment: Navigate into the project directory and run the setup script to install dependencies:

```shell
$ cd active_record_anonymizer
$ bin/setup
```
- Create a New Branch: Before making any changes, create a new branch to keep your work organized:

```shell
$ git checkout -b my-new-feature
```

- Make Your Changes:
- Implement your changes or fixes in your local repository.
- Be sure to keep your changes as focused as possible.
- If you're working on multiple unrelated improvements, consider making separate branches and pull requests for each.

- Write Tests: If you're adding a new feature or fixing a bug, please add or update the corresponding tests.

- Run Tests: Before submitting your changes, run the test suite to ensure everything is working correctly:
```shell
$ bin/rake test
```

- Update Documentation: If your changes involve user-facing features or APIs, update the README or other relevant documentation accordingly.

- Submit a Pull Request: Go to the original `ActiveRecordAnonymizer` repository on GitHub, and you'll see a prompt to submit a pull request from your new branch.


Bug reports and pull requests are welcome on GitHub at https://github.com/keshavbiswa/active_record_anonymizer.

## License

Expand Down

0 comments on commit e143343

Please sign in to comment.