Skip to content

Commit 39a68db

Browse files
committed
Add support for mocking a form builder objects
1 parent 3038ade commit 39a68db

File tree

18 files changed

+109
-8
lines changed

18 files changed

+109
-8
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ test/dummy/.sass-cache
99
Gemfile.lock
1010
test/tmp
1111
*.gem
12+
13+
*.swp

.rubocop.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
AllCops:
2+
RunRailsCops: true
3+
Exclude:
4+
- spec/dummy/db/schema.rb

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,20 @@ E.g: `app/components/card/card.yml`
152152
:link: "http://google.com"
153153

154154
```
155+
156+
If your component depends on a form builder object you can use the following statement:
157+
158+
```yml
159+
:meta: 'There is a class with form builder object'
160+
:stubs:
161+
-
162+
:id: 1
163+
:form: # form builder required parameter
164+
!ruby/object:Something # Mountain View will create a form builder like it was created by `form_for Something.new` helper
165+
attributes: # You can set any attributes on Something instance
166+
name: "test"
167+
```
168+
155169
3) Vist `http://localhost:3000/mountain_view/styleguide`
156170

157171
#### Example Style Guide

lib/mountain_view/component.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ def styleguide_stubs
1818

1919
def component_stubs
2020
if styleguide_stubs.is_a?(Hash)
21-
styleguide_stubs[:stubs] || {}
21+
convert_objects_to_form_objects(styleguide_stubs[:stubs] || {})
2222
elsif styleguide_stubs.is_a?(Array)
23-
styleguide_stubs
23+
convert_objects_to_form_objects(styleguide_stubs)
2424
end
2525
end
2626

@@ -55,5 +55,21 @@ def stubs_correct_format?
5555
def stubs_are_a_hash_with_info?
5656
styleguide_stubs.is_a?(Hash) && styleguide_stubs.key?(:stubs)
5757
end
58+
59+
def convert_objects_to_form_objects(stubs)
60+
case stubs
61+
when Hash
62+
Hash[stubs.map { |k, stub| [k, convert_objects_to_form_objects(stub)] }]
63+
when Enumerable
64+
stubs.map { |stub| convert_objects_to_form_objects(stub) }
65+
when ActiveRecord::Base
66+
ActionView::Base.default_form_builder.new(stubs.model_name.param_key,
67+
stubs,
68+
ActionView::Base.new,
69+
{})
70+
else
71+
stubs
72+
end
73+
end
5874
end
5975
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%= form.button "button text" %>

test/dummy/app/components/form_custom_button/form_custom_button.css

Whitespace-only changes.

test/dummy/app/components/form_custom_button/form_custom_button.js

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
:meta: 'There is a class with form builder object'
2+
:stubs:
3+
-
4+
:id: 1
5+
:form:
6+
!ruby/object:Something
7+
attributes:
8+
name: "test"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class FormCustomButtonComponent < MountainView::Presenter
2+
property :form
3+
end

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@
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}

0 commit comments

Comments
 (0)