simple_form_object 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/simple_form_object/version.rb +1 -1
- data/lib/simple_form_object.rb +9 -1
- data/spec/attribute_spec.rb +18 -0
- 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: 73485885f010300cc9a362e85612e3f279f52926
|
4
|
+
data.tar.gz: 908ce72ef544b469e02cbd9328065f0a18a28b4d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f761ced4db494a667e2734d2fe591c0e5756ad545ad9d2eab0adc6ecd73c8cf4aff623600484e90e42be89099f207c8c2b593f41af74872752d70a9bf2f68b40
|
7
|
+
data.tar.gz: 5d819b5438d3d7caccddbf3231b3cc9e4ec2715712a532077547c2619340de44ba553226ef9a0c50a01ecf3afe721f65351172034601064d976fba935a32b1b5
|
data/lib/simple_form_object.rb
CHANGED
@@ -102,12 +102,20 @@ module SimpleFormObject
|
|
102
102
|
|
103
103
|
def apply_default_to(form)
|
104
104
|
if form.send(@name).nil?
|
105
|
-
form.send("#{@name}=",
|
105
|
+
form.send("#{@name}=", default_value(form)) if @apply_default
|
106
106
|
end
|
107
107
|
end
|
108
108
|
|
109
109
|
private
|
110
110
|
|
111
|
+
def default_value(context)
|
112
|
+
if @default.respond_to?(:call)
|
113
|
+
context.instance_eval(&@default)
|
114
|
+
else
|
115
|
+
@default
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
111
119
|
def extract_options
|
112
120
|
@apply_default = true
|
113
121
|
@default = options.fetch(:default) { @apply_default = false; nil }
|
data/spec/attribute_spec.rb
CHANGED
@@ -54,5 +54,23 @@ describe SimpleFormObject::Attribute do
|
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
|
+
context 'with a lambda/proc default' do
|
58
|
+
let(:default) { Proc.new { :bar } }
|
59
|
+
|
60
|
+
it 'calls the lambda' do
|
61
|
+
expect(form).to receive("#{name}=").with(:bar)
|
62
|
+
attribute.apply_default_to(form)
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'when the proc references the form' do
|
66
|
+
let(:default) { Proc.new { current_foo } }
|
67
|
+
let(:form) { double('Form', foo: nil, current_foo: :bar) }
|
68
|
+
|
69
|
+
it 'calls the lambda' do
|
70
|
+
expect(form).to receive("#{name}=").with(:bar)
|
71
|
+
attribute.apply_default_to(form)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
57
75
|
end
|
58
76
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_form_object
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Leonard Garvey
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|