active_record_mask 0.1.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 +7 -0
- data/.github/workflows/ruby.yml +38 -0
- data/.gitignore +13 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +104 -0
- data/Rakefile +4 -0
- data/active_record_mask.gemspec +33 -0
- data/lib/active_record_mask/active_record_mask.rb +115 -0
- data/lib/active_record_mask/version.rb +3 -0
- data/lib/active_record_mask.rb +1 -0
- metadata +128 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 593e6c6410f460d9132dbc4af812ed92c2f8a214424a6c331155530f9e0ef4d0
|
4
|
+
data.tar.gz: f0731cf80aeb377786fbad0eea4e7f6642825470da78d7e77edbacc7ab4024a6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 74eb58fd54d849f6190a79be682200931d5b9ba5612d448e368dad89505b1ce34b0da231345b50f540bb405a01578f5f0cc916759ff9ecf1ced3dec93333bea5
|
7
|
+
data.tar.gz: ac4f6ad4e9ad758866507b6ae97e30b61a3a4c06282fbd81869bd38e2d6b1a0e34c8e37dd6871b17684327d785d599004564c84c90cbb9df069de3866792b07e
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# This workflow uses actions that are not certified by GitHub.
|
2
|
+
# They are provided by a third-party and are governed by
|
3
|
+
# separate terms of service, privacy policy, and support
|
4
|
+
# documentation.
|
5
|
+
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
|
6
|
+
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
|
7
|
+
|
8
|
+
name: Ruby
|
9
|
+
|
10
|
+
on:
|
11
|
+
push:
|
12
|
+
branches: [ "main" ]
|
13
|
+
pull_request:
|
14
|
+
branches: [ "main" ]
|
15
|
+
|
16
|
+
permissions:
|
17
|
+
contents: read
|
18
|
+
|
19
|
+
jobs:
|
20
|
+
test:
|
21
|
+
|
22
|
+
runs-on: ubuntu-latest
|
23
|
+
strategy:
|
24
|
+
matrix:
|
25
|
+
ruby-version: ['2.6', '2.7', '3.0']
|
26
|
+
|
27
|
+
steps:
|
28
|
+
- uses: actions/checkout@v3
|
29
|
+
- name: Set up Ruby
|
30
|
+
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
|
31
|
+
# change this to (see https://github.com/ruby/setup-ruby#versioning):
|
32
|
+
# uses: ruby/setup-ruby@v1
|
33
|
+
uses: ruby/setup-ruby@55283cc23133118229fd3f97f9336ee23a179fcf # v1.146.0
|
34
|
+
with:
|
35
|
+
ruby-version: ${{ matrix.ruby-version }}
|
36
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
37
|
+
- name: Run tests
|
38
|
+
run: bundle exec rake
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2023 Sven Tantau
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
# ActiveRecordMask
|
2
|
+
|
3
|
+
ActiveRecordMask is a small ruby library that provides an easy way to mask read access to database attributes and associations in ActiveRecord objects.
|
4
|
+
|
5
|
+
It allows you to configure default or empty values with configuration on a per-model basis.
|
6
|
+
|
7
|
+
## Simple example
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
some_object = SomeClass.first
|
11
|
+
|
12
|
+
some_object.title == 'Real Title'
|
13
|
+
=> false
|
14
|
+
|
15
|
+
some_object.title == 'some configured default'
|
16
|
+
=> true
|
17
|
+
|
18
|
+
# Allow reading of real data.
|
19
|
+
some_object.mask_down!
|
20
|
+
|
21
|
+
some_object.title == 'Real Title'
|
22
|
+
=> true
|
23
|
+
|
24
|
+
# Prevent reading of real data.
|
25
|
+
some_object.mask_up!
|
26
|
+
|
27
|
+
some_object.title == 'Real Title'
|
28
|
+
=> false
|
29
|
+
|
30
|
+
```
|
31
|
+
|
32
|
+
## Features
|
33
|
+
|
34
|
+
- Whitelist attributes and associations that should be returned by default.
|
35
|
+
- Define custom default values for attributes.
|
36
|
+
- Toggle between showing real data or default values.
|
37
|
+
- All configurable per model.
|
38
|
+
|
39
|
+
## Installation
|
40
|
+
|
41
|
+
Add this line to your application's Gemfile:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
gem 'active_record_mask'
|
45
|
+
```
|
46
|
+
|
47
|
+
And then execute:
|
48
|
+
|
49
|
+
```bash
|
50
|
+
$ bundle install
|
51
|
+
```
|
52
|
+
|
53
|
+
Or install it yourself as:
|
54
|
+
|
55
|
+
```bash
|
56
|
+
$ gem install active_record_mask
|
57
|
+
```
|
58
|
+
|
59
|
+
## Usage
|
60
|
+
|
61
|
+
### Basic configuration
|
62
|
+
|
63
|
+
Include the `ActiveRecordMask` module in your ActiveRecord model and use the `configure_active_record_mask` block to set up the configuration.
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
class SomeClass < ActiveRecord::Base
|
67
|
+
include ActiveRecordMask
|
68
|
+
|
69
|
+
has_many :another_classes
|
70
|
+
|
71
|
+
# ActiveRecordMask configuration
|
72
|
+
configure_active_record_mask do |config|
|
73
|
+
config.show_real_data_by_default(false)
|
74
|
+
config.allow_attributes([:id])
|
75
|
+
# config.allow_associations([:another_classes])
|
76
|
+
config.revealed_defaults({ title: 'hidden' })
|
77
|
+
end
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
### Configuration options
|
84
|
+
|
85
|
+
- `show_real_data_by_default`: Set to `true` if you want real data to be shown by default, without calling `mask_down!`. Default: `false`
|
86
|
+
|
87
|
+
- `revealed_defaults`: Define custom default values for specific attributes. It accepts a attribute-name value hash. Default: `{}`
|
88
|
+
|
89
|
+
- `allow_attributes`: Define a list of attributes that should not be protected. Default: `[:id]`
|
90
|
+
|
91
|
+
- `allow_associations`: Define a list of associations that should not be protected. Default: `[]`
|
92
|
+
|
93
|
+
## Testing
|
94
|
+
```ruby
|
95
|
+
bundle exec rspec
|
96
|
+
```
|
97
|
+
|
98
|
+
## Contributing
|
99
|
+
|
100
|
+
Bug reports and pull requests are welcome: [https://github.com/sventantau/active_record_mask](https://github.com/sventantau/active_record_mask)
|
101
|
+
|
102
|
+
## License
|
103
|
+
|
104
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path("lib", __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require "active_record_mask/version"
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "active_record_mask"
|
9
|
+
spec.version = ActiveRecordMask::VERSION
|
10
|
+
spec.authors = ["Sven Tantau"]
|
11
|
+
spec.email = ["sven@beastiebytes.com"]
|
12
|
+
|
13
|
+
spec.summary = %q{A gem that provides a configurable mechanism to mask read access to Active Record attributes and associations}
|
14
|
+
spec.description = %q{ActiveRecordMask is a small ruby library that provides an easy way to mask read access to database attributes and associations in ActiveRecord objects.}
|
15
|
+
spec.homepage = "https://github.com/sventantau/active_record_mask"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
19
|
+
spec.metadata["source_code_uri"] = "https://github.com/sventantau/active_record_mask"
|
20
|
+
spec.metadata["bug_tracker_uri"] = "https://github.com/sventantau/active_record_mask/issues"
|
21
|
+
|
22
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
|
26
|
+
|
27
|
+
spec.add_dependency "activerecord", "~> 7"
|
28
|
+
spec.add_dependency "activesupport", "~> 7"
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
31
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
32
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
33
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
module ActiveRecordMask
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
module ClassMethods
|
5
|
+
def configure_active_record_mask
|
6
|
+
yield self if block_given?
|
7
|
+
@allowed_attributes ||= [:id]
|
8
|
+
@allowed_associations ||= []
|
9
|
+
end
|
10
|
+
|
11
|
+
def revealed_defaults(value = nil)
|
12
|
+
@revealed_defaults = value if value
|
13
|
+
@revealed_defaults ||= {}
|
14
|
+
end
|
15
|
+
|
16
|
+
def allow_attributes(value = nil)
|
17
|
+
@allowed_attributes = value if value
|
18
|
+
end
|
19
|
+
|
20
|
+
def allow_associations(value = nil)
|
21
|
+
@allowed_associations = value if value
|
22
|
+
end
|
23
|
+
|
24
|
+
def show_real_data_by_default(value = nil)
|
25
|
+
@show_real_data_by_default = value if value
|
26
|
+
@show_real_data_by_default ||= false
|
27
|
+
end
|
28
|
+
|
29
|
+
attr_reader :allowed_attributes, :allowed_associations
|
30
|
+
end
|
31
|
+
|
32
|
+
included do
|
33
|
+
configure_active_record_mask {}
|
34
|
+
after_initialize :initialize_protector
|
35
|
+
prepend InstanceMethods
|
36
|
+
end
|
37
|
+
|
38
|
+
def mask_down!
|
39
|
+
@show_real_data = true
|
40
|
+
end
|
41
|
+
|
42
|
+
def mask_up!
|
43
|
+
@show_real_data = false
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def initialize_protector
|
49
|
+
@show_real_data = self.class.show_real_data_by_default
|
50
|
+
|
51
|
+
self.class.reflect_on_all_associations.each do |reflection|
|
52
|
+
association = reflection.name
|
53
|
+
|
54
|
+
define_singleton_method(association) do
|
55
|
+
if @show_real_data || self.class.allowed_associations.include?(association.to_sym)
|
56
|
+
super()
|
57
|
+
else
|
58
|
+
reflection.klass.none
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
if reflection.macro == :has_many || reflection.macro == :has_and_belongs_to_many
|
63
|
+
define_singleton_method("#{association.to_s.singularize}_ids") do
|
64
|
+
if @show_real_data || self.class.allowed_associations.include?(association.to_sym)
|
65
|
+
super()
|
66
|
+
else
|
67
|
+
[]
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
module InstanceMethods
|
75
|
+
def _read_attribute(attr_name, &block)
|
76
|
+
return super(attr_name, &block) if @show_real_data
|
77
|
+
|
78
|
+
if self.class.allowed_attributes.include?(attr_name.to_sym) || self.class.allowed_attributes.empty?
|
79
|
+
return super(attr_name, &block)
|
80
|
+
end
|
81
|
+
|
82
|
+
empty_value_for_attribute(attr_name)
|
83
|
+
end
|
84
|
+
|
85
|
+
def empty_value_for_attribute(attr_name)
|
86
|
+
column = self.class.columns_hash[attr_name.to_s]
|
87
|
+
return nil unless column
|
88
|
+
|
89
|
+
if self.class.revealed_defaults.key?(attr_name.to_sym)
|
90
|
+
return self.class.revealed_defaults[attr_name.to_sym]
|
91
|
+
end
|
92
|
+
|
93
|
+
case column.type
|
94
|
+
when :string, :text
|
95
|
+
if (column.try(:array) rescue false)
|
96
|
+
[]
|
97
|
+
else
|
98
|
+
""
|
99
|
+
end
|
100
|
+
when :integer, :float, :decimal
|
101
|
+
0
|
102
|
+
when :datetime, :timestamp, :time, :date
|
103
|
+
nil
|
104
|
+
when :boolean
|
105
|
+
false
|
106
|
+
when :json, :jsonb, :hstore
|
107
|
+
{}
|
108
|
+
when :array
|
109
|
+
[]
|
110
|
+
else
|
111
|
+
nil
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'active_record_mask/active_record_mask'
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_record_mask
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sven Tantau
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-04-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '7'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '7'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '13.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '13.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
description: ActiveRecordMask is a small ruby library that provides an easy way to
|
84
|
+
mask read access to database attributes and associations in ActiveRecord objects.
|
85
|
+
email:
|
86
|
+
- sven@beastiebytes.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".github/workflows/ruby.yml"
|
92
|
+
- ".gitignore"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- active_record_mask.gemspec
|
98
|
+
- lib/active_record_mask.rb
|
99
|
+
- lib/active_record_mask/active_record_mask.rb
|
100
|
+
- lib/active_record_mask/version.rb
|
101
|
+
homepage: https://github.com/sventantau/active_record_mask
|
102
|
+
licenses:
|
103
|
+
- MIT
|
104
|
+
metadata:
|
105
|
+
homepage_uri: https://github.com/sventantau/active_record_mask
|
106
|
+
source_code_uri: https://github.com/sventantau/active_record_mask
|
107
|
+
bug_tracker_uri: https://github.com/sventantau/active_record_mask/issues
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: 2.4.0
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubygems_version: 3.2.3
|
124
|
+
signing_key:
|
125
|
+
specification_version: 4
|
126
|
+
summary: A gem that provides a configurable mechanism to mask read access to Active
|
127
|
+
Record attributes and associations
|
128
|
+
test_files: []
|