active_record-default_values 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -2
- data/lib/active_record/default_values/version.rb +1 -1
- data/lib/active_record/default_values.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2ca922faefc937af54e3560c52fb03b3f4393c02
|
4
|
+
data.tar.gz: 88d8656de39c33a46a672aa2fd959a8adb3a3f44
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7cf4ff9300351b771a5ea19118b4650be518a77225a44dd6d3ac15b8c877113b6b06144e6aeb479b59d52ab6ab6f1267a7e06b239a664addcf5b1f468bc2b60d
|
7
|
+
data.tar.gz: 97302ee6c20ad98757e44d070e5fbcb5e6d81dae72d9352b9c7fcc538cd8f2562ecf42f10c9a0a0828598b76444788874967b63f17fad139db285c176b0192ff
|
data/README.md
CHANGED
@@ -27,11 +27,12 @@ class User < ActiveRecord::Base
|
|
27
27
|
default_value :active, false
|
28
28
|
default_value :add_time, -> { Time.now }
|
29
29
|
default_value :age, 20
|
30
|
+
default_value :description, 'Test', if: :blank?
|
30
31
|
end
|
31
32
|
|
32
|
-
User.new(first_name: 'Samwise')
|
33
|
+
User.new(first_name: 'Samwise', description: '')
|
33
34
|
|
34
|
-
=> #<User id: nil, first_name: "Samwise", last_name: "Doe", add_time: "2015-05-27 15:35:32", active: false, age: 20>
|
35
|
+
=> #<User id: nil, first_name: "Samwise", last_name: "Doe", add_time: "2015-05-27 15:35:32", active: false, age: 20, description: "Test">
|
35
36
|
```
|
36
37
|
|
37
38
|
## Contributing
|
@@ -6,10 +6,10 @@ module ActiveRecord
|
|
6
6
|
extend ActiveSupport::Concern
|
7
7
|
|
8
8
|
class_methods do
|
9
|
-
def default_value(attr_name, default_value)
|
9
|
+
def default_value(attr_name, default_value, **options)
|
10
10
|
after_initialize do
|
11
11
|
value = default_value.respond_to?(:call) ? default_value.call : default_value
|
12
|
-
send("#{attr_name}=", value) if send(attr_name).nil? && new_record?
|
12
|
+
send("#{attr_name}=", value) if send(attr_name).send(options[:if] || :nil?) && new_record?
|
13
13
|
end
|
14
14
|
end
|
15
15
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record-default_values
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Damian Baćkowski
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-06-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|