Skip to content

Commit e93dbea

Browse files
committed
Update mocking syntax, Add pretty output
1 parent dec1985 commit e93dbea

File tree

7 files changed

+74
-45
lines changed

7 files changed

+74
-45
lines changed

lib/mountain_view.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require "mountain_view/presenter"
44
require "mountain_view/component"
55
require "mountain_view/helpers/form_builder"
6+
require "mountain_view/helpers/object_wrapper"
67

78
module MountainView
89
def self.configuration

lib/mountain_view/component.rb

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ def title
1111
end
1212

1313
def styleguide_stubs
14+
handle_proc = proc { |type, val|
15+
_tag, _domain, object_type = type.split(":")
16+
case object_type
17+
when "Object"
18+
attrs = val["attributes"]
19+
obj = val["class"].constantize.new(attrs)
20+
MountainView::Helpers::ObjectWrapper.new(obj, attrs)
21+
when "Form"
22+
MountainView::Helpers::FormBuilder.new(val["for"])
23+
end
24+
}
25+
Psych.add_domain_type("mountain_view", "Object", &handle_proc)
26+
Psych.add_domain_type("mountain_view", "Form", &handle_proc)
1427
YAML.load_file(stubs_file) || {}
1528
rescue Errno::ENOENT
1629
{}
@@ -55,17 +68,5 @@ def stubs_correct_format?
5568
def stubs_are_a_hash_with_info?
5669
styleguide_stubs.is_a?(Hash) && styleguide_stubs.key?(:stubs)
5770
end
58-
59-
def create_form_builder(stub)
60-
ActionView::Base.default_form_builder.new(stub.class.model_name.param_key,
61-
stub,
62-
ActionView::Base.new,
63-
{})
64-
rescue
65-
ActionView::Base.default_form_builder.new(stub.class.model_name.param_key,
66-
stub,
67-
ActionView::Base.new,
68-
{}, nil)
69-
end
7071
end
7172
end

lib/mountain_view/helpers/form_builder.rb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
module MountainView
22
module Helpers
33
class FormBuilder < ActionView::Base.default_form_builder
4-
def init_with(coder)
5-
model = coder.map["model"]
6-
create_form_builder(model)
7-
end
8-
9-
private
4+
attr_accessor :model
105

11-
def create_form_builder(model)
12-
initialize(model.class.model_name.param_key,
13-
model,
14-
ActionView::Base.new,
15-
{})
6+
def initialize(model)
7+
@model = model
8+
super(@model.class.model_name.param_key,
9+
@model,
10+
ActionView::Base.new,
11+
{})
1612
rescue
17-
initialize(model.class.model_name.param_key,
18-
model,
19-
ActionView::Base.new,
20-
{},
21-
nil)
13+
super(@model.class.model_name.param_key,
14+
@model,
15+
ActionView::Base.new,
16+
{},
17+
nil)
18+
end
19+
20+
def to_json(_)
21+
"form_for(#{model.class.model_name}.new(#{model.attributes.delete_if { |k, v| v.nil? }.deep_symbolize_keys}))"
2222
end
2323
end
2424
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module MountainView
2+
module Helpers
3+
class ObjectWrapper < SimpleDelegator
4+
attr_accessor :_attributes
5+
6+
def initialize(object, attributes)
7+
super(object)
8+
@_attributes = attributes
9+
end
10+
11+
def class
12+
__getobj__.class
13+
end
14+
15+
def to_json(_)
16+
object = __getobj__
17+
"#{object.class.model_name}.new(#{_attributes.deep_symbolize_keys})"
18+
end
19+
end
20+
end
21+
end

test/dummy/app/components/form_custom_button/form_custom_button.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22
:stubs:
33
-
44
:id: 1
5-
:some_model: !ruby/object:Something
5+
:some_model: !mountain_view:Object
6+
class: Something
67
attributes:
78
name: 'blabla'
9+
:some_another_model: !Object
10+
class: Something
11+
attributes:
12+
name: 'another blabla'
813
:form:
9-
!ruby/object:MountainView::Helpers::FormBuilder
10-
model: !ruby/object:Something
14+
!Form
15+
for: !Object
16+
class: Something
1117
attributes:
1218
name: 'something name'

test/dummy/app/components/header/header.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,3 @@
77
-
88
:id: 2
99
:title: "You won't believe what happened to this man at Aspen"
10-
-
11-
:id: 3
12-
:title: "Testing form objects"
13-
:form: {form_for: Object}

test/mountain_view/component_test.rb

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ def test_styleguide_stubs
2727
},
2828
{ id: 2,
2929
title: "You won't believe what happened to this man at Aspen"
30-
},
31-
{
32-
id: 3,
33-
title: "Testing form objects",
34-
form: { "form_for" => "Object" }
3530
}
3631
]
3732
}
@@ -52,11 +47,6 @@ def test_component_stubs
5247
{
5348
id: 2,
5449
title: "You won't believe what happened to this man at Aspen"
55-
},
56-
{
57-
id: 3,
58-
title: "Testing form objects",
59-
form: { "form_for" => "Object" }
6050
}
6151
]
6252
assert_instance_of Array, component.component_stubs
@@ -138,4 +128,18 @@ def test_stubs?
138128
assert_equal false, component_without_stub_file.stubs?
139129
assert_equal false, component_with_empty_stub_file.stubs?
140130
end
131+
132+
def test_component_stubs_pretty_json
133+
component = MountainView::Component.new("form_custom_button")
134+
component_properties = component.component_stubs.first
135+
json = JSON.pretty_generate component_properties
136+
137+
model_pattern = /Something.new\({:name=>"blabla"}\)/
138+
form_pattern = /form_for\(Something.new\({:name=>\"something name\"}\)\)/
139+
140+
p json
141+
142+
assert_match(model_pattern, json)
143+
assert_match(form_pattern, json)
144+
end
141145
end

0 commit comments

Comments
 (0)