validates_unchangeable 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: 12c950dd86c8a0f40865568800414af94ed6a4b09d0d38f422f4586640512156
4
+ data.tar.gz: fc6eaf7ac254fef11f412176c1b8582473bdec79d7f203dc8d990f591875e93b
5
+ SHA512:
6
+ metadata.gz: 760409153dd7f2b49d46d0d26dfb4fe9ccd6697c3fb0c00705d9851ffba4a8c4c27f2dca5ece5fbb0bb771e1fb401dc8bf1eef736728b404fc13a4fa0748bb7d
7
+ data.tar.gz: 18eef940a95472ea9fa802385d91060b1b11dae3ab06b44e2bd6eeccaf89990286ec6aa38d9076c39453095eb4bfdacda787ab4d2f5a2cdc9fa53038edd45186
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .idea
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ .ruby-gemset
14
+ .ruby-version
15
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+ NewCops: enable
4
+ Style/Documentation:
5
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in validates_unchangeable.gemspec
8
+ gemspec
@@ -0,0 +1,80 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ validates_unchangeable (0.1.0)
5
+ activemodel (>= 5.0, < 7.0)
6
+ i18n
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (6.0.3.4)
12
+ activesupport (= 6.0.3.4)
13
+ activerecord (6.0.3.4)
14
+ activemodel (= 6.0.3.4)
15
+ activesupport (= 6.0.3.4)
16
+ activesupport (6.0.3.4)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 0.7, < 2)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ zeitwerk (~> 2.2, >= 2.2.2)
22
+ ast (2.4.1)
23
+ concurrent-ruby (1.1.7)
24
+ diff-lcs (1.4.4)
25
+ i18n (1.8.5)
26
+ concurrent-ruby (~> 1.0)
27
+ minitest (5.14.2)
28
+ parallel (1.19.2)
29
+ parser (2.7.2.0)
30
+ ast (~> 2.4.1)
31
+ rainbow (3.0.0)
32
+ rake (10.5.0)
33
+ regexp_parser (1.8.1)
34
+ rexml (3.2.4)
35
+ rspec (3.9.0)
36
+ rspec-core (~> 3.9.0)
37
+ rspec-expectations (~> 3.9.0)
38
+ rspec-mocks (~> 3.9.0)
39
+ rspec-core (3.9.3)
40
+ rspec-support (~> 3.9.3)
41
+ rspec-expectations (3.9.2)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.9.0)
44
+ rspec-mocks (3.9.1)
45
+ diff-lcs (>= 1.2.0, < 2.0)
46
+ rspec-support (~> 3.9.0)
47
+ rspec-support (3.9.3)
48
+ rubocop (0.93.0)
49
+ parallel (~> 1.10)
50
+ parser (>= 2.7.1.5)
51
+ rainbow (>= 2.2.2, < 4.0)
52
+ regexp_parser (>= 1.8)
53
+ rexml
54
+ rubocop-ast (>= 0.6.0)
55
+ ruby-progressbar (~> 1.7)
56
+ unicode-display_width (>= 1.4.0, < 2.0)
57
+ rubocop-ast (0.7.1)
58
+ parser (>= 2.7.1.5)
59
+ ruby-progressbar (1.10.1)
60
+ sqlite3 (1.4.2)
61
+ thread_safe (0.3.6)
62
+ tzinfo (1.2.7)
63
+ thread_safe (~> 0.1)
64
+ unicode-display_width (1.7.0)
65
+ zeitwerk (2.4.0)
66
+
67
+ PLATFORMS
68
+ ruby
69
+
70
+ DEPENDENCIES
71
+ activerecord (>= 5.0, < 7.0)
72
+ bundler (~> 1.17)
73
+ rake (~> 10.0)
74
+ rspec (~> 3.0)
75
+ rubocop (> 0.90)
76
+ sqlite3 (> 0)
77
+ validates_unchangeable!
78
+
79
+ BUNDLED WITH
80
+ 1.17.3
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Vital Ryabchinskiy
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,86 @@
1
+ # Validates Unchangeable
2
+
3
+ This gem adds the capability of validating unchangeable attributes to ActiveRecord and ActiveModel.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'validates_unchangeable'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install validates_unchangeable
20
+
21
+ ## Usage
22
+
23
+ ### With ActiveRecord
24
+
25
+ ```ruby
26
+ class User < ActiveRecord::Base
27
+ validates :email, unchangeable: true
28
+ end
29
+ ```
30
+
31
+ ### With ActiveModel
32
+ ```ruby
33
+ class User
34
+ include ActiveModel::Dirty
35
+ include ActiveModel::Validations
36
+
37
+ define_attribute_methods :email
38
+ attr_reader :email
39
+
40
+ validates :email, unchangeable: true
41
+
42
+ def self.create(attributes = {})
43
+ new(attributes).tap(&:save)
44
+ end
45
+
46
+ def initialize(attributes = {})
47
+ @email = attributes[:email]
48
+ end
49
+
50
+ def value=(val)
51
+ value_will_change! unless val == @email
52
+ @email = val
53
+ end
54
+
55
+ def save
56
+ changes_applied
57
+ end
58
+ end
59
+ ````
60
+
61
+ ### With RSpec
62
+ Require the matcher in `spec_helper.rb` or `rails_helper.rb`:
63
+
64
+ ```ruby
65
+ require 'validates_unchangeable/rspec_matcher'
66
+ ```
67
+
68
+ In your spec:
69
+
70
+ ```ruby
71
+ describe User
72
+ it { is_expected.to validate_unchangeable_of(:email) }
73
+ end
74
+ ```
75
+
76
+ ### I18n
77
+ The default error message `cannot be changed`.
78
+ You can pass the `message: "my custom error"` option to your validation to define your own, custom message.
79
+
80
+ ## Contributing
81
+
82
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/validates_unchangeable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
83
+
84
+ ## License
85
+
86
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,8 @@
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
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'validates_unchangeable'
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,4 @@
1
+ en:
2
+ errors:
3
+ messages:
4
+ unchangeable: cannot be changed
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/i18n'
4
+ require 'validates_unchangeable/version'
5
+ require 'validates_unchangeable/active_model'
6
+
7
+ I18n.load_path += Dir["#{File.dirname(__FILE__)}/../config/locales/*.yml"]
8
+
9
+ module ValidatesUnchangeable
10
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_model'
4
+
5
+ module ActiveModel
6
+ module Validations
7
+ class UnchangeableValidator < ActiveModel::EachValidator
8
+ def validate_each(record, attribute, _value)
9
+ return if !record.public_send("#{attribute}_changed?") || record.public_send("#{attribute}_was").nil?
10
+
11
+ record.errors.add(attribute, :unchangeable)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec::Matchers.define :validate_unchangeable_of do |attribute|
4
+ match do
5
+ actual = subject.is_a?(Class) ? subject.new : subject
6
+ actual.public_send(:"#{attribute}=", 'qwerty')
7
+ actual.save(validate: false)
8
+ actual.public_send(:"#{attribute}=", 'ytrewq')
9
+ expect(actual).to be_invalid
10
+ @expected_message ||= I18n.t('errors.messages.unchangeable')
11
+ expect(actual.errors.messages[attribute.to_sym]).to include(@expected_message)
12
+ end
13
+ chain :with_message do |message|
14
+ @expected_message = message
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ValidatesUnchangeable
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,46 @@
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 'validates_unchangeable/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'validates_unchangeable'
9
+ spec.version = ValidatesUnchangeable::VERSION
10
+ spec.authors = ['Vital Ryabchinskiy']
11
+ spec.email = ['vital.ryabchinskiy@gmail.com']
12
+
13
+ spec.summary = 'Library for validating unchangeable attributes.'
14
+ spec.description = 'Library for validating unchangeable attributes.'
15
+ spec.homepage = 'https://github.com/vitalinfo/validates_unchangeable'
16
+ spec.license = 'MIT'
17
+
18
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
19
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
20
+ if spec.respond_to?(:metadata)
21
+ spec.metadata['homepage_uri'] = spec.homepage
22
+ else
23
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
24
+ 'public gem pushes.'
25
+ end
26
+
27
+ # Specify which files should be added to the gem when it is released.
28
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
29
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
30
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
31
+ end
32
+ spec.bindir = 'exe'
33
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
34
+ spec.require_paths = ['lib']
35
+
36
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.4')
37
+
38
+ spec.add_development_dependency 'activerecord', '>= 5.0', '< 7.0'
39
+ spec.add_development_dependency 'bundler', '~> 1.17'
40
+ spec.add_development_dependency 'rake', '~> 10.0'
41
+ spec.add_development_dependency 'rspec', '~> 3.0'
42
+ spec.add_development_dependency 'rubocop', '> 0.90'
43
+ spec.add_development_dependency 'sqlite3', '> 0'
44
+ spec.add_dependency 'activemodel', '>= 5.0', '< 7.0'
45
+ spec.add_dependency 'i18n'
46
+ end
metadata ADDED
@@ -0,0 +1,184 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validates_unchangeable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vital Ryabchinskiy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-10-10 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: '5.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '7.0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '5.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '7.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.17'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.17'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '10.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '10.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '3.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '3.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rubocop
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">"
80
+ - !ruby/object:Gem::Version
81
+ version: '0.90'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">"
87
+ - !ruby/object:Gem::Version
88
+ version: '0.90'
89
+ - !ruby/object:Gem::Dependency
90
+ name: sqlite3
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">"
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">"
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: activemodel
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '5.0'
110
+ - - "<"
111
+ - !ruby/object:Gem::Version
112
+ version: '7.0'
113
+ type: :runtime
114
+ prerelease: false
115
+ version_requirements: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '5.0'
120
+ - - "<"
121
+ - !ruby/object:Gem::Version
122
+ version: '7.0'
123
+ - !ruby/object:Gem::Dependency
124
+ name: i18n
125
+ requirement: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ type: :runtime
131
+ prerelease: false
132
+ version_requirements: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ description: Library for validating unchangeable attributes.
138
+ email:
139
+ - vital.ryabchinskiy@gmail.com
140
+ executables: []
141
+ extensions: []
142
+ extra_rdoc_files: []
143
+ files:
144
+ - ".gitignore"
145
+ - ".rspec"
146
+ - ".rubocop.yml"
147
+ - Gemfile
148
+ - Gemfile.lock
149
+ - LICENSE.txt
150
+ - README.md
151
+ - Rakefile
152
+ - bin/console
153
+ - bin/setup
154
+ - config/locales/en.yml
155
+ - lib/validates_unchangeable.rb
156
+ - lib/validates_unchangeable/active_model.rb
157
+ - lib/validates_unchangeable/rspec_matcher.rb
158
+ - lib/validates_unchangeable/version.rb
159
+ - validates_unchangeable.gemspec
160
+ homepage: https://github.com/vitalinfo/validates_unchangeable
161
+ licenses:
162
+ - MIT
163
+ metadata:
164
+ homepage_uri: https://github.com/vitalinfo/validates_unchangeable
165
+ post_install_message:
166
+ rdoc_options: []
167
+ require_paths:
168
+ - lib
169
+ required_ruby_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '2.4'
174
+ required_rubygems_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ requirements: []
180
+ rubygems_version: 3.0.8
181
+ signing_key:
182
+ specification_version: 4
183
+ summary: Library for validating unchangeable attributes.
184
+ test_files: []