attribute_defaults 0.2 → 0.3
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/Gemfile +1 -1
- data/Gemfile.lock +19 -17
- data/attribute_defaults.gemspec +2 -2
- data/lib/attribute_defaults.rb +38 -40
- data/lib/attribute_defaults/version.rb +1 -1
- data/spec/spec_helper.rb +1 -0
- metadata +14 -15
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -2,26 +2,28 @@ PATH
|
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
4
|
attribute_defaults (0.2)
|
5
|
-
activerecord (>= 3)
|
5
|
+
activerecord (>= 3.1.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: http://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activemodel (3.
|
11
|
-
activesupport (= 3.
|
12
|
-
builder (~>
|
13
|
-
i18n (~> 0.
|
14
|
-
activerecord (3.
|
15
|
-
activemodel (= 3.
|
16
|
-
activesupport (= 3.
|
17
|
-
arel (~> 2.
|
18
|
-
tzinfo (~> 0.3.
|
19
|
-
activesupport (3.
|
20
|
-
|
21
|
-
|
10
|
+
activemodel (3.1.3)
|
11
|
+
activesupport (= 3.1.3)
|
12
|
+
builder (~> 3.0.0)
|
13
|
+
i18n (~> 0.6)
|
14
|
+
activerecord (3.1.3)
|
15
|
+
activemodel (= 3.1.3)
|
16
|
+
activesupport (= 3.1.3)
|
17
|
+
arel (~> 2.2.1)
|
18
|
+
tzinfo (~> 0.3.29)
|
19
|
+
activesupport (3.1.3)
|
20
|
+
multi_json (~> 1.0)
|
21
|
+
arel (2.2.1)
|
22
|
+
builder (3.0.0)
|
22
23
|
diff-lcs (1.1.2)
|
23
|
-
i18n (0.
|
24
|
-
|
24
|
+
i18n (0.6.0)
|
25
|
+
multi_json (1.0.4)
|
26
|
+
mysql2 (0.3.11)
|
25
27
|
rspec (2.6.0)
|
26
28
|
rspec-core (~> 2.6.0)
|
27
29
|
rspec-expectations (~> 2.6.0)
|
@@ -30,12 +32,12 @@ GEM
|
|
30
32
|
rspec-expectations (2.6.0)
|
31
33
|
diff-lcs (~> 1.1.2)
|
32
34
|
rspec-mocks (2.6.0)
|
33
|
-
tzinfo (0.3.
|
35
|
+
tzinfo (0.3.31)
|
34
36
|
|
35
37
|
PLATFORMS
|
36
38
|
ruby
|
37
39
|
|
38
40
|
DEPENDENCIES
|
39
41
|
attribute_defaults!
|
40
|
-
mysql2
|
42
|
+
mysql2
|
41
43
|
rspec
|
data/attribute_defaults.gemspec
CHANGED
@@ -10,8 +10,8 @@ Gem::Specification.new do |s|
|
|
10
10
|
s.files = `git ls-files`.split("\n")
|
11
11
|
s.homepage = "http://github.com/jviney/attribute_defaults"
|
12
12
|
|
13
|
-
s.add_dependency "activerecord", ">= 3"
|
13
|
+
s.add_dependency "activerecord", ">= 3.1.0"
|
14
14
|
|
15
15
|
s.add_development_dependency "rspec"
|
16
|
-
s.add_development_dependency "mysql2"
|
16
|
+
s.add_development_dependency "mysql2"
|
17
17
|
end
|
data/lib/attribute_defaults.rb
CHANGED
@@ -82,51 +82,49 @@ module AttributeDefaults
|
|
82
82
|
|
83
83
|
alias_method :default, :defaults
|
84
84
|
end
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
yield record if block_given?
|
92
|
-
end
|
85
|
+
|
86
|
+
if ActiveRecord::VERSION::STRING >= "3.1"
|
87
|
+
def initialize_with_defaults(attributes = nil, options = {})
|
88
|
+
initialize_without_defaults(attributes, options) do |record|
|
89
|
+
record.apply_default_attribute_values(attributes)
|
90
|
+
yield record if block_given?
|
93
91
|
end
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
92
|
+
end
|
93
|
+
else
|
94
|
+
def initialize_with_defaults(attributes = nil)
|
95
|
+
initialize_without_defaults(attributes) do |record|
|
96
|
+
record.apply_default_attribute_values(attributes)
|
97
|
+
yield record if block_given?
|
100
98
|
end
|
101
99
|
end
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
100
|
+
end
|
101
|
+
|
102
|
+
def apply_default_attribute_values(specific_attributes)
|
103
|
+
specific_attributes = (specific_attributes || {}).stringify_keys
|
104
|
+
|
105
|
+
# Rails 3.1 deprecates #primary_key_name in favour of :foreign_key
|
106
|
+
foreign_key_method = if ActiveRecord::VERSION::STRING >= "3.1"
|
107
|
+
:foreign_key
|
108
|
+
else
|
109
|
+
:primary_key_name
|
110
|
+
end
|
111
|
+
|
112
|
+
self.class.attribute_defaults.each do |default|
|
113
|
+
next if specific_attributes.include?(default.attribute)
|
114
|
+
|
115
|
+
# Ignore a default value for association_id if association has been specified
|
116
|
+
reflection = self.class.reflections[default.attribute.to_sym]
|
117
|
+
if reflection and reflection.macro == :belongs_to and specific_attributes.include?(reflection.send(foreign_key_method).to_s)
|
118
|
+
next
|
111
119
|
end
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
reflection = self.class.reflections[default.attribute.to_sym]
|
118
|
-
if reflection and reflection.macro == :belongs_to and specific_attributes.include?(reflection.send(foreign_key_method).to_s)
|
119
|
-
next
|
120
|
-
end
|
121
|
-
|
122
|
-
# Ignore a default value for association if association_id has been specified
|
123
|
-
reflection = self.class.reflections.values.find { |r| r.macro == :belongs_to && r.send(foreign_key_method).to_s == default.attribute }
|
124
|
-
if reflection and specific_attributes.include?(reflection.name.to_s)
|
125
|
-
next
|
126
|
-
end
|
127
|
-
|
128
|
-
send("#{default.attribute}=", default.value(self))
|
120
|
+
|
121
|
+
# Ignore a default value for association if association_id has been specified
|
122
|
+
reflection = self.class.reflections.values.find { |r| r.macro == :belongs_to && r.send(foreign_key_method).to_s == default.attribute }
|
123
|
+
if reflection and specific_attributes.include?(reflection.name.to_s)
|
124
|
+
next
|
129
125
|
end
|
126
|
+
|
127
|
+
send("#{default.attribute}=", default.value(self))
|
130
128
|
end
|
131
129
|
end
|
132
130
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attribute_defaults
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 13
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: "0.
|
8
|
+
- 3
|
9
|
+
version: "0.3"
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Jonathan Viney
|
@@ -14,8 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date:
|
18
|
-
default_executable:
|
17
|
+
date: 2012-01-03 00:00:00 Z
|
19
18
|
dependencies:
|
20
19
|
- !ruby/object:Gem::Dependency
|
21
20
|
name: activerecord
|
@@ -25,10 +24,12 @@ dependencies:
|
|
25
24
|
requirements:
|
26
25
|
- - ">="
|
27
26
|
- !ruby/object:Gem::Version
|
28
|
-
hash:
|
27
|
+
hash: 3
|
29
28
|
segments:
|
30
29
|
- 3
|
31
|
-
|
30
|
+
- 1
|
31
|
+
- 0
|
32
|
+
version: 3.1.0
|
32
33
|
type: :runtime
|
33
34
|
version_requirements: *id001
|
34
35
|
- !ruby/object:Gem::Dependency
|
@@ -51,13 +52,12 @@ dependencies:
|
|
51
52
|
requirement: &id003 !ruby/object:Gem::Requirement
|
52
53
|
none: false
|
53
54
|
requirements:
|
54
|
-
- -
|
55
|
+
- - ">="
|
55
56
|
- !ruby/object:Gem::Version
|
56
|
-
hash:
|
57
|
+
hash: 3
|
57
58
|
segments:
|
58
59
|
- 0
|
59
|
-
|
60
|
-
version: "0.3"
|
60
|
+
version: "0"
|
61
61
|
type: :development
|
62
62
|
version_requirements: *id003
|
63
63
|
description: Add default attribute values when creating models.
|
@@ -82,7 +82,6 @@ files:
|
|
82
82
|
- spec/database.yml
|
83
83
|
- spec/schema.rb
|
84
84
|
- spec/spec_helper.rb
|
85
|
-
has_rdoc: true
|
86
85
|
homepage: http://github.com/jviney/attribute_defaults
|
87
86
|
licenses: []
|
88
87
|
|
@@ -112,9 +111,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
112
111
|
requirements: []
|
113
112
|
|
114
113
|
rubyforge_project:
|
115
|
-
rubygems_version: 1.
|
114
|
+
rubygems_version: 1.8.10
|
116
115
|
signing_key:
|
117
116
|
specification_version: 3
|
118
|
-
summary: attribute_defaults-0.
|
117
|
+
summary: attribute_defaults-0.3
|
119
118
|
test_files: []
|
120
119
|
|