Skip to content

Commit

Permalink
Update mocking syntax, Add pretty output
Browse files Browse the repository at this point in the history
  • Loading branch information
drakmail committed May 6, 2016
1 parent dec1985 commit 458c863
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 45 deletions.
1 change: 1 addition & 0 deletions lib/mountain_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "mountain_view/presenter"
require "mountain_view/component"
require "mountain_view/helpers/form_builder"
require "mountain_view/helpers/object_wrapper"

module MountainView
def self.configuration
Expand Down
25 changes: 13 additions & 12 deletions lib/mountain_view/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ def title
end

def styleguide_stubs
handle_proc = proc { |type, val|
_tag, _domain, object_type = type.split(":")
case object_type
when "Object"
attrs = val["attributes"]
obj = val["class"].constantize.new(attrs)
MountainView::Helpers::ObjectWrapper.new(obj, attrs)
when "Form"
MountainView::Helpers::FormBuilder.new(val["for"])
end
}
Psych.add_domain_type("mountain_view", "Object", &handle_proc)
Psych.add_domain_type("mountain_view", "Form", &handle_proc)
YAML.load_file(stubs_file) || {}
rescue Errno::ENOENT
{}
Expand Down Expand Up @@ -55,17 +68,5 @@ def stubs_correct_format?
def stubs_are_a_hash_with_info?
styleguide_stubs.is_a?(Hash) && styleguide_stubs.key?(:stubs)
end

def create_form_builder(stub)
ActionView::Base.default_form_builder.new(stub.class.model_name.param_key,
stub,
ActionView::Base.new,
{})
rescue
ActionView::Base.default_form_builder.new(stub.class.model_name.param_key,
stub,
ActionView::Base.new,
{}, nil)
end
end
end
32 changes: 16 additions & 16 deletions lib/mountain_view/helpers/form_builder.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
module MountainView
module Helpers
class FormBuilder < ActionView::Base.default_form_builder
def init_with(coder)
model = coder.map["model"]
create_form_builder(model)
end

private
attr_accessor :model

def create_form_builder(model)
initialize(model.class.model_name.param_key,
model,
ActionView::Base.new,
{})
def initialize(model)
@model = model
super(@model.class.model_name.param_key,
@model,
ActionView::Base.new,
{})
rescue
initialize(model.class.model_name.param_key,
model,
ActionView::Base.new,
{},
nil)
super(@model.class.model_name.param_key,
@model,
ActionView::Base.new,
{},
nil)
end

def to_json(_)
"form_for(#{model.to_json(nil)})"
end
end
end
Expand Down
21 changes: 21 additions & 0 deletions lib/mountain_view/helpers/object_wrapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module MountainView
module Helpers
class ObjectWrapper < SimpleDelegator
attr_accessor :_attributes

def initialize(object, attributes)
super(object)
@_attributes = attributes
end

def class
__getobj__.class
end

def to_json(_)
object = __getobj__
"#{object.class.model_name}.new(#{_attributes.deep_symbolize_keys})"
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
:stubs:
-
:id: 1
:some_model: !ruby/object:Something
:some_model: !mountain_view:Object
class: Something
attributes:
name: 'blabla'
:some_another_model: !Object
class: Something
attributes:
name: 'another blabla'
:form:
!ruby/object:MountainView::Helpers::FormBuilder
model: !ruby/object:Something
!Form
for: !Object
class: Something
attributes:
name: 'something name'
4 changes: 0 additions & 4 deletions test/dummy/app/components/header/header.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,3 @@
-
:id: 2
:title: "You won't believe what happened to this man at Aspen"
-
:id: 3
:title: "Testing form objects"
:form: {form_for: Object}
22 changes: 12 additions & 10 deletions test/mountain_view/component_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ def test_styleguide_stubs
},
{ id: 2,
title: "You won't believe what happened to this man at Aspen"
},
{
id: 3,
title: "Testing form objects",
form: { "form_for" => "Object" }
}
]
}
Expand All @@ -52,11 +47,6 @@ def test_component_stubs
{
id: 2,
title: "You won't believe what happened to this man at Aspen"
},
{
id: 3,
title: "Testing form objects",
form: { "form_for" => "Object" }
}
]
assert_instance_of Array, component.component_stubs
Expand Down Expand Up @@ -138,4 +128,16 @@ def test_stubs?
assert_equal false, component_without_stub_file.stubs?
assert_equal false, component_with_empty_stub_file.stubs?
end

def test_component_stubs_pretty_json
component = MountainView::Component.new("form_custom_button")
component_properties = component.component_stubs.first
json = JSON.pretty_generate component_properties

model_pattern = /Something.new\({:name=>"blabla"}\)/
form_pattern = /form_for\(Something.new\({:name=>\"something name\"}\)\)/

assert_match(model_pattern, json)
assert_match(form_pattern, json)
end
end

0 comments on commit 458c863

Please sign in to comment.