diff --git a/lib/mountain_view.rb b/lib/mountain_view.rb index 1eaf447..32ef5a9 100644 --- a/lib/mountain_view.rb +++ b/lib/mountain_view.rb @@ -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 diff --git a/lib/mountain_view/component.rb b/lib/mountain_view/component.rb index 3a16d31..84434ef 100644 --- a/lib/mountain_view/component.rb +++ b/lib/mountain_view/component.rb @@ -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 {} @@ -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 diff --git a/lib/mountain_view/helpers/form_builder.rb b/lib/mountain_view/helpers/form_builder.rb index 95c65f3..ea54d15 100644 --- a/lib/mountain_view/helpers/form_builder.rb +++ b/lib/mountain_view/helpers/form_builder.rb @@ -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 diff --git a/lib/mountain_view/helpers/object_wrapper.rb b/lib/mountain_view/helpers/object_wrapper.rb new file mode 100644 index 0000000..7ab4fb5 --- /dev/null +++ b/lib/mountain_view/helpers/object_wrapper.rb @@ -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 diff --git a/test/dummy/app/components/form_custom_button/form_custom_button.yml b/test/dummy/app/components/form_custom_button/form_custom_button.yml index 1dd748c..630a640 100644 --- a/test/dummy/app/components/form_custom_button/form_custom_button.yml +++ b/test/dummy/app/components/form_custom_button/form_custom_button.yml @@ -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' diff --git a/test/dummy/app/components/header/header.yml b/test/dummy/app/components/header/header.yml index f74a42e..692845d 100644 --- a/test/dummy/app/components/header/header.yml +++ b/test/dummy/app/components/header/header.yml @@ -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} diff --git a/test/mountain_view/component_test.rb b/test/mountain_view/component_test.rb index b665600..0e5c2e6 100644 --- a/test/mountain_view/component_test.rb +++ b/test/mountain_view/component_test.rb @@ -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" } } ] } @@ -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 @@ -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