activerecord-safe_initialize 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -13
- data/.gitignore +1 -0
- data/.travis.yml +17 -0
- data/Gemfile +0 -1
- data/README.md +8 -0
- data/gemfiles/Gemfile.rails-3.1 +5 -0
- data/gemfiles/Gemfile.rails-3.2 +5 -0
- data/gemfiles/Gemfile.rails-4.0 +5 -0
- data/gemfiles/Gemfile.rails-4.1 +5 -0
- data/gemfiles/Gemfile.rails-4.2 +5 -0
- data/gemfiles/Gemfile.rails-edge +6 -0
- data/lib/active_record/safe_initialize/version.rb +1 -1
- data/lib/active_record/safe_initialize.rb +10 -6
- data/spec/active_record/safe_initialize_spec.rb +13 -0
- metadata +24 -18
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
NTkyOWU3OWJiZTk5ZDk4NzcxYmM2MjM4ZjA3YzAyMDRmZjk3MzdmYg==
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a817b8badef4a8323d46f2b56f5a3d9d1e4425c6
|
4
|
+
data.tar.gz: 7a97aa0476ede030e0278b5a5a8c8a897ef2e89a
|
7
5
|
SHA512:
|
8
|
-
metadata.gz:
|
9
|
-
|
10
|
-
YjYwMzllYzk0MGQwNDA2NjRkMmYzMTUwOGM5YWVhYjAxMjFkNjJiNmQ1YjM1
|
11
|
-
M2I3ODNhY2ZjN2QxNDkzNDJkMzA0MTVkMTNkNmQwNDg4NTdiODU=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
NTllYmQzNmU5MmU3MGJmYzU1N2I2ODAzNmJlNWEwNTJiYmJjMjdiYTIwYjU0
|
14
|
-
Y2JkZWRiNzc2ZmFjNzhlMjI5YjhkMjM0YTc5ZjZjYmIxNDM2YWM5OTM5OWE0
|
15
|
-
OWY1MjIyNTM3ZDFhMGE3Yjk2MGVkY2QxODk2NTViYWU2ZGQ2ZDE=
|
6
|
+
metadata.gz: 018e2cc671635c149e20519d257818f94e601d9a84c774fce04a652ef6f6a8b00877eafc7625e2a4973e43aa9661f6258fa488e12c0bf40a5a13a4bb22510a62
|
7
|
+
data.tar.gz: 22fa1c3518284b18b80f97c404a80ea152c8e2609997ccb8beb3cfed5e05a39228eed4d92a613232f8b03482881a0f3777752f0e4421af55916af9e15e512b9f
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -1,4 +1,21 @@
|
|
1
1
|
language: ruby
|
2
2
|
rvm:
|
3
3
|
- 1.9.3
|
4
|
+
- 2.0.0
|
5
|
+
- 2.1
|
4
6
|
- 2.2
|
7
|
+
|
8
|
+
gemfile:
|
9
|
+
- Gemfile
|
10
|
+
- gemfiles/Gemfile.rails-4.1
|
11
|
+
- gemfiles/Gemfile.rails-4.0
|
12
|
+
- gemfiles/Gemfile.rails-3.2
|
13
|
+
|
14
|
+
matrix:
|
15
|
+
include:
|
16
|
+
- rvm: 1.9.3
|
17
|
+
gemfile: gemfiles/Gemfile.rails-3.1
|
18
|
+
- rvm: 2.2
|
19
|
+
gemfile: gemfiles/Gemfile.rails-edge
|
20
|
+
allow_failures:
|
21
|
+
- gemfile: gemfiles/Gemfile.rails-edge
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -69,6 +69,14 @@ class Post < ActiveRecord::Base
|
|
69
69
|
end
|
70
70
|
```
|
71
71
|
|
72
|
+
You can set a default for multiple attributes at once:
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
class Employee < ActiveRecord::Base
|
76
|
+
safe_initialize :pay_rate, :holiday_rate, with: 0.0
|
77
|
+
end
|
78
|
+
```
|
79
|
+
|
72
80
|
## Contributing
|
73
81
|
|
74
82
|
1. Fork it ( https://github.com/lyconic/activerecord-safe_initialize/fork )
|
@@ -1,18 +1,22 @@
|
|
1
1
|
require "active_record"
|
2
2
|
require "active_record/safe_initialize/version"
|
3
|
+
require 'active_support/core_ext/array/extract_options'
|
3
4
|
|
4
5
|
module ActiveRecord
|
5
6
|
module SafeInitialize
|
6
|
-
def safe_initialize(
|
7
|
+
def safe_initialize(*attributes)
|
8
|
+
options = attributes.extract_options!
|
7
9
|
raise ArgumentError, "Missing initialization value" unless options[:with]
|
8
10
|
|
9
11
|
after_initialize do
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
12
|
+
attributes.each do |attribute|
|
13
|
+
if has_attribute?(attribute) && read_attribute(attribute).nil?
|
14
|
+
value = options[:with]
|
15
|
+
value = instance_exec(&value) if value.respond_to?(:call)
|
16
|
+
value = self.send(value) if value.is_a?(Symbol)
|
14
17
|
|
15
|
-
|
18
|
+
self.send "#{attribute}=", value
|
19
|
+
end
|
16
20
|
end
|
17
21
|
end
|
18
22
|
end
|
@@ -49,4 +49,17 @@ describe ActiveRecord::SafeInitialize do
|
|
49
49
|
expect( klass.first.uuid ).to eq uuid
|
50
50
|
end
|
51
51
|
end
|
52
|
+
|
53
|
+
context "multiple attributes" do
|
54
|
+
it "creates a default for each one" do
|
55
|
+
klass = Class.new(Post)
|
56
|
+
|
57
|
+
expect{ klass.safe_initialize :title, :body, :uuid, with: 'foo' }.to_not raise_error
|
58
|
+
|
59
|
+
inst = klass.new
|
60
|
+
expect( inst.title ).to eq 'foo'
|
61
|
+
expect( inst.body ).to eq 'foo'
|
62
|
+
expect( inst.uuid ).to eq 'foo'
|
63
|
+
end
|
64
|
+
end
|
52
65
|
end
|
metadata
CHANGED
@@ -1,83 +1,83 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-safe_initialize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Lassek
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-09-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '3.1'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '3.1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - ~>
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '1.7'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - ~>
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '1.7'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - ~>
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '10.0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - ~>
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '10.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - ~>
|
59
|
+
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '3.3'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - ~>
|
66
|
+
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '3.3'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: sqlite3
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
description:
|
@@ -87,14 +87,20 @@ executables: []
|
|
87
87
|
extensions: []
|
88
88
|
extra_rdoc_files: []
|
89
89
|
files:
|
90
|
-
- .gitignore
|
91
|
-
- .rspec
|
92
|
-
- .travis.yml
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
93
|
- Gemfile
|
94
94
|
- LICENSE.txt
|
95
95
|
- README.md
|
96
96
|
- Rakefile
|
97
97
|
- activerecord-safe_initialize.gemspec
|
98
|
+
- gemfiles/Gemfile.rails-3.1
|
99
|
+
- gemfiles/Gemfile.rails-3.2
|
100
|
+
- gemfiles/Gemfile.rails-4.0
|
101
|
+
- gemfiles/Gemfile.rails-4.1
|
102
|
+
- gemfiles/Gemfile.rails-4.2
|
103
|
+
- gemfiles/Gemfile.rails-edge
|
98
104
|
- lib/active_record/safe_initialize.rb
|
99
105
|
- lib/active_record/safe_initialize/version.rb
|
100
106
|
- lib/activerecord-safe_initialize.rb
|
@@ -111,17 +117,17 @@ require_paths:
|
|
111
117
|
- lib
|
112
118
|
required_ruby_version: !ruby/object:Gem::Requirement
|
113
119
|
requirements:
|
114
|
-
- -
|
120
|
+
- - ">="
|
115
121
|
- !ruby/object:Gem::Version
|
116
122
|
version: '0'
|
117
123
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
124
|
requirements:
|
119
|
-
- -
|
125
|
+
- - ">="
|
120
126
|
- !ruby/object:Gem::Version
|
121
127
|
version: '0'
|
122
128
|
requirements: []
|
123
129
|
rubyforge_project:
|
124
|
-
rubygems_version: 2.4.
|
130
|
+
rubygems_version: 2.4.6
|
125
131
|
signing_key:
|
126
132
|
specification_version: 4
|
127
133
|
summary: Safely initialize an ActiveRecord attribute with respect to missing columns
|