mholling-active_url 0.1.3 → 0.1.4
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/VERSION.yml +1 -1
- data/lib/active_url/callbacks.rb +5 -5
- data/rails/init.rb +1 -0
- data/spec/belongs_to_spec.rb +4 -0
- data/spec/callbacks_spec.rb +45 -0
- data/spec/instance_spec.rb +8 -0
- data/spec/validations_spec.rb +4 -13
- metadata +5 -2
data/VERSION.yml
CHANGED
data/lib/active_url/callbacks.rb
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
module ActiveUrl
|
|
2
2
|
module Callbacks
|
|
3
|
-
def
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
def save_with_callbacks
|
|
4
|
+
returning(save_without_callbacks) do |result|
|
|
5
|
+
run_callbacks(:after_save) unless result.blank?
|
|
6
|
+
end
|
|
7
7
|
end
|
|
8
8
|
|
|
9
9
|
def self.included(base)
|
|
10
10
|
base.class_eval do
|
|
11
11
|
include ActiveSupport::Callbacks # Already included by ActiveRecord.
|
|
12
|
-
alias_method_chain :
|
|
12
|
+
alias_method_chain :save, :callbacks
|
|
13
13
|
define_callbacks :after_save
|
|
14
14
|
end
|
|
15
15
|
end
|
data/rails/init.rb
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'active_url'
|
data/spec/belongs_to_spec.rb
CHANGED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe ActiveUrl do
|
|
4
|
+
before(:each) do
|
|
5
|
+
ActiveUrl::Config.stub!(:secret).and_return("secret")
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
context "instance with callbacks" do
|
|
9
|
+
before(:all) do
|
|
10
|
+
class ::Registration < ActiveUrl::Base
|
|
11
|
+
attribute :email
|
|
12
|
+
after_save :send_registration_email
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
after(:all) do
|
|
17
|
+
Object.send(:remove_const, "Registration")
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
before(:each) do
|
|
21
|
+
@registration = Registration.new(:email => "email@example.com")
|
|
22
|
+
@registration.stub!(:valid?).and_return(true)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "should not run the callback when saved if the instance is invalid" do
|
|
26
|
+
@registration.stub!(:valid?).and_return(false)
|
|
27
|
+
@registration.should_not_receive(:send_registration_email)
|
|
28
|
+
@registration.save
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "should run the callback when saved if the instance is valid" do
|
|
32
|
+
@registration.should_receive(:send_registration_email)
|
|
33
|
+
@registration.save
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
it "should not run the callback when found from its ID" do
|
|
37
|
+
@registration.stub!(:send_registration_email).and_return(true)
|
|
38
|
+
@registration.save
|
|
39
|
+
@found = Registration.new
|
|
40
|
+
Registration.should_receive(:new).and_return(@found)
|
|
41
|
+
@found.should_not_receive(:send_registration_email)
|
|
42
|
+
Registration.find(@registration.id)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
data/spec/instance_spec.rb
CHANGED
|
@@ -49,6 +49,10 @@ describe ActiveUrl do
|
|
|
49
49
|
end
|
|
50
50
|
end
|
|
51
51
|
|
|
52
|
+
after(:all) do
|
|
53
|
+
Object.send(:remove_const, "DerivedClass")
|
|
54
|
+
end
|
|
55
|
+
|
|
52
56
|
context "instance" do
|
|
53
57
|
it "should have individually accessible attribute readers" do
|
|
54
58
|
@url = DerivedClass.new
|
|
@@ -103,6 +107,10 @@ describe ActiveUrl do
|
|
|
103
107
|
end
|
|
104
108
|
end
|
|
105
109
|
|
|
110
|
+
after(:all) do
|
|
111
|
+
Object.send(:remove_const, "OtherClass")
|
|
112
|
+
end
|
|
113
|
+
|
|
106
114
|
it "should be based on class and attributes only" do
|
|
107
115
|
@url = DerivedClass.new(:bar => "bar", :baz => "baz")
|
|
108
116
|
@url2 = DerivedClass.new(:bar => "bar", :baz => "baz")
|
data/spec/validations_spec.rb
CHANGED
|
@@ -13,14 +13,13 @@ describe ActiveUrl do
|
|
|
13
13
|
validates_format_of :email, :with => /^[\w\.=-]+@[\w\.-]+\.[a-zA-Z]{2,4}$/ix
|
|
14
14
|
validates_length_of :password, :minimum => 8
|
|
15
15
|
validates_numericality_of :age
|
|
16
|
-
after_save :send_registration_email
|
|
17
|
-
|
|
18
|
-
def send_registration_email
|
|
19
|
-
@sent = true
|
|
20
|
-
end
|
|
21
16
|
end
|
|
22
17
|
end
|
|
23
18
|
|
|
19
|
+
after(:all) do
|
|
20
|
+
Object.send(:remove_const, "Registration")
|
|
21
|
+
end
|
|
22
|
+
|
|
24
23
|
context "when invalid" do
|
|
25
24
|
before(:each) do
|
|
26
25
|
@registration = Registration.new(:email => "user @ example . com", :password => "short", :age => "ten")
|
|
@@ -63,10 +62,6 @@ describe ActiveUrl do
|
|
|
63
62
|
it "should validate numericality of an attribute" do
|
|
64
63
|
@registration.errors[:age].should_not be_nil
|
|
65
64
|
end
|
|
66
|
-
|
|
67
|
-
it "should not execute any after_save callbacks" do
|
|
68
|
-
@registration.instance_variables.should_not include("@sent")
|
|
69
|
-
end
|
|
70
65
|
end
|
|
71
66
|
end
|
|
72
67
|
|
|
@@ -92,10 +87,6 @@ describe ActiveUrl do
|
|
|
92
87
|
@registration.id.should == @registration.to_param
|
|
93
88
|
end
|
|
94
89
|
|
|
95
|
-
it "should execute any after_save callbacks" do
|
|
96
|
-
@registration.instance_variables.map(&:to_s).should include("@sent")
|
|
97
|
-
end
|
|
98
|
-
|
|
99
90
|
context "and re-found by its class" do
|
|
100
91
|
before(:each) do
|
|
101
92
|
@found = Registration.find(@registration.id)
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: mholling-active_url
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Matthew Hollingworth
|
|
@@ -9,7 +9,7 @@ autorequire:
|
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
11
|
|
|
12
|
-
date: 2009-06-
|
|
12
|
+
date: 2009-06-21 00:00:00 -07:00
|
|
13
13
|
default_executable:
|
|
14
14
|
dependencies:
|
|
15
15
|
- !ruby/object:Gem::Dependency
|
|
@@ -44,7 +44,9 @@ files:
|
|
|
44
44
|
- lib/active_url/crypto.rb
|
|
45
45
|
- lib/active_url/errors.rb
|
|
46
46
|
- lib/active_url/validations.rb
|
|
47
|
+
- rails/init.rb
|
|
47
48
|
- spec/belongs_to_spec.rb
|
|
49
|
+
- spec/callbacks_spec.rb
|
|
48
50
|
- spec/crypto_spec.rb
|
|
49
51
|
- spec/instance_spec.rb
|
|
50
52
|
- spec/spec_helper.rb
|
|
@@ -77,6 +79,7 @@ specification_version: 2
|
|
|
77
79
|
summary: A Rails library for generating secret URLs.
|
|
78
80
|
test_files:
|
|
79
81
|
- spec/belongs_to_spec.rb
|
|
82
|
+
- spec/callbacks_spec.rb
|
|
80
83
|
- spec/crypto_spec.rb
|
|
81
84
|
- spec/instance_spec.rb
|
|
82
85
|
- spec/spec_helper.rb
|