No release in over a year
ActiveRecordMask is a small ruby library that provides an easy way to mask read access to database attributes and associations in ActiveRecord objects.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.0
~> 13.0
~> 3.0

Runtime

 Project Readme

Ruby

ActiveRecordMask

ActiveRecordMask is a small ruby library that provides an easy way to mask read access to database attributes and associations in ActiveRecord objects.

It allows you to configure default or empty values with configuration on a per-model basis.

Simple example

some_object = SomeClass.first

some_object.title == 'Real Title'
=> false

some_object.some_associations.count == 0
=> true

some_object.title == 'some configured default'
=> true

# Allow reading of real data.
some_object.mask_down!

some_object.title == 'Real Title'
=> true

some_object.some_associations.count
=> 2342

# Prevent reading of real data.
some_object.mask_up!

some_object.title == 'Real Title'
=> false

Features

  • Whitelist attributes and associations that should be returned by default.
  • Define custom default values for attributes.
  • Toggle between showing real data or default values.
  • All configurable per model.

Installation

Add this line to your application's Gemfile:

gem 'active_record_mask'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install active_record_mask

Usage

Basic configuration

Include the ActiveRecordMask module in your ActiveRecord model and use the configure_active_record_mask block to set up the configuration.

class SomeClass < ActiveRecord::Base
  include ActiveRecordMask

  has_many :another_classes
  
  # ActiveRecordMask configuration
  configure_active_record_mask do |config|
    config.show_real_data_by_default(false)
    config.allow_attributes([:id])
    # config.allow_associations([:another_classes])
    config.revealed_defaults({ title: 'hidden' })
  end
end

Configuration options

  • show_real_data_by_default: Set to true if you want real data to be shown by default, without calling mask_down!. Default: false

  • revealed_defaults: Define custom default values for specific attributes. It accepts a attribute-name value hash. Default: {}

  • allow_attributes: Define a list of attributes that should not be protected. Default: [:id]

  • allow_associations: Define a list of associations that should not be protected. Default: []

Testing

bundle exec rspec

Contributing

Bug reports and pull requests are welcome: https://github.com/sventantau/active_record_mask

License

The gem is available as open source under the terms of the MIT License.