Skip to content

Commit bd290be

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

21 files changed

+166
-6
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

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ rails = case rails_version
1616
end
1717

1818
gem "rails", rails
19+
gem "test-unit", "~> 3.0"
1920

2021
# Declare any dependencies that are still in development here instead of in
2122
# your gemspec. These might include edge Rails or gems from your path or

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,56 @@ 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+
:form:
163+
!ruby/object:MountainView::Helpers::FormBuilder
164+
model: !ruby/object:Something
165+
attributes:
166+
name: 'something name'
167+
```
168+
169+
Also you can stub any ruby class with the following method:
170+
171+
172+
```yml
173+
:meta: 'There is a class with an AR object'
174+
:stubs:
175+
-
176+
:some_model: !ruby/object:Something
177+
attributes:
178+
name: 'blabla'
179+
```
180+
181+
For any complex logic you can define a custom Compoenent Stub with `init_with(coder)` method which will receive a `#map` with attributes loaded from yaml file:
182+
183+
```yml
184+
:meta: 'There is a class with a complex logic'
185+
:stubs:
186+
-
187+
:some_model: !ruby/object:ComponentStub
188+
params:
189+
- 'blabla'
190+
- 'nothing'
191+
name: 'Some Name'
192+
```
193+
194+
```ruby
195+
class ComponentStub < SomeModel # SomeModel could be AR class
196+
def init_with(coder)
197+
param1 = coder.map["params"].first
198+
somename = coder.map["name"]
199+
# or `@properties = coder.map`
200+
initialize(param1, somename) # call initializer of SomeModel
201+
end
202+
end
203+
```
204+
155205
3) Vist `http://localhost:3000/mountain_view/styleguide`
156206

157207
#### Example Style Guide

lib/mountain_view.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require "mountain_view/configuration"
33
require "mountain_view/presenter"
44
require "mountain_view/component"
5+
require "mountain_view/helpers/form_builder"
56

67
module MountainView
78
def self.configuration

lib/mountain_view/component.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,17 @@ 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 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
5870
end
5971
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module MountainView
2+
module Helpers
3+
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
10+
11+
def create_form_builder(model)
12+
initialize(model.class.model_name.param_key, model, ActionView::Base.new, {})
13+
rescue
14+
initialize(model.class.model_name.param_key, model, ActionView::Base.new, {}, nil)
15+
end
16+
end
17+
end
18+
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.

0 commit comments

Comments
 (0)