activerecord_readonly 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0f18170673ea01466f4f50f217c9f81fb59155abf3664a6ec5f78a5ddd0d9bd3
4
+ data.tar.gz: cf7e4d12acb5a2b4f083dca7d56e481b9de102c264f90cf9f683e9aaef3659d5
5
+ SHA512:
6
+ metadata.gz: e48adbfd6d3952d09cab0a53fa7d80296b55b6da666a069fe6e94e7493d187251b0bb6e33afd8637de8a0606507cf48ec846398ccbfbc69d857d2fd77ac2d5d9
7
+ data.tar.gz: 1c6cb1ea24bef98cae6f5218018339d02379ea9584a967ca676c9a75766695bfab8362437f0bdacd6ec9482990bb24bc339e6299cabe839726be6ca0d3828e2e
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,13 @@
1
+ Style/StringLiterals:
2
+ Enabled: true
3
+ EnforcedStyle: double_quotes
4
+
5
+ Style/StringLiteralsInInterpolation:
6
+ Enabled: true
7
+ EnforcedStyle: double_quotes
8
+
9
+ Layout/LineLength:
10
+ Max: 120
11
+
12
+ Documentation:
13
+ Enabled: false
@@ -0,0 +1,82 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+
17
+ Examples of unacceptable behavior include:
18
+
19
+ * The use of sexualized language or imagery, and sexual attention or
20
+ advances of any kind
21
+ * Trolling, insulting or derogatory comments, and personal or political attacks
22
+ * Public or private harassment
23
+ * Publishing others' private information, such as a physical or email
24
+ address, without their explicit permission
25
+ * Other conduct which could reasonably be considered inappropriate in a
26
+ professional setting
27
+
28
+ ## Enforcement Responsibilities
29
+
30
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
31
+
32
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
33
+
34
+ ## Scope
35
+
36
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
37
+
38
+ ## Enforcement
39
+
40
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at example@example.com. All complaints will be reviewed and investigated promptly and fairly.
41
+
42
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
43
+
44
+ ## Enforcement Guidelines
45
+
46
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
47
+
48
+ ### 1. Correction
49
+
50
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
51
+
52
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
53
+
54
+ ### 2. Warning
55
+
56
+ **Community Impact**: A violation through a single incident or series of actions.
57
+
58
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
59
+
60
+ ### 3. Temporary Ban
61
+
62
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
63
+
64
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
65
+
66
+ ### 4. Permanent Ban
67
+
68
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
69
+
70
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
71
+
72
+ ## Attribution
73
+
74
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
75
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
76
+
77
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
78
+
79
+ [homepage]: https://www.contributor-covenant.org
80
+
81
+ For answers to common questions about this code of conduct, see the FAQ at
82
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in activerecord_readonly.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 0.80"
@@ -0,0 +1,79 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ activerecord_readonly (0.1.0)
5
+ activerecord (>= 6)
6
+ activesupport
7
+ request_store (>= 1.0.5)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activemodel (6.1.0)
13
+ activesupport (= 6.1.0)
14
+ activerecord (6.1.0)
15
+ activemodel (= 6.1.0)
16
+ activesupport (= 6.1.0)
17
+ activesupport (6.1.0)
18
+ concurrent-ruby (~> 1.0, >= 1.0.2)
19
+ i18n (>= 1.6, < 2)
20
+ minitest (>= 5.1)
21
+ tzinfo (~> 2.0)
22
+ zeitwerk (~> 2.3)
23
+ ast (2.4.1)
24
+ concurrent-ruby (1.1.7)
25
+ diff-lcs (1.4.4)
26
+ i18n (1.8.5)
27
+ concurrent-ruby (~> 1.0)
28
+ minitest (5.14.2)
29
+ parallel (1.20.1)
30
+ parser (2.7.2.0)
31
+ ast (~> 2.4.1)
32
+ rack (2.2.3)
33
+ rainbow (3.0.0)
34
+ rake (13.0.1)
35
+ regexp_parser (2.0.0)
36
+ request_store (1.5.0)
37
+ rack (>= 1.4)
38
+ rexml (3.2.4)
39
+ rspec (3.10.0)
40
+ rspec-core (~> 3.10.0)
41
+ rspec-expectations (~> 3.10.0)
42
+ rspec-mocks (~> 3.10.0)
43
+ rspec-core (3.10.0)
44
+ rspec-support (~> 3.10.0)
45
+ rspec-expectations (3.10.0)
46
+ diff-lcs (>= 1.2.0, < 2.0)
47
+ rspec-support (~> 3.10.0)
48
+ rspec-mocks (3.10.0)
49
+ diff-lcs (>= 1.2.0, < 2.0)
50
+ rspec-support (~> 3.10.0)
51
+ rspec-support (3.10.0)
52
+ rubocop (0.93.1)
53
+ parallel (~> 1.10)
54
+ parser (>= 2.7.1.5)
55
+ rainbow (>= 2.2.2, < 4.0)
56
+ regexp_parser (>= 1.8)
57
+ rexml
58
+ rubocop-ast (>= 0.6.0)
59
+ ruby-progressbar (~> 1.7)
60
+ unicode-display_width (>= 1.4.0, < 2.0)
61
+ rubocop-ast (1.3.0)
62
+ parser (>= 2.7.1.5)
63
+ ruby-progressbar (1.10.1)
64
+ tzinfo (2.0.3)
65
+ concurrent-ruby (~> 1.0)
66
+ unicode-display_width (1.7.0)
67
+ zeitwerk (2.4.2)
68
+
69
+ PLATFORMS
70
+ x86_64-linux
71
+
72
+ DEPENDENCIES
73
+ activerecord_readonly!
74
+ rake (~> 13.0)
75
+ rspec (~> 3.0)
76
+ rubocop (~> 0.80)
77
+
78
+ BUNDLED WITH
79
+ 2.2.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 h1kita
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
13
+ all 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
21
+ THE SOFTWARE.
@@ -0,0 +1,56 @@
1
+ # Activerecord::Readonly
2
+
3
+ WIP
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'activerecord_readonly'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install activerecord_readonly
20
+
21
+ ## Usage
22
+
23
+ ```rb
24
+ # switch readonly mode
25
+ #
26
+ # excepts
27
+ # class name to exclude
28
+ # default: []
29
+ # only
30
+ # class name to limit
31
+ # default: []
32
+ ActiverecordReadonly.switch!(current_user.readable?, excepts: ["User"], only: [])
33
+
34
+ # reset readonly mode
35
+ ActiverecordReadonly.reset_readonly!
36
+
37
+ # switch readonly mode only inside the block
38
+ ActiverecordReadonly.with(true) do
39
+ end
40
+
41
+ # reset readonly mode only inside the block
42
+ ActiverecordReadonly.with(false) do
43
+ end
44
+ ```
45
+
46
+ ## Contributing
47
+
48
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/activerecord_readonly. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/activerecord_readonly/blob/master/CODE_OF_CONDUCT.md).
49
+
50
+ ## License
51
+
52
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
53
+
54
+ ## Code of Conduct
55
+
56
+ Everyone interacting in the Activerecord::Readonly project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/activerecord_readonly/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/activerecord_readonly/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "activerecord_readonly"
7
+ spec.version = ActiverecordReadonly::VERSION
8
+ spec.authors = ["h1kita"]
9
+
10
+ spec.summary = "Control readonly for activerecord."
11
+ spec.description = "Control readonly for activerecord."
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
14
+
15
+ spec.metadata["source_code_uri"] = "https://github.com/shunhikita/activerecord_readonly"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ # Uncomment to register a new dependency of your gem
27
+ # spec.add_dependency "example-gem", "~> 1.0"
28
+ spec.add_dependency("activerecord", ">= 6")
29
+ spec.add_dependency("activesupport")
30
+ spec.add_dependency("request_store", ">= 1.0.5")
31
+
32
+ # For more information and examples about making a new gem, checkout our
33
+ # guide at: https://bundler.io/guides/creating_gem.html
34
+ end
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "activerecord_readonly"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "activerecord_readonly/version"
4
+ require_relative "activerecord_readonly/model"
5
+ require_relative "activerecord_readonly/current"
6
+ require_relative "activerecord_readonly/railtie"
7
+
8
+ module ActiverecordReadonly
9
+ class Error < StandardError; end
10
+
11
+ class << self
12
+ def switch!(be_readonly = false, excepts: [], only: [])
13
+ RequestStore[:activerecord_readonly] = ActiverecordReadonly::Current.new(be_readonly,
14
+ excepts.map(&:to_s),
15
+ only.map(&:to_s))
16
+ end
17
+
18
+ def reset_readonly!
19
+ RequestStore[:activerecord_readonly] = ActiverecordReadonly::Current.new(false,
20
+ [],
21
+ [])
22
+ end
23
+
24
+ def current
25
+ RequestStore[:activerecord_readonly]
26
+ end
27
+
28
+ def readonly?
29
+ current&.be_readonly || false
30
+ end
31
+
32
+ def except_class_names
33
+ current&.excepts || []
34
+ end
35
+
36
+ def only_class_names
37
+ current&.only || []
38
+ end
39
+
40
+ def with(be_readonly)
41
+ old = readonly?
42
+
43
+ switch!(be_readonly)
44
+ yield
45
+ switch!(old)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiverecordReadonly
4
+ class Current
5
+ attr_reader :be_readonly
6
+ attr_reader :excepts
7
+ attr_reader :only
8
+
9
+ def initialize(be_readonly, excepts = [], only = [])
10
+ @be_readonly = be_readonly
11
+ @excepts = excepts
12
+ @only = only
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "activerecord_readonly"
4
+
5
+ module ActiverecordReadonly
6
+ module Model
7
+ extend ActiveSupport::Concern
8
+ READONLY_CLASS_METHODS = %i[decrement_counter delete_all increment_counter
9
+ update_all update_counters insert_all insert_all! upsert_all].freeze
10
+
11
+ READONLY_INSTANCE_METHODS = %i[decrement! delete increment! update_colummn update_columns touch].freeze
12
+
13
+ included do
14
+ before_destroy do
15
+ raise ActiveRecord::ReadOnlyRecord if self.class.readonly?
16
+ end
17
+
18
+ def readonly?
19
+ self.class.readonly?
20
+ end
21
+
22
+ READONLY_INSTANCE_METHODS.each do |m|
23
+ define_method m do |*_|
24
+ raise ActiveRecord::ReadOnlyRecord if self.class.readonly?
25
+
26
+ super
27
+ end
28
+ end
29
+ end
30
+
31
+ class_methods do
32
+ def readonly?
33
+ ActiverecordReadonly.readonly? &&
34
+ !ActiverecordReadonly.except_class_names.include?(name) &&
35
+ ActiverecordReadonly.only_class_names.include?(name)
36
+ end
37
+
38
+ READONLY_CLASS_METHODS.each do |m|
39
+ define_method m do |*_|
40
+ raise ActiveRecord::ReadOnlyRecord if readonly?
41
+
42
+ super
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "activerecord_readonly"
4
+
5
+ module ActiverecordReadonly
6
+ class Railtie < Rails::Railtie
7
+ initializer "activerecord-be_readonly.active_record" do
8
+ ActiveSupport.on_load(:active_record) do
9
+ include ActiverecordReadonly::Model
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiverecordReadonly
4
+ VERSION = "0.1.0"
5
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord_readonly
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - h1kita
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-12-18 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: '6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: request_store
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 1.0.5
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.5
55
+ description: Control readonly for activerecord.
56
+ email:
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - ".gitignore"
62
+ - ".rspec"
63
+ - ".rubocop.yml"
64
+ - CODE_OF_CONDUCT.md
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - activerecord_readonly.gemspec
71
+ - bin/console
72
+ - bin/setup
73
+ - lib/activerecord_readonly.rb
74
+ - lib/activerecord_readonly/current.rb
75
+ - lib/activerecord_readonly/model.rb
76
+ - lib/activerecord_readonly/railtie.rb
77
+ - lib/activerecord_readonly/version.rb
78
+ homepage:
79
+ licenses:
80
+ - MIT
81
+ metadata:
82
+ source_code_uri: https://github.com/shunhikita/activerecord_readonly
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: 2.4.0
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubygems_version: 3.1.4
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Control readonly for activerecord.
102
+ test_files: []