Skip to content

Commit

Permalink
some refactoring to ensure stable compatibility with the rails form_h…
Browse files Browse the repository at this point in the history
…elpers
  • Loading branch information
Steve Forkin committed Aug 1, 2015
1 parent f2723ee commit 79e5227
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 13 deletions.
37 changes: 33 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MustacheForm

## Travis CI
## Builds
[![Build Status](https://travis-ci.org/netflakes/mustache_form.svg?branch=master)](https://travis-ci.org/netflakes/mustache_form)


Expand All @@ -25,6 +25,19 @@ Or install it yourself as:

$ gem install mustache_form


## Installation - Configuration

Mustache form has an install generator that creates an initializer allowing you to turn on and off
options. For now the only option is to enable the SimpleForm support globally. Basically this means
that when this option is turned on - then the simple_form helper will always be used.

You can safely turn this on if you are using simple_form as you can also turn it off for individual
forms, see below:




## Usage

The best way to explain how to use this little gem is with a few code samples from an existing
Expand Down Expand Up @@ -128,10 +141,26 @@ module People
```

#### NB: simple_form gem dependancy
#### SimpleForm - Simple Fields For

**Simple Form** also comes with some extra helpers you can use inside rails default forms without relying
on `simple_form_for` helper. They are listed below.

Wrapper to use **Simple Form** inside a default rails form. It works in the same way that the `fields_for`
Rails helper, but change the builder to use the `SimpleForm::FormBuilder`.

```ruby
form_for @user do |f|
f.simple_fields_for :posts do |posts_form|
# Here you have all simple_form methods available
posts_form.input :title
end
end
```

This version has an inbuilt dependancy on the simple_form gem. I intend to remove this dependancy in the next
build and include an install generator that lets the user decide whether it should be included and enabled or not.
**Mustache Form** should support these as we are simply yielding to your view method that contains the inputs
during the process and hence you should be able to use these - though I would caution that I have not had the
chance to test this feature.

## Development

Expand Down
27 changes: 22 additions & 5 deletions lib/mustache_form/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,37 @@
module MustacheForm
module FormHelper

def mustache_form_tag(url: nil, html: nil)
MustacheForm.simple_form_enabled == true ? formable = :simple_form_tag : formable = :form_tag
# The basic form_tag just wraps the rails form_tag helper and then yields to
# the view method that handles the form inputs
#
#-form_tag(url_for_options = {}, options = {}, &block)
#
def mustache_form_tag(url_for_options: {}, html_options: {})
formable = :form_tag
lambda do |text|
send(formable, url: url, html: html) do |f|
send(formable, url_for_options, html_options) do |f|
obj = FormedMustache.new(yield(f))
Mustache.render(text, obj).html_safe
end
end
end

def mustache_form_for(object, url: nil, html: nil)
# The form_for version in the basic form wraps the rails form_for helper and
# then yields to the method that handles the form inputs. It also handles the
# the optional use of the simple_form form helper. Since the method signature
# is identical it just needs to call the correct method name
#
#-form_for(record, options = {}, &block)
#-simple_form_for(record, options = {}, &block)
#
def mustache_form_for(object, url_for_options: {}, html_options: {}, use_rails_form_helper: false)
MustacheForm.simple_form_enabled == true ? formable = :simple_form_for : formable = :form_for
formable = :form_for if use_rails_form_helper
lambda do |text|
send(formable, object, url: url, html: html) do |f|
options = {
url: url_for_options, html: html_options
}
send(formable, object, options) do |f|
obj = FormedMustache.new(yield(f))
Mustache.render(text, obj).html_safe
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mustache_form/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module MustacheForm
VERSION = "0.2"
VERSION = "0.2.1"
end
5 changes: 2 additions & 3 deletions spec/mustache_form/form_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ class MockFormView < Mustache
attr_accessor :simple_form_called # to verify that we are using simple_form

def test_form_tag
mustache_form_tag(url: nil, html: nil) do |form|
mustache_form_tag(url_for_options: {}, html_options: {}) do |form|
{ key: form[:key], value: form[:value] }
end
end

def test_form_for(object)
mustache_form_for(object, url: nil, html: nil) do |form|
mustache_form_for(object, url_for_options: {}, html_options: {}) do |form|
{ key: form[:key], value: form[:value] }
end
end
Expand Down Expand Up @@ -94,7 +94,6 @@ def initialize(id, name); @id, @name = id, name; end;
input_value, expected_value = "first_name", "Jo"
form_double = double('MustacheFormDouble')
subject.test_form_tag.call(input_value)
expect(subject.simple_form_called).to eq true
end

it 'correctly calls the simple_form_for helper' do
Expand Down

0 comments on commit 79e5227

Please sign in to comment.