ar_default_values 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +2 -2
- data/VERSION +1 -1
- data/ar_default_values.gemspec +9 -10
- data/lib/ar_default_values.rb +7 -3
- data/spec/ar_default_values_spec.rb +59 -34
- metadata +55 -21
- data/Gemfile.lock +0 -51
data/Gemfile
CHANGED
@@ -5,8 +5,8 @@ source "http://rubygems.org"
|
|
5
5
|
|
6
6
|
# Add dependencies to develop your gem here.
|
7
7
|
# Include everything needed to run rake, tests, features, etc.
|
8
|
-
gem "activesupport"
|
9
|
-
gem "activerecord"
|
8
|
+
gem "activesupport", ">= 3.0.0"
|
9
|
+
gem "activerecord", ">= 3.0.0"
|
10
10
|
|
11
11
|
group :development do
|
12
12
|
gem "rspec"
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/ar_default_values.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "ar_default_values"
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.3.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["masarakki"]
|
12
|
-
s.date = "
|
12
|
+
s.date = "2013-01-08"
|
13
13
|
s.description = "ActiveRecord with default values"
|
14
14
|
s.email = "masaki@hisme.net"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -20,7 +20,6 @@ Gem::Specification.new do |s|
|
|
20
20
|
".document",
|
21
21
|
".rspec",
|
22
22
|
"Gemfile",
|
23
|
-
"Gemfile.lock",
|
24
23
|
"LICENSE.txt",
|
25
24
|
"README.rdoc",
|
26
25
|
"Rakefile",
|
@@ -35,23 +34,23 @@ Gem::Specification.new do |s|
|
|
35
34
|
s.homepage = "http://github.com/masarakki/ar_default_values"
|
36
35
|
s.licenses = ["MIT"]
|
37
36
|
s.require_paths = ["lib"]
|
38
|
-
s.rubygems_version = "1.8.
|
37
|
+
s.rubygems_version = "1.8.24"
|
39
38
|
s.summary = "ActiveRecord with default values"
|
40
39
|
|
41
40
|
if s.respond_to? :specification_version then
|
42
41
|
s.specification_version = 3
|
43
42
|
|
44
43
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
45
|
-
s.add_runtime_dependency(%q<activesupport>, [">= 0"])
|
46
|
-
s.add_runtime_dependency(%q<activerecord>, [">= 0"])
|
44
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 3.0.0"])
|
45
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 3.0.0"])
|
47
46
|
s.add_development_dependency(%q<rspec>, [">= 0"])
|
48
47
|
s.add_development_dependency(%q<rdoc>, [">= 0"])
|
49
48
|
s.add_development_dependency(%q<bundler>, [">= 0"])
|
50
49
|
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
51
50
|
s.add_development_dependency(%q<sqlite3>, [">= 0"])
|
52
51
|
else
|
53
|
-
s.add_dependency(%q<activesupport>, [">= 0"])
|
54
|
-
s.add_dependency(%q<activerecord>, [">= 0"])
|
52
|
+
s.add_dependency(%q<activesupport>, [">= 3.0.0"])
|
53
|
+
s.add_dependency(%q<activerecord>, [">= 3.0.0"])
|
55
54
|
s.add_dependency(%q<rspec>, [">= 0"])
|
56
55
|
s.add_dependency(%q<rdoc>, [">= 0"])
|
57
56
|
s.add_dependency(%q<bundler>, [">= 0"])
|
@@ -59,8 +58,8 @@ Gem::Specification.new do |s|
|
|
59
58
|
s.add_dependency(%q<sqlite3>, [">= 0"])
|
60
59
|
end
|
61
60
|
else
|
62
|
-
s.add_dependency(%q<activesupport>, [">= 0"])
|
63
|
-
s.add_dependency(%q<activerecord>, [">= 0"])
|
61
|
+
s.add_dependency(%q<activesupport>, [">= 3.0.0"])
|
62
|
+
s.add_dependency(%q<activerecord>, [">= 3.0.0"])
|
64
63
|
s.add_dependency(%q<rspec>, [">= 0"])
|
65
64
|
s.add_dependency(%q<rdoc>, [">= 0"])
|
66
65
|
s.add_dependency(%q<bundler>, [">= 0"])
|
data/lib/ar_default_values.rb
CHANGED
@@ -45,11 +45,15 @@ module ActiveRecord
|
|
45
45
|
define_method(:initialize_with_default_values) do |*attributes, &inner_block|
|
46
46
|
defaults = defaults.merge(block.call) if block_given?
|
47
47
|
defaults = Hash[defaults.each_pair.map{ |key, value| [key, value.kind_of?(Proc) ? value.call : value]}]
|
48
|
-
|
49
|
-
|
50
|
-
|
48
|
+
authorizer = self.mass_assignment_authorizer(nil)
|
49
|
+
|
50
|
+
parted_defaults = defaults.partition do |key, value|
|
51
|
+
authorizer.deny?(key)
|
51
52
|
end
|
52
53
|
|
54
|
+
sanitized_defaults = Hash[parted_defaults[1]]
|
55
|
+
protected_defaults = Hash[parted_defaults[0]]
|
56
|
+
|
53
57
|
attributes[0] = sanitized_defaults.merge(attributes.first || {})
|
54
58
|
initialize_without_default_values(*attributes, &inner_block)
|
55
59
|
protected_defaults.each_pair do |key, value|
|
@@ -8,56 +8,81 @@ end
|
|
8
8
|
|
9
9
|
class Book2 < ActiveRecord::Base
|
10
10
|
self.table_name = :books
|
11
|
-
|
12
|
-
default_values price: 0 do
|
11
|
+
attr_protected :edition
|
12
|
+
default_values price: 0, edition: 1 do
|
13
13
|
t = Time.now
|
14
14
|
{:released_at => t }
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
+
class Book3 < ActiveRecord::Base
|
19
|
+
self.table_name = :books
|
20
|
+
attr_accessible :price
|
21
|
+
default_values price: 0, edition: 1, released_at: -> { Time.now }
|
22
|
+
end
|
23
|
+
|
18
24
|
describe "ArDefaultValues" do
|
19
25
|
subject { @sample }
|
20
|
-
|
21
|
-
@now = Time.at(Time.now.to_i)
|
22
|
-
Time.stub(:now).and_return(@now)
|
23
|
-
end
|
24
|
-
context :without_attributes do
|
26
|
+
describe :fixed_default_values do
|
25
27
|
before do
|
26
|
-
@
|
28
|
+
@now = Time.at(Time.now.to_i)
|
29
|
+
Time.stub(:now).and_return(@now)
|
27
30
|
end
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
31
|
+
context :without_attributes do
|
32
|
+
before do
|
33
|
+
@sample = Book.new
|
34
|
+
end
|
35
|
+
its(:title) { should be_nil }
|
36
|
+
its(:price) { should == 0 }
|
37
|
+
its(:edition) { should == 1 }
|
38
|
+
its(:released_at) { should == @now }
|
39
|
+
end
|
40
|
+
context :with_attributes do
|
41
|
+
before do
|
42
|
+
@sample = Book.new(:price => 100)
|
43
|
+
end
|
44
|
+
its(:title) { should be_nil }
|
45
|
+
its(:price) { should == 100 }
|
46
|
+
its(:edition) { should == 1 }
|
47
|
+
its(:released_at) { should == @now }
|
48
|
+
end
|
49
|
+
context :with_block do
|
50
|
+
before do
|
51
|
+
@sample = Book.new do |t|
|
52
|
+
t.title = 'foo'
|
53
|
+
end
|
54
|
+
end
|
55
|
+
its(:title) { should == 'foo' }
|
56
|
+
its(:price) { should == 0 }
|
57
|
+
its(:edition) { should == 1 }
|
58
|
+
its(:released_at) { should == @now }
|
59
|
+
end
|
60
|
+
|
61
|
+
context :consider_attr_protected do
|
62
|
+
before do
|
63
|
+
@sample = Book.new(:edition => 3)
|
64
|
+
end
|
65
|
+
its(:title) { should be_nil }
|
66
|
+
its(:price) { should == 0 }
|
67
|
+
its(:edition) { should == 1 }
|
68
|
+
its(:released_at) { should == @now }
|
36
69
|
end
|
37
|
-
its(:title) { should be_nil }
|
38
|
-
its(:price) { should == 100 }
|
39
|
-
its(:edition) { should == 1 }
|
40
|
-
its(:released_at) { should == @now }
|
41
70
|
end
|
42
|
-
|
71
|
+
|
72
|
+
describe :instance_values_with_block do
|
43
73
|
before do
|
44
|
-
@sample =
|
45
|
-
|
46
|
-
|
74
|
+
@sample = Book2.new
|
75
|
+
sleep 1
|
76
|
+
@target = Book2.new
|
47
77
|
end
|
48
|
-
its(:
|
49
|
-
its(:price) { should == 0 }
|
50
|
-
its(:edition) { should == 1 }
|
51
|
-
its(:released_at) { should == @now }
|
78
|
+
its(:released_at) { should_not eq @target.released_at }
|
52
79
|
end
|
53
80
|
|
54
|
-
|
81
|
+
describe :both_attr_accessible_and_attr_protected do
|
55
82
|
before do
|
56
|
-
@sample =
|
83
|
+
@sample = Book3.new(:price => 150)
|
57
84
|
end
|
58
|
-
its(:
|
59
|
-
its(:
|
60
|
-
its(:edition) { should == 3 }
|
61
|
-
its(:released_at) { should == @now }
|
85
|
+
its(:price) { should == 150 }
|
86
|
+
its(:edition) { should == 1 }
|
62
87
|
end
|
63
88
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ar_default_values
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,33 +9,43 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-01-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version:
|
21
|
+
version: 3.0.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.0.0
|
25
30
|
- !ruby/object:Gem::Dependency
|
26
31
|
name: activerecord
|
27
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
28
33
|
none: false
|
29
34
|
requirements:
|
30
35
|
- - ! '>='
|
31
36
|
- !ruby/object:Gem::Version
|
32
|
-
version:
|
37
|
+
version: 3.0.0
|
33
38
|
type: :runtime
|
34
39
|
prerelease: false
|
35
|
-
version_requirements:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 3.0.0
|
36
46
|
- !ruby/object:Gem::Dependency
|
37
47
|
name: rspec
|
38
|
-
requirement:
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
39
49
|
none: false
|
40
50
|
requirements:
|
41
51
|
- - ! '>='
|
@@ -43,10 +53,15 @@ dependencies:
|
|
43
53
|
version: '0'
|
44
54
|
type: :development
|
45
55
|
prerelease: false
|
46
|
-
version_requirements:
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
47
62
|
- !ruby/object:Gem::Dependency
|
48
63
|
name: rdoc
|
49
|
-
requirement:
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
50
65
|
none: false
|
51
66
|
requirements:
|
52
67
|
- - ! '>='
|
@@ -54,10 +69,15 @@ dependencies:
|
|
54
69
|
version: '0'
|
55
70
|
type: :development
|
56
71
|
prerelease: false
|
57
|
-
version_requirements:
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
58
78
|
- !ruby/object:Gem::Dependency
|
59
79
|
name: bundler
|
60
|
-
requirement:
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
61
81
|
none: false
|
62
82
|
requirements:
|
63
83
|
- - ! '>='
|
@@ -65,10 +85,15 @@ dependencies:
|
|
65
85
|
version: '0'
|
66
86
|
type: :development
|
67
87
|
prerelease: false
|
68
|
-
version_requirements:
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
69
94
|
- !ruby/object:Gem::Dependency
|
70
95
|
name: jeweler
|
71
|
-
requirement:
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
72
97
|
none: false
|
73
98
|
requirements:
|
74
99
|
- - ! '>='
|
@@ -76,10 +101,15 @@ dependencies:
|
|
76
101
|
version: '0'
|
77
102
|
type: :development
|
78
103
|
prerelease: false
|
79
|
-
version_requirements:
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
80
110
|
- !ruby/object:Gem::Dependency
|
81
111
|
name: sqlite3
|
82
|
-
requirement:
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
83
113
|
none: false
|
84
114
|
requirements:
|
85
115
|
- - ! '>='
|
@@ -87,7 +117,12 @@ dependencies:
|
|
87
117
|
version: '0'
|
88
118
|
type: :development
|
89
119
|
prerelease: false
|
90
|
-
version_requirements:
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
91
126
|
description: ActiveRecord with default values
|
92
127
|
email: masaki@hisme.net
|
93
128
|
executables: []
|
@@ -99,7 +134,6 @@ files:
|
|
99
134
|
- .document
|
100
135
|
- .rspec
|
101
136
|
- Gemfile
|
102
|
-
- Gemfile.lock
|
103
137
|
- LICENSE.txt
|
104
138
|
- README.rdoc
|
105
139
|
- Rakefile
|
@@ -125,7 +159,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
125
159
|
version: '0'
|
126
160
|
segments:
|
127
161
|
- 0
|
128
|
-
hash:
|
162
|
+
hash: 1043313969
|
129
163
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
164
|
none: false
|
131
165
|
requirements:
|
@@ -134,7 +168,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
134
168
|
version: '0'
|
135
169
|
requirements: []
|
136
170
|
rubyforge_project:
|
137
|
-
rubygems_version: 1.8.
|
171
|
+
rubygems_version: 1.8.24
|
138
172
|
signing_key:
|
139
173
|
specification_version: 3
|
140
174
|
summary: ActiveRecord with default values
|
data/Gemfile.lock
DELETED
@@ -1,51 +0,0 @@
|
|
1
|
-
GEM
|
2
|
-
remote: http://rubygems.org/
|
3
|
-
specs:
|
4
|
-
activemodel (3.2.2)
|
5
|
-
activesupport (= 3.2.2)
|
6
|
-
builder (~> 3.0.0)
|
7
|
-
activerecord (3.2.2)
|
8
|
-
activemodel (= 3.2.2)
|
9
|
-
activesupport (= 3.2.2)
|
10
|
-
arel (~> 3.0.2)
|
11
|
-
tzinfo (~> 0.3.29)
|
12
|
-
activesupport (3.2.2)
|
13
|
-
i18n (~> 0.6)
|
14
|
-
multi_json (~> 1.0)
|
15
|
-
arel (3.0.2)
|
16
|
-
builder (3.0.0)
|
17
|
-
diff-lcs (1.1.3)
|
18
|
-
git (1.2.5)
|
19
|
-
i18n (0.6.0)
|
20
|
-
jeweler (1.8.3)
|
21
|
-
bundler (~> 1.0)
|
22
|
-
git (>= 1.2.5)
|
23
|
-
rake
|
24
|
-
rdoc
|
25
|
-
json (1.6.5)
|
26
|
-
multi_json (1.1.0)
|
27
|
-
rake (0.9.2.2)
|
28
|
-
rdoc (3.12)
|
29
|
-
json (~> 1.4)
|
30
|
-
rspec (2.9.0)
|
31
|
-
rspec-core (~> 2.9.0)
|
32
|
-
rspec-expectations (~> 2.9.0)
|
33
|
-
rspec-mocks (~> 2.9.0)
|
34
|
-
rspec-core (2.9.0)
|
35
|
-
rspec-expectations (2.9.0)
|
36
|
-
diff-lcs (~> 1.1.3)
|
37
|
-
rspec-mocks (2.9.0)
|
38
|
-
sqlite3 (1.3.5)
|
39
|
-
tzinfo (0.3.32)
|
40
|
-
|
41
|
-
PLATFORMS
|
42
|
-
ruby
|
43
|
-
|
44
|
-
DEPENDENCIES
|
45
|
-
activerecord
|
46
|
-
activesupport
|
47
|
-
bundler
|
48
|
-
jeweler
|
49
|
-
rdoc
|
50
|
-
rspec
|
51
|
-
sqlite3
|