simple_form_object 0.0.3 → 0.0.4
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.rb +6 -4
- data/lib/simple_form_object/version.rb +1 -1
- data/simple_form_objects.gemspec +1 -1
- data/spec/attribute_spec.rb +58 -0
- data/spec/model_spec.rb +19 -44
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1fbf783ca89d65945dbdeac23091e2b8609e7d31
|
4
|
+
data.tar.gz: 7e4049380a3fd366d256921c8ba53211bfbcdcd4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ccceb3c72c942e845e37ca67800fd60c216361d281faa1c18ab8acc68c4111c1e39e2946b141095772d9eaac33ab61e49fa371e0fd5f33090bd8ce40fb16897a
|
7
|
+
data.tar.gz: 9575382b6d1f35cc090ca179b3911182ad31ee387012ae3193dd9ff82cc6be4a48ea4d9f746f19c0a27dcd0489b59600a341a916446e83016fb781b2b536ce00
|
data/lib/simple_form_object.rb
CHANGED
@@ -39,9 +39,9 @@ module SimpleFormObject
|
|
39
39
|
end
|
40
40
|
|
41
41
|
class Attribute
|
42
|
-
def initialize(name, type, options)
|
42
|
+
def initialize(name, type = nil, options)
|
43
43
|
@name = name
|
44
|
-
@type = type
|
44
|
+
@type = type || :string
|
45
45
|
@options = options
|
46
46
|
|
47
47
|
extract_options
|
@@ -55,14 +55,16 @@ module SimpleFormObject
|
|
55
55
|
|
56
56
|
def apply_default_to(form)
|
57
57
|
if form.send(@name).nil?
|
58
|
-
form.send("#{@name}=", @default) if @
|
58
|
+
form.send("#{@name}=", @default) if @apply_default
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
62
|
private
|
63
63
|
|
64
64
|
def extract_options
|
65
|
-
@
|
65
|
+
@apply_default = true
|
66
|
+
@default = options.fetch(:default) { @apply_default = false; nil }
|
67
|
+
@skip_validations = options.fetch(:skip_validations, false)
|
66
68
|
end
|
67
69
|
end
|
68
70
|
|
data/simple_form_objects.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["lengarvey@gmail.com"]
|
11
11
|
spec.summary = %q{Simple form objects for simple form and rails}
|
12
12
|
spec.description = %q{Very simple form objects for simple form and rails}
|
13
|
-
spec.homepage = "http://github.com/
|
13
|
+
spec.homepage = "http://github.com/reinteractive-open/simple_form_object"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SimpleFormObject::Attribute do
|
4
|
+
describe 'initialization' do
|
5
|
+
subject(:attribute) { SimpleFormObject::Attribute.new(name, type, options) }
|
6
|
+
|
7
|
+
let(:name) { :foo }
|
8
|
+
let(:type) { :boolean }
|
9
|
+
let(:options) { { default: 'hello' } }
|
10
|
+
|
11
|
+
it 'should be initialized with a name, type and options hash' do
|
12
|
+
expect(attribute.name).to eq name
|
13
|
+
expect(attribute.type).to eq type
|
14
|
+
expect(attribute.options).to eq options
|
15
|
+
end
|
16
|
+
|
17
|
+
describe 'type' do
|
18
|
+
let(:type) { nil }
|
19
|
+
|
20
|
+
it 'is :string when nil or not provided' do
|
21
|
+
expect(attribute.type).to eq :string
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '#apply_default_to' do
|
27
|
+
let(:name) { :foo }
|
28
|
+
let(:type) { :boolean }
|
29
|
+
let(:options) { { default: default } }
|
30
|
+
let(:default) { true }
|
31
|
+
let(:attribute) { SimpleFormObject::Attribute.new(name, type, options) }
|
32
|
+
let(:form) { double(foo: nil) }
|
33
|
+
|
34
|
+
it 'should apply the default value to the attribute on the object' do
|
35
|
+
expect(form).to receive("#{name}=").with(default)
|
36
|
+
attribute.apply_default_to(form)
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'with a falsey default' do
|
40
|
+
let(:default) { false }
|
41
|
+
it 'should apply the default value to the attribute on the object' do
|
42
|
+
expect(form).to receive("#{name}=").with(default)
|
43
|
+
attribute.apply_default_to(form)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'when the form object attribute has a value' do
|
48
|
+
let(:form) { double(foo: false) }
|
49
|
+
|
50
|
+
it 'should not apply the default' do
|
51
|
+
expect(form).to_not receive("#{name}=").with(default)
|
52
|
+
attribute.apply_default_to(form)
|
53
|
+
expect(form.foo).to eq false
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
data/spec/model_spec.rb
CHANGED
@@ -1,15 +1,11 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe '
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
include SimpleFormObject
|
8
|
-
end
|
9
|
-
|
10
|
-
Klass.new
|
11
|
-
end
|
3
|
+
describe 'SimpleFormObject' do
|
4
|
+
let(:base_klass) { Class.new.tap{|k| k.include SimpleFormObject } }
|
5
|
+
let(:klass) { base_klass }
|
6
|
+
let(:instance) { klass.new }
|
12
7
|
|
8
|
+
context 'when included' do
|
13
9
|
it 'should have ActiveModel::Model' do
|
14
10
|
expect(instance).to respond_to :valid?
|
15
11
|
end
|
@@ -17,14 +13,9 @@ describe 'including SimpleFormObject' do
|
|
17
13
|
|
18
14
|
describe '.attribute' do
|
19
15
|
let(:attribute) { :foo }
|
20
|
-
let(:instance) do
|
21
|
-
class Klass
|
22
|
-
include SimpleFormObject
|
23
|
-
|
24
|
-
attribute :foo
|
25
|
-
end
|
26
16
|
|
27
|
-
|
17
|
+
before do
|
18
|
+
klass.attribute :foo
|
28
19
|
end
|
29
20
|
|
30
21
|
it 'should respond to "attribute"' do
|
@@ -39,15 +30,10 @@ describe 'including SimpleFormObject' do
|
|
39
30
|
let(:types) { %i(boolean string email url tel password search text file hidden integer float decimal range datetime date time select radio_buttons check_boxes country time_zone) }
|
40
31
|
|
41
32
|
let(:type) { :boolean }
|
42
|
-
let(:attr) { :
|
43
|
-
let(:instance) do
|
44
|
-
class AnotherForm
|
45
|
-
include SimpleFormObject
|
46
|
-
|
47
|
-
attribute :foo, :boolean
|
48
|
-
end
|
33
|
+
let(:attr) { :la }
|
49
34
|
|
50
|
-
|
35
|
+
before do
|
36
|
+
klass.attribute attr, type
|
51
37
|
end
|
52
38
|
|
53
39
|
it 'should return a fake column with the correct type' do
|
@@ -56,34 +42,23 @@ describe 'including SimpleFormObject' do
|
|
56
42
|
end
|
57
43
|
|
58
44
|
describe 'options' do
|
59
|
-
|
60
|
-
|
61
|
-
class AnotherForm
|
62
|
-
include SimpleFormObject
|
63
|
-
|
64
|
-
attribute :foo, :boolean, default: true
|
65
|
-
end
|
45
|
+
let(:type) { :boolean }
|
46
|
+
let(:attr) { :doh }
|
66
47
|
|
67
|
-
|
68
|
-
|
48
|
+
before do
|
49
|
+
klass.attribute attr, type, default: true
|
50
|
+
end
|
69
51
|
|
52
|
+
describe 'default' do
|
70
53
|
it 'should set the attribute to the default' do
|
71
|
-
expect(instance.
|
54
|
+
expect(instance.doh).to eq true
|
72
55
|
end
|
73
56
|
|
74
57
|
context 'when a value is supplied in the initialization hash' do
|
75
|
-
let(:instance)
|
76
|
-
class AnotherForm
|
77
|
-
include SimpleFormObject
|
78
|
-
|
79
|
-
attribute :foo, :boolean, default: true
|
80
|
-
end
|
81
|
-
|
82
|
-
AnotherForm.new(foo: false)
|
83
|
-
end
|
58
|
+
let(:instance) { klass.new(doh: false) }
|
84
59
|
|
85
60
|
it 'should use the value in the hash' do
|
86
|
-
expect(instance.
|
61
|
+
expect(instance.doh).to eq false
|
87
62
|
end
|
88
63
|
end
|
89
64
|
|
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.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Leonard Garvey
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01-
|
11
|
+
date: 2014-01-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -95,9 +95,10 @@ files:
|
|
95
95
|
- lib/simple_form_object.rb
|
96
96
|
- lib/simple_form_object/version.rb
|
97
97
|
- simple_form_objects.gemspec
|
98
|
+
- spec/attribute_spec.rb
|
98
99
|
- spec/model_spec.rb
|
99
100
|
- spec/spec_helper.rb
|
100
|
-
homepage: http://github.com/
|
101
|
+
homepage: http://github.com/reinteractive-open/simple_form_object
|
101
102
|
licenses:
|
102
103
|
- MIT
|
103
104
|
metadata: {}
|
@@ -122,5 +123,6 @@ signing_key:
|
|
122
123
|
specification_version: 4
|
123
124
|
summary: Simple form objects for simple form and rails
|
124
125
|
test_files:
|
126
|
+
- spec/attribute_spec.rb
|
125
127
|
- spec/model_spec.rb
|
126
128
|
- spec/spec_helper.rb
|