deep_dirty 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: aa08f99165db0f0e0859e69ff59e42770a386749
4
+ data.tar.gz: 7140bae29f2ab14f146d09aebfd0f1c8292f4f09
5
+ SHA512:
6
+ metadata.gz: 3aa52d60188245c4b20788e31a051f32b89b94febd9b3c9229f06adc1d25e27be1d118da7664e140fbda4969d9de804d9d1f091cd83ea152eb4d5c914a069ac1
7
+ data.tar.gz: 2fa0f5ea60ad001ad9a90d0ec152899d658944340079d52cbeb61705d850d672164c8a8115eebc1c8dce650f5e1a6bf76fedb05e016846e71f60359d105a0964
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in deep_dirty.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Laas Toom
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,56 @@
1
+ # DeepDirty
2
+
3
+ `ActiveModel::Dirty` does not track changes that are _destructive_ or _inplace_:
4
+
5
+ user = User.first
6
+ user.name.upcase!
7
+ u.changed? # => false
8
+
9
+ `DeepDirty` fixes this by adding a `before_validation` hook that compares all attributes to their `before_type_cast` values and records any changes.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'deep_dirty'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install deep_dirty
24
+
25
+ ## Usage
26
+
27
+ In an `ActiveModel` include this module:
28
+
29
+ class User < ActiveRecord::Base
30
+ include DeepDirty
31
+ end
32
+
33
+ user = User.first
34
+ user.name.upcase!
35
+ user.valid?
36
+ user.changed? # => true
37
+ user.changes # => {"name" => ["Test User", "TEST USER"]}
38
+
39
+
40
+ Alternatively if you need to check changes without calling validation, use `deep_changed?`:
41
+
42
+ user = User.first
43
+ user.name.upcase!
44
+ user.changed? # => false
45
+ user.deep_changed? # => true
46
+ user.changed? # => true
47
+ user.changes # => {"name" => ["Test User", "TEST USER"]}
48
+
49
+
50
+ ## Contributing
51
+
52
+ 1. Fork it ( https://github.com/borgand/deep_dirty/fork )
53
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
54
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
55
+ 4. Push to the branch (`git push origin my-new-feature`)
56
+ 5. Create a new Pull Request
@@ -0,0 +1,11 @@
1
+ require 'rspec/core/rake_task'
2
+ require "bundler/gem_tasks"
3
+
4
+
5
+ # Default directory to look in is `/specs`
6
+ # Run with `rake spec`
7
+ RSpec::Core::RakeTask.new(:spec) do |task|
8
+ task.rspec_opts = ['--color', '--format', 'doc']
9
+ end
10
+
11
+ task :default => :spec
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'deep_dirty/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "deep_dirty"
8
+ spec.version = DeepDirty::VERSION
9
+ spec.authors = ["Laas Toom"]
10
+ spec.email = ["laas.toom@gmail.com"]
11
+ spec.summary = %q{Dirty tracking that supports implicit destructive changes}
12
+ spec.description = %q{Sets up `before_validation` callback that compares every attribute to it's before
13
+ type cast value and marks all changes detected.}
14
+ spec.homepage = "https://github.com/borgand/deep_dirty"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.6"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+
26
+ spec.add_runtime_dependency "activemodel", ">= 3.0.0"
27
+ end
@@ -0,0 +1,30 @@
1
+ require "deep_dirty/version"
2
+
3
+ module DeepDirty
4
+ def self.included(base)
5
+ base.class_eval do
6
+ before_validation :deep_dirty_check
7
+ end
8
+ end
9
+
10
+ # apply deep dirty check and then return `changed?`
11
+ def deep_changed?
12
+ deep_dirty_check
13
+ changed?
14
+ end
15
+
16
+ # Compare each attribute to it's value before type cast and mark changes where detected
17
+ def deep_dirty_check
18
+ self.attributes.keys.each do |attr|
19
+ next if attribute_changed?(attr)
20
+ col = column_for_attribute(attr)
21
+ uncast_value = read_attribute_before_type_cast(attr)
22
+ recast_value = col.type_cast(uncast_value)
23
+ unless recast_value == read_attribute(attr)
24
+ changed_attributes[attr] = recast_value
25
+ end
26
+ end
27
+ # This should never break validation
28
+ true
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module DeepDirty
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+
3
+ describe DeepDirty do
4
+ subject do
5
+ TestSubject.new
6
+ end
7
+
8
+ context "Without changes" do
9
+ it "does not show changes" do
10
+ expect(subject).not_to be_changed
11
+ end
12
+ end
13
+
14
+ context "With explicit changes" do
15
+ it 'reports model to be changed' do
16
+ subject.name = 'new name'
17
+ expect(subject).to be_changed
18
+ end
19
+
20
+ it 'reports attribute as changed' do
21
+ subject.name = 'new name'
22
+ expect(subject).to be_name_changed
23
+ end
24
+
25
+ it 'returns new value for attribute' do
26
+ subject.name = 'New Name'
27
+ expect(subject.name).to eq('New Name')
28
+ end
29
+
30
+ it 'returns old value for attribute' do
31
+ subject.name = 'New Name'
32
+ expect(subject.name_was).to eq('Test User')
33
+ end
34
+ end
35
+
36
+ context "With implicit changes" do
37
+ it 'reports model to be deep_changed' do
38
+ subject.name.upcase!
39
+ expect(subject).to be_deep_changed
40
+ end
41
+
42
+ it 'reports model to be changed after validation' do
43
+ subject.name.upcase!
44
+ # DeepDirty hooks into before_validation
45
+ subject.validation
46
+ expect(subject).to be_changed
47
+ end
48
+
49
+ it 'reports attribute as changed after validation' do
50
+ subject.name.upcase!
51
+ subject.validation
52
+ expect(subject).to be_name_changed
53
+ end
54
+
55
+ it 'returns new value for attribute after validation' do
56
+ subject.name.upcase!
57
+ subject.validation
58
+ expect(subject.name).to eq('TEST USER')
59
+ end
60
+
61
+ it 'returns old value for attribute after validation' do
62
+ subject.name.upcase!
63
+ subject.validation
64
+ expect(subject.name_was).to eq('Test User')
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,3 @@
1
+ require 'active_model'
2
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'deep_dirty')
3
+ require File.join(File.dirname(__FILE__), 'test_subject')
@@ -0,0 +1,52 @@
1
+ class TestSubject
2
+ # These must be run before we can use callbacks
3
+ extend ActiveModel::Callbacks
4
+ define_model_callbacks :validation, :only => :before
5
+
6
+ include ActiveModel::Dirty
7
+ include DeepDirty
8
+
9
+ attr_accessor :name
10
+ define_attribute_methods :name
11
+
12
+ def initialize
13
+ @name = "Test User"
14
+ @name_before_type_cast = "Test User"
15
+ end
16
+
17
+ def attributes
18
+ {'name' => @name}
19
+ end
20
+
21
+ def name=(val)
22
+ name_will_change! unless val == @name
23
+ @name = val
24
+ end
25
+
26
+ def validation
27
+ run_callbacks :validation
28
+ end
29
+
30
+ def read_attribute_before_type_cast(attr)
31
+ case attr
32
+ when :name, "name"
33
+ @name_before_type_cast
34
+ end
35
+ end
36
+
37
+ def read_attribute(attr)
38
+ case attr
39
+ when :name, "name"
40
+ @name
41
+ end
42
+ end
43
+
44
+ def column_for_attribute(attr)
45
+ fake_column = Object.new
46
+ type_cast_value = read_attribute_before_type_cast(attr)
47
+ fake_column.define_singleton_method(:type_cast) do |value|
48
+ type_cast_value
49
+ end
50
+ fake_column
51
+ end
52
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: deep_dirty
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Laas Toom
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activemodel
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 3.0.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 3.0.0
69
+ description: |-
70
+ Sets up `before_validation` callback that compares every attribute to it's before
71
+ type cast value and marks all changes detected.
72
+ email:
73
+ - laas.toom@gmail.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - deep_dirty.gemspec
84
+ - lib/deep_dirty.rb
85
+ - lib/deep_dirty/version.rb
86
+ - spec/deep_dirty_spec.rb
87
+ - spec/spec_helper.rb
88
+ - spec/test_subject.rb
89
+ homepage: https://github.com/borgand/deep_dirty
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.2.2
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Dirty tracking that supports implicit destructive changes
113
+ test_files:
114
+ - spec/deep_dirty_spec.rb
115
+ - spec/spec_helper.rb
116
+ - spec/test_subject.rb