ohm-contrib 0.0.31 → 0.0.33
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.
- data/LICENSE +2 -1
- data/README.markdown +2 -1
- data/Rakefile +4 -53
- data/lib/ohm/contrib.rb +2 -2
- data/lib/ohm/contrib/boundaries.rb +1 -0
- data/lib/ohm/contrib/callbacks.rb +5 -4
- data/lib/ohm/contrib/date_validations.rb +2 -1
- data/lib/ohm/contrib/extra_validations.rb +1 -0
- data/lib/ohm/contrib/length_validations.rb +1 -0
- data/lib/ohm/contrib/locking.rb +2 -1
- data/lib/ohm/contrib/lunar_macros.rb +5 -4
- data/lib/ohm/contrib/number_validations.rb +1 -0
- data/lib/ohm/contrib/slug.rb +1 -0
- data/lib/ohm/contrib/timestamping.rb +1 -0
- data/lib/ohm/contrib/typecast.rb +1 -0
- data/lib/ohm/contrib/web_validations.rb +2 -1
- data/test/autoload_test.rb +19 -0
- data/test/boundaries_test.rb +45 -0
- data/test/callbacks_lint.rb +105 -0
- data/test/date_validations_test.rb +29 -0
- data/test/helper.rb +19 -10
- data/test/instance_callbacks_test.rb +45 -0
- data/test/length_validations_test.rb +31 -0
- data/test/lunar_macros_test.rb +146 -0
- data/test/macro_callbacks_test.rb +57 -0
- data/test/membership_validation_test.rb +31 -0
- data/test/number_validations_test.rb +37 -0
- data/test/slug_test.rb +30 -0
- data/test/timestamping_test.rb +32 -0
- data/test/typecast_array_test.rb +154 -0
- data/test/typecast_boolean_test.rb +43 -0
- data/test/typecast_date_test.rb +77 -0
- data/test/typecast_decimal_test.rb +77 -0
- data/test/typecast_float_test.rb +52 -0
- data/test/typecast_hash_test.rb +117 -0
- data/test/typecast_integer_test.rb +66 -0
- data/test/typecast_string_test.rb +38 -0
- data/test/typecast_time_test.rb +67 -0
- data/test/typecast_timezone_test.rb +37 -0
- data/test/web_validations_test.rb +79 -0
- metadata +47 -56
- data/.document +0 -5
- data/.gitignore +0 -25
- data/VERSION +0 -1
- data/lib/ohm/contrib/to_hash.rb +0 -39
- data/ohm-contrib.gemspec +0 -103
- data/test/test_ohm_boundaries.rb +0 -74
- data/test/test_ohm_contrib.rb +0 -70
- data/test/test_ohm_contrib_callbacks.rb +0 -375
- data/test/test_ohm_date_validations.rb +0 -30
- data/test/test_ohm_extra_validations.rb +0 -32
- data/test/test_ohm_length_validations.rb +0 -32
- data/test/test_ohm_lunar_macros.rb +0 -165
- data/test/test_ohm_number_validations.rb +0 -59
- data/test/test_ohm_slug.rb +0 -29
- data/test/test_ohm_timestamping.rb +0 -64
- data/test/test_ohm_to_hash.rb +0 -67
- data/test/test_ohm_typecast.rb +0 -707
- data/test/test_ohm_web_validations.rb +0 -114
data/.document
DELETED
data/.gitignore
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
## MAC OS
|
2
|
-
.DS_Store
|
3
|
-
|
4
|
-
## TEXTMATE
|
5
|
-
*.tmproj
|
6
|
-
tmtags
|
7
|
-
|
8
|
-
## EMACS
|
9
|
-
*~
|
10
|
-
\#*
|
11
|
-
.\#*
|
12
|
-
|
13
|
-
## VIM
|
14
|
-
*.swp
|
15
|
-
|
16
|
-
## PROJECT::GENERAL
|
17
|
-
coverage
|
18
|
-
rdoc
|
19
|
-
pkg
|
20
|
-
|
21
|
-
## PROJECT::SPECIFIC
|
22
|
-
/.rvmrc
|
23
|
-
/.yardoc
|
24
|
-
/doc
|
25
|
-
/tests
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.0.31
|
data/lib/ohm/contrib/to_hash.rb
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
module Ohm
|
2
|
-
# Ohm has already added its own `to_hash` method. The difference
|
3
|
-
# is that it chose the albeit better whitelisted approach.
|
4
|
-
#
|
5
|
-
# @example
|
6
|
-
#
|
7
|
-
# # this is the core Ohm#to_hash implementation
|
8
|
-
# class Post < Ohm::Model
|
9
|
-
# attribute :body
|
10
|
-
# def validate
|
11
|
-
# assert_present :body
|
12
|
-
# end
|
13
|
-
# end
|
14
|
-
#
|
15
|
-
# Post.create.to_hash == { :errors => [[:body, :not_present]] }
|
16
|
-
# # => true
|
17
|
-
#
|
18
|
-
# Post.create(:body => "The body").to_hash == { :id => 1 }
|
19
|
-
# # => true
|
20
|
-
#
|
21
|
-
# # now this is the all-in-one (kinda Railsy) method.
|
22
|
-
# class Post < Ohm::Model
|
23
|
-
# attribute :body
|
24
|
-
# end
|
25
|
-
#
|
26
|
-
# Post.create(:body => "Body").to_hash == { :id => 1, :body => "Body" }
|
27
|
-
# # => true
|
28
|
-
#
|
29
|
-
# @todo use super when Ohm has finally release their #to_hash impl.
|
30
|
-
module ToHash
|
31
|
-
def to_hash
|
32
|
-
atts = attributes + counters
|
33
|
-
hash = atts.inject({}) { |h, att| h[att] = send(att); h }
|
34
|
-
hash[:id] = @id
|
35
|
-
hash
|
36
|
-
end
|
37
|
-
alias :to_h :to_hash
|
38
|
-
end
|
39
|
-
end
|
data/ohm-contrib.gemspec
DELETED
@@ -1,103 +0,0 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
-
# -*- encoding: utf-8 -*-
|
5
|
-
|
6
|
-
Gem::Specification.new do |s|
|
7
|
-
s.name = %q{ohm-contrib}
|
8
|
-
s.version = "0.0.31"
|
9
|
-
|
10
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = ["Cyril David"]
|
12
|
-
s.date = %q{2010-08-11}
|
13
|
-
s.description = %q{Highly decoupled drop-in functionality for Ohm models}
|
14
|
-
s.email = %q{cyx.ucron@gmail.com}
|
15
|
-
s.extra_rdoc_files = [
|
16
|
-
"LICENSE",
|
17
|
-
"README.markdown"
|
18
|
-
]
|
19
|
-
s.files = [
|
20
|
-
".document",
|
21
|
-
".gitignore",
|
22
|
-
"LICENSE",
|
23
|
-
"README.markdown",
|
24
|
-
"Rakefile",
|
25
|
-
"VERSION",
|
26
|
-
"lib/ohm/contrib.rb",
|
27
|
-
"lib/ohm/contrib/boundaries.rb",
|
28
|
-
"lib/ohm/contrib/callbacks.rb",
|
29
|
-
"lib/ohm/contrib/date_validations.rb",
|
30
|
-
"lib/ohm/contrib/extra_validations.rb",
|
31
|
-
"lib/ohm/contrib/length_validations.rb",
|
32
|
-
"lib/ohm/contrib/locking.rb",
|
33
|
-
"lib/ohm/contrib/lunar_macros.rb",
|
34
|
-
"lib/ohm/contrib/number_validations.rb",
|
35
|
-
"lib/ohm/contrib/slug.rb",
|
36
|
-
"lib/ohm/contrib/timestamping.rb",
|
37
|
-
"lib/ohm/contrib/to_hash.rb",
|
38
|
-
"lib/ohm/contrib/typecast.rb",
|
39
|
-
"lib/ohm/contrib/web_validations.rb",
|
40
|
-
"ohm-contrib.gemspec",
|
41
|
-
"test/helper.rb",
|
42
|
-
"test/test_ohm_boundaries.rb",
|
43
|
-
"test/test_ohm_contrib.rb",
|
44
|
-
"test/test_ohm_contrib_callbacks.rb",
|
45
|
-
"test/test_ohm_date_validations.rb",
|
46
|
-
"test/test_ohm_extra_validations.rb",
|
47
|
-
"test/test_ohm_length_validations.rb",
|
48
|
-
"test/test_ohm_lunar_macros.rb",
|
49
|
-
"test/test_ohm_number_validations.rb",
|
50
|
-
"test/test_ohm_slug.rb",
|
51
|
-
"test/test_ohm_timestamping.rb",
|
52
|
-
"test/test_ohm_to_hash.rb",
|
53
|
-
"test/test_ohm_typecast.rb",
|
54
|
-
"test/test_ohm_web_validations.rb"
|
55
|
-
]
|
56
|
-
s.homepage = %q{http://labs.sinefunc.com/ohm-contrib}
|
57
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
58
|
-
s.require_paths = ["lib"]
|
59
|
-
s.rubygems_version = %q{1.3.7}
|
60
|
-
s.summary = %q{A collection of ohm related modules}
|
61
|
-
s.test_files = [
|
62
|
-
"test/helper.rb",
|
63
|
-
"test/test_ohm_boundaries.rb",
|
64
|
-
"test/test_ohm_contrib.rb",
|
65
|
-
"test/test_ohm_contrib_callbacks.rb",
|
66
|
-
"test/test_ohm_date_validations.rb",
|
67
|
-
"test/test_ohm_extra_validations.rb",
|
68
|
-
"test/test_ohm_length_validations.rb",
|
69
|
-
"test/test_ohm_lunar_macros.rb",
|
70
|
-
"test/test_ohm_number_validations.rb",
|
71
|
-
"test/test_ohm_slug.rb",
|
72
|
-
"test/test_ohm_timestamping.rb",
|
73
|
-
"test/test_ohm_to_hash.rb",
|
74
|
-
"test/test_ohm_typecast.rb",
|
75
|
-
"test/test_ohm_web_validations.rb"
|
76
|
-
]
|
77
|
-
|
78
|
-
if s.respond_to? :specification_version then
|
79
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
80
|
-
s.specification_version = 3
|
81
|
-
|
82
|
-
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
83
|
-
s.add_development_dependency(%q<contest>, [">= 0"])
|
84
|
-
s.add_development_dependency(%q<redis>, [">= 0"])
|
85
|
-
s.add_development_dependency(%q<timecop>, [">= 0"])
|
86
|
-
s.add_development_dependency(%q<mocha>, [">= 0"])
|
87
|
-
s.add_development_dependency(%q<lunar>, [">= 0"])
|
88
|
-
else
|
89
|
-
s.add_dependency(%q<contest>, [">= 0"])
|
90
|
-
s.add_dependency(%q<redis>, [">= 0"])
|
91
|
-
s.add_dependency(%q<timecop>, [">= 0"])
|
92
|
-
s.add_dependency(%q<mocha>, [">= 0"])
|
93
|
-
s.add_dependency(%q<lunar>, [">= 0"])
|
94
|
-
end
|
95
|
-
else
|
96
|
-
s.add_dependency(%q<contest>, [">= 0"])
|
97
|
-
s.add_dependency(%q<redis>, [">= 0"])
|
98
|
-
s.add_dependency(%q<timecop>, [">= 0"])
|
99
|
-
s.add_dependency(%q<mocha>, [">= 0"])
|
100
|
-
s.add_dependency(%q<lunar>, [">= 0"])
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
data/test/test_ohm_boundaries.rb
DELETED
@@ -1,74 +0,0 @@
|
|
1
|
-
require "helper"
|
2
|
-
|
3
|
-
class TestOhmBoundaries < Test::Unit::TestCase
|
4
|
-
setup do
|
5
|
-
Ohm.flush
|
6
|
-
end
|
7
|
-
|
8
|
-
class Person < Ohm::Model
|
9
|
-
include Ohm::Boundaries
|
10
|
-
|
11
|
-
attribute :name
|
12
|
-
index :name
|
13
|
-
end
|
14
|
-
|
15
|
-
context "when there are no People" do
|
16
|
-
should "have a nil Person.first" do
|
17
|
-
assert_nil Person.first
|
18
|
-
end
|
19
|
-
|
20
|
-
should "have a nil Person.last" do
|
21
|
-
assert_nil Person.last
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
context "after creating a single Person named matz" do
|
26
|
-
setup do
|
27
|
-
@matz = Person.create(:name => "matz")
|
28
|
-
end
|
29
|
-
|
30
|
-
should "have matz as the first person" do
|
31
|
-
assert_equal @matz, Person.first
|
32
|
-
end
|
33
|
-
|
34
|
-
should "have matz as the last person" do
|
35
|
-
assert_equal @matz, Person.last
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
context "after creating matz first then linus next" do
|
40
|
-
setup do
|
41
|
-
@matz = Person.create(:name => "matz")
|
42
|
-
@linus = Person.create(:name => "linus")
|
43
|
-
end
|
44
|
-
|
45
|
-
should "have matz as the first Person" do
|
46
|
-
assert_equal @matz, Person.first
|
47
|
-
end
|
48
|
-
|
49
|
-
should "have linus as the last Person" do
|
50
|
-
assert_equal @linus, Person.last
|
51
|
-
end
|
52
|
-
|
53
|
-
context "when searching name => matz" do
|
54
|
-
test "matz is the first and last" do
|
55
|
-
assert_equal @matz, Person.first(:name => "matz")
|
56
|
-
assert_equal @matz, Person.last(:name => "matz")
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
context "when searching name => linus" do
|
61
|
-
test "linus is first and last" do
|
62
|
-
assert_equal @linus, Person.first(:name => "linus")
|
63
|
-
assert_equal @linus, Person.last(:name => "linus")
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
context "when searching name => quentin" do
|
68
|
-
should "have nil as first and last" do
|
69
|
-
assert_nil Person.first(:name => "quentin")
|
70
|
-
assert_nil Person.last(:name => "quentin")
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
data/test/test_ohm_contrib.rb
DELETED
@@ -1,70 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class TestOhmContrib < Test::Unit::TestCase
|
4
|
-
test "autoloading of Boundaries" do
|
5
|
-
assert_nothing_raised NameError, LoadError do
|
6
|
-
Ohm::Boundaries
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
test "autoloading of timestamping" do
|
11
|
-
assert_nothing_raised NameError, LoadError do
|
12
|
-
Ohm::Timestamping
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
test "autoloading of ToHash" do
|
17
|
-
assert_nothing_raised NameError, LoadError do
|
18
|
-
Ohm::ToHash
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
test "autoloading of WebValidations" do
|
23
|
-
assert_nothing_raised NameError, LoadError do
|
24
|
-
Ohm::WebValidations
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
test "autoloading of NumberValidations" do
|
29
|
-
assert_nothing_raised NameError, LoadError do
|
30
|
-
Ohm::NumberValidations
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
test "autoloading of Typecast" do
|
35
|
-
assert_nothing_raised NameError, LoadError do
|
36
|
-
Ohm::Typecast
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
test "autoloading of Locking" do
|
41
|
-
assert_nothing_raised NameError, LoadError do
|
42
|
-
Ohm::Locking
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
test "autoloading of ExtraValidations" do
|
47
|
-
assert_nothing_raised NameError, LoadError do
|
48
|
-
Ohm::ExtraValidations
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
test "autoloading of DateValidations" do
|
53
|
-
assert_nothing_raised NameError, LoadError do
|
54
|
-
Ohm::DateValidations
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
test "autoloading of LunarMacros" do
|
59
|
-
require 'lunar'
|
60
|
-
assert_nothing_raised NameError, LoadError do
|
61
|
-
Ohm::LunarMacros
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
test "autoloading of Slug" do
|
66
|
-
assert_nothing_raised NameError, LoadError do
|
67
|
-
Ohm::Slug
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
@@ -1,375 +0,0 @@
|
|
1
|
-
require "helper"
|
2
|
-
|
3
|
-
class OhmContribCallbacksTest < Test::Unit::TestCase
|
4
|
-
describe "instance level validation callbacks" do
|
5
|
-
class InstanceTypePost < Ohm::Model
|
6
|
-
include Ohm::Callbacks
|
7
|
-
|
8
|
-
attribute :body
|
9
|
-
|
10
|
-
def validate
|
11
|
-
super
|
12
|
-
|
13
|
-
assert_present :body
|
14
|
-
end
|
15
|
-
|
16
|
-
def did?(action)
|
17
|
-
instance_variable_get("@#{ action }")
|
18
|
-
end
|
19
|
-
|
20
|
-
def count(action)
|
21
|
-
instance_variable_get("@#{ action }")
|
22
|
-
end
|
23
|
-
|
24
|
-
protected
|
25
|
-
def before_validate() incr(:do_before_validate) end
|
26
|
-
def after_validate() incr(:do_after_validate) end
|
27
|
-
def before_create() incr(:do_before_create) end
|
28
|
-
def after_create() incr(:do_after_create) end
|
29
|
-
def before_save() incr(:do_before_save) end
|
30
|
-
def after_save() incr(:do_after_save) end
|
31
|
-
def before_delete() incr(:do_before_delete) end
|
32
|
-
def after_delete() incr(:do_after_delete) end
|
33
|
-
|
34
|
-
|
35
|
-
def incr(action)
|
36
|
-
val = instance_variable_get("@#{ action }")
|
37
|
-
val ||= 0
|
38
|
-
val += 1
|
39
|
-
|
40
|
-
instance_variable_set("@#{ action }", val)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
context "on save when invalid state" do
|
45
|
-
setup do
|
46
|
-
@post = InstanceTypePost.new
|
47
|
-
@post.save
|
48
|
-
end
|
49
|
-
|
50
|
-
should "still call before / after validate" do
|
51
|
-
assert @post.did?(:do_before_validate)
|
52
|
-
assert @post.did?(:do_after_validate)
|
53
|
-
end
|
54
|
-
|
55
|
-
should "not call all other callbacks" do
|
56
|
-
assert ! @post.did?(:do_before_create)
|
57
|
-
assert ! @post.did?(:do_after_create)
|
58
|
-
assert ! @post.did?(:do_before_save)
|
59
|
-
assert ! @post.did?(:do_after_save)
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
context "on save when valid state" do
|
64
|
-
setup do
|
65
|
-
@post = InstanceTypePost.new(:body => "The Body")
|
66
|
-
@post.save
|
67
|
-
end
|
68
|
-
|
69
|
-
should "call all callbacks" do
|
70
|
-
assert @post.did?(:do_before_validate)
|
71
|
-
assert @post.did?(:do_after_validate)
|
72
|
-
assert @post.did?(:do_before_create)
|
73
|
-
assert @post.did?(:do_after_create)
|
74
|
-
assert @post.did?(:do_before_save)
|
75
|
-
assert @post.did?(:do_after_save)
|
76
|
-
end
|
77
|
-
|
78
|
-
should "call create / save callbacks only once" do
|
79
|
-
assert_equal 1, @post.count(:do_before_create)
|
80
|
-
assert_equal 1, @post.count(:do_after_create)
|
81
|
-
assert_equal 1, @post.count(:do_before_save)
|
82
|
-
assert_equal 1, @post.count(:do_after_create)
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
context "on create when valid state" do
|
87
|
-
setup do
|
88
|
-
@post = InstanceTypePost.create(:body => "The Body")
|
89
|
-
end
|
90
|
-
|
91
|
-
should "call all callbacks" do
|
92
|
-
assert @post.did?(:do_before_validate)
|
93
|
-
assert @post.did?(:do_after_validate)
|
94
|
-
assert @post.did?(:do_before_create)
|
95
|
-
assert @post.did?(:do_after_create)
|
96
|
-
assert @post.did?(:do_before_save)
|
97
|
-
assert @post.did?(:do_after_save)
|
98
|
-
end
|
99
|
-
|
100
|
-
should "call create / save callbacks only once" do
|
101
|
-
assert_equal 1, @post.count(:do_before_create)
|
102
|
-
assert_equal 1, @post.count(:do_after_create)
|
103
|
-
assert_equal 1, @post.count(:do_before_save)
|
104
|
-
assert_equal 1, @post.count(:do_after_create)
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
|
109
|
-
context "on save of an existing object" do
|
110
|
-
setup do
|
111
|
-
@post = InstanceTypePost.create(:body => "The Body")
|
112
|
-
@post = InstanceTypePost[@post.id]
|
113
|
-
|
114
|
-
@post.save
|
115
|
-
end
|
116
|
-
|
117
|
-
should "not call create related callbacks" do
|
118
|
-
assert ! @post.did?(:do_before_create)
|
119
|
-
assert ! @post.did?(:do_after_create)
|
120
|
-
end
|
121
|
-
|
122
|
-
should "call the rest of the callbacks" do
|
123
|
-
assert @post.did?(:do_before_validate)
|
124
|
-
assert @post.did?(:do_after_validate)
|
125
|
-
assert @post.did?(:do_before_save)
|
126
|
-
assert @post.did?(:do_after_save)
|
127
|
-
end
|
128
|
-
|
129
|
-
should "call save callbacks only once" do
|
130
|
-
assert_equal 1, @post.count(:do_before_save)
|
131
|
-
assert_equal 1, @post.count(:do_after_save)
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
context "on save of an existing invalid object" do
|
136
|
-
setup do
|
137
|
-
@post = InstanceTypePost.create(:body => "The Body")
|
138
|
-
@post = InstanceTypePost[@post.id]
|
139
|
-
|
140
|
-
@post.body = nil
|
141
|
-
@post.save
|
142
|
-
end
|
143
|
-
|
144
|
-
should "call validation related callbacks" do
|
145
|
-
assert @post.did?(:do_before_validate)
|
146
|
-
assert @post.did?(:do_after_validate)
|
147
|
-
end
|
148
|
-
|
149
|
-
should "not call create related callbacks" do
|
150
|
-
assert ! @post.did?(:do_before_create)
|
151
|
-
assert ! @post.did?(:do_after_create)
|
152
|
-
end
|
153
|
-
|
154
|
-
should "also not call save related callbacks" do
|
155
|
-
assert ! @post.did?(:do_before_save)
|
156
|
-
assert ! @post.did?(:do_after_save)
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
context "on delete" do
|
161
|
-
setup do
|
162
|
-
@post = InstanceTypePost.create(:body => "The Body")
|
163
|
-
@post = InstanceTypePost[@post.id]
|
164
|
-
@post.delete
|
165
|
-
end
|
166
|
-
|
167
|
-
|
168
|
-
should "call delete related callbacks once" do
|
169
|
-
assert_equal 1, @post.count(:do_before_delete)
|
170
|
-
assert_equal 1, @post.count(:do_after_delete)
|
171
|
-
end
|
172
|
-
|
173
|
-
should "not call all other callbacks" do
|
174
|
-
assert ! @post.did?(:do_before_validate)
|
175
|
-
assert ! @post.did?(:do_after_validate)
|
176
|
-
assert ! @post.did?(:do_before_create)
|
177
|
-
assert ! @post.did?(:do_after_create)
|
178
|
-
assert ! @post.did?(:do_before_save)
|
179
|
-
assert ! @post.did?(:do_after_save)
|
180
|
-
end
|
181
|
-
end
|
182
|
-
end
|
183
|
-
|
184
|
-
describe "macro level validation callbacks" do
|
185
|
-
class Post < Ohm::Model
|
186
|
-
include Ohm::Callbacks
|
187
|
-
|
188
|
-
attribute :body
|
189
|
-
|
190
|
-
before :validate, :do_before_validate
|
191
|
-
after :validate, :do_after_validate
|
192
|
-
|
193
|
-
before :create, :do_before_create
|
194
|
-
after :create, :do_after_create
|
195
|
-
|
196
|
-
before :save, :do_before_save
|
197
|
-
after :save, :do_after_save
|
198
|
-
|
199
|
-
before :delete, :do_before_delete
|
200
|
-
after :delete, :do_after_delete
|
201
|
-
|
202
|
-
def validate
|
203
|
-
super
|
204
|
-
|
205
|
-
assert_present :body
|
206
|
-
end
|
207
|
-
|
208
|
-
def did?(action)
|
209
|
-
instance_variable_get("@#{ action }")
|
210
|
-
end
|
211
|
-
|
212
|
-
def count(action)
|
213
|
-
instance_variable_get("@#{ action }")
|
214
|
-
end
|
215
|
-
|
216
|
-
protected
|
217
|
-
def do_before_validate() incr(:do_before_validate) end
|
218
|
-
def do_after_validate() incr(:do_after_validate) end
|
219
|
-
def do_before_create() incr(:do_before_create) end
|
220
|
-
def do_after_create() incr(:do_after_create) end
|
221
|
-
def do_before_save() incr(:do_before_save) end
|
222
|
-
def do_after_save() incr(:do_after_save) end
|
223
|
-
def do_before_delete() incr(:do_before_delete) end
|
224
|
-
def do_after_delete() incr(:do_after_delete) end
|
225
|
-
|
226
|
-
|
227
|
-
def incr(action)
|
228
|
-
val = instance_variable_get("@#{ action }")
|
229
|
-
val ||= 0
|
230
|
-
val += 1
|
231
|
-
|
232
|
-
instance_variable_set("@#{ action }", val)
|
233
|
-
end
|
234
|
-
end
|
235
|
-
|
236
|
-
context "on save when invalid state" do
|
237
|
-
setup do
|
238
|
-
@post = Post.new
|
239
|
-
@post.save
|
240
|
-
end
|
241
|
-
|
242
|
-
should "still call before / after validate" do
|
243
|
-
assert @post.did?(:do_before_validate)
|
244
|
-
assert @post.did?(:do_after_validate)
|
245
|
-
end
|
246
|
-
|
247
|
-
should "not call all other callbacks" do
|
248
|
-
assert ! @post.did?(:do_before_create)
|
249
|
-
assert ! @post.did?(:do_after_create)
|
250
|
-
assert ! @post.did?(:do_before_save)
|
251
|
-
assert ! @post.did?(:do_after_save)
|
252
|
-
end
|
253
|
-
end
|
254
|
-
|
255
|
-
context "on save when valid state" do
|
256
|
-
setup do
|
257
|
-
@post = Post.new(:body => "The Body")
|
258
|
-
@post.save
|
259
|
-
end
|
260
|
-
|
261
|
-
should "call all callbacks" do
|
262
|
-
assert @post.did?(:do_before_validate)
|
263
|
-
assert @post.did?(:do_after_validate)
|
264
|
-
assert @post.did?(:do_before_create)
|
265
|
-
assert @post.did?(:do_after_create)
|
266
|
-
assert @post.did?(:do_before_save)
|
267
|
-
assert @post.did?(:do_after_save)
|
268
|
-
end
|
269
|
-
|
270
|
-
should "call create / save callbacks only once" do
|
271
|
-
assert_equal 1, @post.count(:do_before_create)
|
272
|
-
assert_equal 1, @post.count(:do_after_create)
|
273
|
-
assert_equal 1, @post.count(:do_before_save)
|
274
|
-
assert_equal 1, @post.count(:do_after_create)
|
275
|
-
end
|
276
|
-
end
|
277
|
-
|
278
|
-
context "on create when valid state" do
|
279
|
-
setup do
|
280
|
-
@post = Post.create(:body => "The Body")
|
281
|
-
end
|
282
|
-
|
283
|
-
should "call all callbacks" do
|
284
|
-
assert @post.did?(:do_before_validate)
|
285
|
-
assert @post.did?(:do_after_validate)
|
286
|
-
assert @post.did?(:do_before_create)
|
287
|
-
assert @post.did?(:do_after_create)
|
288
|
-
assert @post.did?(:do_before_save)
|
289
|
-
assert @post.did?(:do_after_save)
|
290
|
-
end
|
291
|
-
|
292
|
-
should "call create / save callbacks only once" do
|
293
|
-
assert_equal 1, @post.count(:do_before_create)
|
294
|
-
assert_equal 1, @post.count(:do_after_create)
|
295
|
-
assert_equal 1, @post.count(:do_before_save)
|
296
|
-
assert_equal 1, @post.count(:do_after_create)
|
297
|
-
end
|
298
|
-
end
|
299
|
-
|
300
|
-
|
301
|
-
context "on save of an existing object" do
|
302
|
-
setup do
|
303
|
-
@post = Post.create(:body => "The Body")
|
304
|
-
@post = Post[@post.id]
|
305
|
-
|
306
|
-
@post.save
|
307
|
-
end
|
308
|
-
|
309
|
-
should "not call create related callbacks" do
|
310
|
-
assert ! @post.did?(:do_before_create)
|
311
|
-
assert ! @post.did?(:do_after_create)
|
312
|
-
end
|
313
|
-
|
314
|
-
should "call the rest of the callbacks" do
|
315
|
-
assert @post.did?(:do_before_validate)
|
316
|
-
assert @post.did?(:do_after_validate)
|
317
|
-
assert @post.did?(:do_before_save)
|
318
|
-
assert @post.did?(:do_after_save)
|
319
|
-
end
|
320
|
-
|
321
|
-
should "call save callbacks only once" do
|
322
|
-
assert_equal 1, @post.count(:do_before_save)
|
323
|
-
assert_equal 1, @post.count(:do_after_save)
|
324
|
-
end
|
325
|
-
end
|
326
|
-
|
327
|
-
context "on save of an existing invalid object" do
|
328
|
-
setup do
|
329
|
-
@post = Post.create(:body => "The Body")
|
330
|
-
@post = Post[@post.id]
|
331
|
-
|
332
|
-
@post.body = nil
|
333
|
-
@post.save
|
334
|
-
end
|
335
|
-
|
336
|
-
should "call validation related callbacks" do
|
337
|
-
assert @post.did?(:do_before_validate)
|
338
|
-
assert @post.did?(:do_after_validate)
|
339
|
-
end
|
340
|
-
|
341
|
-
should "not call create related callbacks" do
|
342
|
-
assert ! @post.did?(:do_before_create)
|
343
|
-
assert ! @post.did?(:do_after_create)
|
344
|
-
end
|
345
|
-
|
346
|
-
should "also not call save related callbacks" do
|
347
|
-
assert ! @post.did?(:do_before_save)
|
348
|
-
assert ! @post.did?(:do_after_save)
|
349
|
-
end
|
350
|
-
end
|
351
|
-
|
352
|
-
context "on delete" do
|
353
|
-
setup do
|
354
|
-
@post = Post.create(:body => "The Body")
|
355
|
-
@post = Post[@post.id]
|
356
|
-
@post.delete
|
357
|
-
end
|
358
|
-
|
359
|
-
|
360
|
-
should "call delete related callbacks once" do
|
361
|
-
assert_equal 1, @post.count(:do_before_delete)
|
362
|
-
assert_equal 1, @post.count(:do_after_delete)
|
363
|
-
end
|
364
|
-
|
365
|
-
should "not call all other callbacks" do
|
366
|
-
assert ! @post.did?(:do_before_validate)
|
367
|
-
assert ! @post.did?(:do_after_validate)
|
368
|
-
assert ! @post.did?(:do_before_create)
|
369
|
-
assert ! @post.did?(:do_after_create)
|
370
|
-
assert ! @post.did?(:do_before_save)
|
371
|
-
assert ! @post.did?(:do_after_save)
|
372
|
-
end
|
373
|
-
end
|
374
|
-
end
|
375
|
-
end
|