Skip to content

Commit

Permalink
Add support for mocking a form builder objects
Browse files Browse the repository at this point in the history
  • Loading branch information
drakmail committed Apr 27, 2016
1 parent 3038ade commit 39a68db
Show file tree
Hide file tree
Showing 18 changed files with 109 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ test/dummy/.sass-cache
Gemfile.lock
test/tmp
*.gem

*.swp
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
AllCops:
RunRailsCops: true
Exclude:
- spec/dummy/db/schema.rb
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ E.g: `app/components/card/card.yml`
:link: "http://google.com"

```

If your component depends on a form builder object you can use the following statement:

```yml
:meta: 'There is a class with form builder object'
:stubs:
-
:id: 1
:form: # form builder required parameter
!ruby/object:Something # Mountain View will create a form builder like it was created by `form_for Something.new` helper
attributes: # You can set any attributes on Something instance
name: "test"
```
3) Vist `http://localhost:3000/mountain_view/styleguide`

#### Example Style Guide
Expand Down
20 changes: 18 additions & 2 deletions lib/mountain_view/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ def styleguide_stubs

def component_stubs
if styleguide_stubs.is_a?(Hash)
styleguide_stubs[:stubs] || {}
convert_objects_to_form_objects(styleguide_stubs[:stubs] || {})
elsif styleguide_stubs.is_a?(Array)
styleguide_stubs
convert_objects_to_form_objects(styleguide_stubs)
end
end

Expand Down Expand Up @@ -55,5 +55,21 @@ def stubs_correct_format?
def stubs_are_a_hash_with_info?
styleguide_stubs.is_a?(Hash) && styleguide_stubs.key?(:stubs)
end

def convert_objects_to_form_objects(stubs)
case stubs
when Hash
Hash[stubs.map { |k, stub| [k, convert_objects_to_form_objects(stub)] }]
when Enumerable
stubs.map { |stub| convert_objects_to_form_objects(stub) }
when ActiveRecord::Base
ActionView::Base.default_form_builder.new(stubs.model_name.param_key,
stubs,
ActionView::Base.new,
{})
else
stubs
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= form.button "button text" %>
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
:meta: 'There is a class with form builder object'
:stubs:
-
:id: 1
:form:
!ruby/object:Something
attributes:
name: "test"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class FormCustomButtonComponent < MountainView::Presenter
property :form
end
4 changes: 4 additions & 0 deletions test/dummy/app/components/header/header.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@
-
:id: 2
:title: "You won't believe what happened to this man at Aspen"
-
:id: 3
:title: "Testing form objects"
:form: {form_for: Object}
2 changes: 2 additions & 0 deletions test/dummy/app/models/something.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Something < ActiveRecord::Base
end
7 changes: 2 additions & 5 deletions test/dummy/config/application.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
require File.expand_path('../boot', __FILE__)

require "active_support"
require "action_controller"
require "action_view"
require "sprockets/railtie"
require "rails/all"

Bundler.require(*Rails.groups)
require "mountain_view"
Expand Down Expand Up @@ -36,7 +33,7 @@ class Application < Rails::Application
# Do not eager load code on boot. This avoids loading your whole application
# just for the purpose of running a single test. If you are using a tool that
# preloads Rails for running tests, you may have to set it to true.
config.eager_load = false
config.eager_load = true

# Configure static asset server for tests with Cache-Control for performance.
config.static_cache_control = 'public, max-age=3600'
Expand Down
2 changes: 1 addition & 1 deletion test/dummy/config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ development:
# Do not set this db to the same as development or production.
test:
<<: *default
database: db/test.sqlite3
database: ":memory:"

production:
<<: *default
Expand Down
9 changes: 9 additions & 0 deletions test/dummy/db/migrate/20160427131303_create_somethings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateSomethings < ActiveRecord::Migration
def change
create_table :somethings do |t|
t.string :name

t.timestamps null: false
end
end
end
22 changes: 22 additions & 0 deletions test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160427131303) do

create_table "somethings", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

end
10 changes: 10 additions & 0 deletions test/mountain_view/component_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ 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 @@ -47,6 +52,11 @@ 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
7 changes: 7 additions & 0 deletions test/mountain_view/presenter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,11 @@ class MountainView::PresenterTest < ActiveSupport::TestCase
presenter = InheritedPresenter.new("inherited", {})
assert_equal [], presenter.data
end

test "form stubs" do
component = MountainView::Component.new("form_custom_button")
stub_data = component.component_stubs.first
presenter = FormCustomButtonComponent.new("form_custom_button", stub_data)
assert_equal ActionView::Helpers::FormBuilder, presenter.form.class
end
end
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@
# for generators
require "rails/generators/test_case"
require "generators/mountain_view/component_generator"

load "#{Rails.root}/db/schema.rb"

0 comments on commit 39a68db

Please sign in to comment.