ar_default_values 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -1
- data/Gemfile +3 -0
- data/ar_default_values.gemspec +2 -2
- data/lib/ar_default_values/version.rb +1 -1
- data/lib/ar_default_values.rb +3 -11
- data/spec/ar_default_values_spec.rb +18 -17
- data/spec/spec_helper.rb +1 -0
- metadata +6 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5e377838e48ccad8237c57909a67c15cc5e43223
|
4
|
+
data.tar.gz: 8ac4024d5e11a96f357f4ba25c20f4b071338bbf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1ebfcad27fdbf3b80067629dc4ab04c54bf59ab5167b1a8755356e67ebf17ebc9b3b8053a827258f929fbb06cbc4e90ec5393f750bba8c46db1cc0d3945ba607
|
7
|
+
data.tar.gz: a8d0969fee54980e33627d707b577c70ca0fe10ff2e619012b40a7ae084fe2e8f0c9ba3afd82f16ffce1dd7686f587c6b29269d36d3d32a4bafcc7894e04a09e
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/ar_default_values.gemspec
CHANGED
@@ -18,8 +18,8 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_runtime_dependency "activerecord", ">= 3.
|
22
|
-
spec.add_runtime_dependency "activesupport", ">= 3.
|
21
|
+
spec.add_runtime_dependency "activerecord", ">= 3.2"
|
22
|
+
spec.add_runtime_dependency "activesupport", ">= 3.2"
|
23
23
|
spec.add_development_dependency "bundler", "~> 1.3"
|
24
24
|
spec.add_development_dependency "rake"
|
25
25
|
end
|
data/lib/ar_default_values.rb
CHANGED
@@ -45,19 +45,11 @@ module DefaultValues
|
|
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
|
-
authorizer = self.mass_assignment_authorizer(nil)
|
49
48
|
|
50
|
-
parted_defaults = defaults.partition do |key, value|
|
51
|
-
authorizer.deny?(key)
|
52
|
-
end
|
53
|
-
|
54
|
-
sanitized_defaults = Hash[parted_defaults[1]]
|
55
|
-
protected_defaults = Hash[parted_defaults[0]]
|
56
|
-
|
57
|
-
attributes[0] = sanitized_defaults.merge(attributes.first || {})
|
58
49
|
initialize_without_default_values(*attributes, &inner_block)
|
59
|
-
|
60
|
-
|
50
|
+
|
51
|
+
defaults.each_pair do |key, value|
|
52
|
+
write_attribute(key, value) unless attribute_present?(key)
|
61
53
|
end
|
62
54
|
end
|
63
55
|
|
@@ -1,14 +1,14 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
3
|
class Book < ActiveRecord::Base
|
4
|
-
attr_protected :edition
|
4
|
+
attr_protected :edition if Rails.version.to_i < 4
|
5
5
|
|
6
6
|
default_values price: 0, edition: 1, released_at: lambda { Time.now }
|
7
7
|
end
|
8
8
|
|
9
9
|
class Book2 < ActiveRecord::Base
|
10
10
|
self.table_name = :books
|
11
|
-
attr_protected :edition
|
11
|
+
attr_protected :edition if Rails.version.to_i < 4
|
12
12
|
default_values price: 0, edition: 1 do
|
13
13
|
t = Time.now
|
14
14
|
{:released_at => t }
|
@@ -17,17 +17,16 @@ end
|
|
17
17
|
|
18
18
|
class Book3 < ActiveRecord::Base
|
19
19
|
self.table_name = :books
|
20
|
-
attr_accessible :price
|
20
|
+
attr_accessible :price if Rails.version.to_i < 4
|
21
21
|
default_values price: 0, edition: 1, released_at: -> { Time.now }
|
22
22
|
end
|
23
23
|
|
24
24
|
describe "ArDefaultValues" do
|
25
|
+
let!(:now) { Time.at(Time.now.to_i) }
|
26
|
+
|
25
27
|
subject { @sample }
|
26
28
|
describe :fixed_default_values do
|
27
|
-
before
|
28
|
-
@now = Time.at(Time.now.to_i)
|
29
|
-
Time.stub(:now).and_return(@now)
|
30
|
-
end
|
29
|
+
before { Time.stub(:now) { now } }
|
31
30
|
context :without_attributes do
|
32
31
|
before do
|
33
32
|
@sample = Book.new
|
@@ -35,7 +34,7 @@ describe "ArDefaultValues" do
|
|
35
34
|
its(:title) { should be_nil }
|
36
35
|
its(:price) { should == 0 }
|
37
36
|
its(:edition) { should == 1 }
|
38
|
-
its(:released_at) { should ==
|
37
|
+
its(:released_at) { should == now }
|
39
38
|
end
|
40
39
|
context :with_attributes do
|
41
40
|
before do
|
@@ -44,7 +43,7 @@ describe "ArDefaultValues" do
|
|
44
43
|
its(:title) { should be_nil }
|
45
44
|
its(:price) { should == 100 }
|
46
45
|
its(:edition) { should == 1 }
|
47
|
-
its(:released_at) { should ==
|
46
|
+
its(:released_at) { should == now }
|
48
47
|
end
|
49
48
|
context :with_block do
|
50
49
|
before do
|
@@ -55,17 +54,19 @@ describe "ArDefaultValues" do
|
|
55
54
|
its(:title) { should == 'foo' }
|
56
55
|
its(:price) { should == 0 }
|
57
56
|
its(:edition) { should == 1 }
|
58
|
-
its(:released_at) { should ==
|
57
|
+
its(:released_at) { should == now }
|
59
58
|
end
|
60
59
|
|
61
|
-
|
62
|
-
|
63
|
-
|
60
|
+
if Rails.version.to_i < 4
|
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 }
|
64
69
|
end
|
65
|
-
its(:title) { should be_nil }
|
66
|
-
its(:price) { should == 0 }
|
67
|
-
its(:edition) { should == 1 }
|
68
|
-
its(:released_at) { should == @now }
|
69
70
|
end
|
70
71
|
end
|
71
72
|
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- masarakki
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
11
|
+
date: 2013-10-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,40 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - '>='
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 3.
|
20
|
-
- - <
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version: '4'
|
19
|
+
version: '3.2'
|
23
20
|
type: :runtime
|
24
21
|
prerelease: false
|
25
22
|
version_requirements: !ruby/object:Gem::Requirement
|
26
23
|
requirements:
|
27
24
|
- - '>='
|
28
25
|
- !ruby/object:Gem::Version
|
29
|
-
version: 3.
|
30
|
-
- - <
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
version: '4'
|
26
|
+
version: '3.2'
|
33
27
|
- !ruby/object:Gem::Dependency
|
34
28
|
name: activesupport
|
35
29
|
requirement: !ruby/object:Gem::Requirement
|
36
30
|
requirements:
|
37
31
|
- - '>='
|
38
32
|
- !ruby/object:Gem::Version
|
39
|
-
version: 3.
|
40
|
-
- - <
|
41
|
-
- !ruby/object:Gem::Version
|
42
|
-
version: '4'
|
33
|
+
version: '3.2'
|
43
34
|
type: :runtime
|
44
35
|
prerelease: false
|
45
36
|
version_requirements: !ruby/object:Gem::Requirement
|
46
37
|
requirements:
|
47
38
|
- - '>='
|
48
39
|
- !ruby/object:Gem::Version
|
49
|
-
version: 3.
|
50
|
-
- - <
|
51
|
-
- !ruby/object:Gem::Version
|
52
|
-
version: '4'
|
40
|
+
version: '3.2'
|
53
41
|
- !ruby/object:Gem::Dependency
|
54
42
|
name: bundler
|
55
43
|
requirement: !ruby/object:Gem::Requirement
|