activemodel-attribute_changed_specification 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ if ENV['RAILS']
6
+ gem 'rails', path: ENV['RAILS']
7
+ else
8
+ gem 'rails'
9
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Yasuharu Ozaki
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.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # activemodel-attribute_changed_specification
2
+ [![Build Status](https://travis-ci.org/YasuOza/activemodel-attribute_changed_specification.png?branch=master)](https://travis-ci.org/YasuOza/activemodel-attribute_changed_specification)
3
+
4
+ Expand `_changed?` method defined in ActiveModel::Dirty. You can specify changed attribute value.
5
+
6
+ ## Installation
7
+
8
+ Add this gem to your Gemfile
9
+
10
+ ```
11
+ gem 'activemodel-attribute_changed_specification'
12
+ ```
13
+
14
+ Install via bundle
15
+
16
+ ```
17
+ $ bundle
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ Specify value changes.
23
+
24
+ ```ruby
25
+ user = User.new
26
+ user.name = 'Bob'
27
+ user.name_changed?(from: nil, to: 'Bob') # => true
28
+ user.name_changed?(from: 'Paul', to: 'Bob') # => false
29
+ ```
30
+
31
+ You can still use original `_changed?` method.
32
+
33
+ ```ruby
34
+ user = User.new
35
+ user.name = 'Bob'
36
+ user.name_changed? # => true
37
+ ```
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.pattern = "test/**/*_test.rb"
6
+ end
7
+
8
+ task :default => :test
@@ -0,0 +1,19 @@
1
+ require File.expand_path('../lib/active_model/attribute_changed_specification/version', __FILE__)
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = 'activemodel-attribute_changed_specification'
5
+ gem.version = ActiveModel::AttributeChangedSpecification::VERSION
6
+ gem.authors = 'Yasuharu Ozaki'
7
+ gem.email = 'yasuharu.ozaki@gmail.com'
8
+ gem.description = %q{Expand _changed? method. Enable to specify changed attribute value}
9
+ gem.summary = %q{Expand _changed? method. Enable to specify changed attribute value}
10
+ gem.homepage = 'https://github.com/YasuOza/activemodel-attribute_changed_specification'
11
+
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- test/*`.split("\n")
14
+ gem.require_paths = ['lib']
15
+
16
+ gem.add_dependency 'activemodel'
17
+ gem.add_dependency 'activerecord'
18
+ gem.add_development_dependency 'minitest', '>= 3'
19
+ end
@@ -0,0 +1,19 @@
1
+ module AttributeChangedSpecification
2
+ module ::ActiveModel
3
+ module Dirty
4
+
5
+ private
6
+
7
+ def attribute_changed_with_specification?(attr, *args)
8
+ if args.empty?
9
+ attribute_changed_without_specification?(attr)
10
+ else
11
+ return false unless self.changes.include?(attr)
12
+ self.changes[attr][0] == args[0][:from] && __send__(attr) == args[0][:to]
13
+ end
14
+ end
15
+ alias_method_chain :attribute_changed?, :specification
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveModel
2
+ module AttributeChangedSpecification
3
+ VERSION = '0.1.1'
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ require 'active_support/lazy_load_hooks'
2
+ require 'active_model/attribute_changed_specification/version'
3
+
4
+ ActiveSupport.on_load(:active_record) do
5
+ require 'active_model/attribute_changed_specification/specification'
6
+ end
@@ -0,0 +1,76 @@
1
+ # This code is mostly derived from rails/activemodel/test/cases/dirty_test.rb
2
+
3
+ require 'minitest/autorun'
4
+ require 'active_model'
5
+ require 'active_model/attribute_changed_specification/specification'
6
+
7
+ class DirtyTest < ActiveModel::TestCase
8
+ class DirtyModel
9
+ include ActiveModel::Dirty
10
+ define_attribute_methods [:name, :color]
11
+
12
+ def initialize
13
+ @name = nil
14
+ @color = nil
15
+ end
16
+
17
+ def name
18
+ @name
19
+ end
20
+
21
+ def name=(val)
22
+ name_will_change!
23
+ @name = val
24
+ end
25
+
26
+ def color
27
+ @color
28
+ end
29
+
30
+ def color=(val)
31
+ color_will_change! unless val == @color
32
+ @color = val
33
+ end
34
+
35
+ def save
36
+ @previously_changed = changes
37
+ @changed_attributes.clear
38
+ end
39
+ end
40
+
41
+ setup do
42
+ @model = DirtyModel.new
43
+ end
44
+
45
+ test "setting attribute will result in change" do
46
+ assert !@model.name_changed?
47
+ assert !@model.color_changed?
48
+
49
+ assert !@model.name_changed?(from: nil, to: 'name')
50
+ assert !@model.name_changed?(from: nil, to: nil)
51
+
52
+ assert !@model.color_changed?(from: nil, to: 'name')
53
+ assert !@model.color_changed?(from: nil, to: nil)
54
+ end
55
+
56
+ test "detect initialize change" do
57
+ @model.name = 'Yasu'
58
+ assert @model.changed?
59
+
60
+ assert @model.name_changed?
61
+ assert @model.name_changed?(from: nil, to: 'Yasu')
62
+ assert !@model.name_changed?(from: nil, to: 'Paul')
63
+
64
+ assert !@model.color_changed?
65
+ assert !@model.color_changed?(from: nil, to: nil)
66
+ end
67
+
68
+ test "detect update" do
69
+ @model.color = 'red'
70
+ @model.save
71
+ @model.color = 'green'
72
+
73
+ assert @model.color_changed?
74
+ assert @model.color_changed?(from: 'red', to: 'green')
75
+ end
76
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activemodel-attribute_changed_specification
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Yasuharu Ozaki
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activemodel
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activerecord
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: minitest
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '3'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '3'
62
+ description: Expand _changed? method. Enable to specify changed attribute value
63
+ email: yasuharu.ozaki@gmail.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - .gitignore
69
+ - .travis.yml
70
+ - Gemfile
71
+ - LICENSE
72
+ - README.md
73
+ - Rakefile
74
+ - activemodel-attribute_changed_specification.gemspec
75
+ - lib/active_model/attribute_changed_specification/specification.rb
76
+ - lib/active_model/attribute_changed_specification/version.rb
77
+ - lib/activemodel-attribute_changed_specification.rb
78
+ - test/specification_test.rb
79
+ homepage: https://github.com/YasuOza/activemodel-attribute_changed_specification
80
+ licenses: []
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 1.8.24
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: Expand _changed? method. Enable to specify changed attribute value
103
+ test_files:
104
+ - test/specification_test.rb
105
+ has_rdoc: