Skip to content

Commit

Permalink
little bit of readme-ing
Browse files Browse the repository at this point in the history
  • Loading branch information
ajb committed Sep 27, 2013
1 parent c23f136 commit c3a3b22
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
47 changes: 40 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
Formbuilder.rb [![circle ci status](https://circleci.com/gh/dobtco/formbuilder-rb.png?circle-token=a769ad2fc81271bc1869b5e5a95053efa36b376f)](https://circleci.com/gh/dobtco/formbuilder-rb) <a href='https://coveralls.io/r/dobtco/formbuilder-rb'><img src='https://coveralls.io/repos/dobtco/formbuilder-rb/badge.png' alt='Coverage Status' /></a>
========

## Set up your models
#### The `ResponseField`, `EntryAttachment`, and `Form` models live inside the engine
Formbuilder.rb is a [Rails Engine](#) that's designed as a compliment to [Formbuilder.js](https://github.com/dobtco/formbuilder), a library that lets your users create their own webforms inside of your application.

Since Formbuilder.rb is a fairly non-trial piece of software, it's important to understand its components and how it works:

1. We add `ResponseField`, `EntryAttachment`, and `Form` models to your application. (Each type of response field (text, checkboxes, dropdown, etc.) uses [STI](), inheriting from the `ResponseField` model.)
3. You `include Formbuilder::Entry` in an existing model.
4. We add a few classes to help you render forms and entries.

*Note: All Formbuidler models and classes are namespaced within the `Formbuilder` module.*

If you have a few moments, consider reading the source, especially the Rails app in `spec/dummy`, as it should give you a good idea of how Formbuilder integrates.

### Installation
#### 1) Create the migrations for the Formbuilder models
1. `rake formbuilder:install:migrations`
2. `rake db:migrate`

#### The `Entry` model gets mixed in to an existing model in your application
#### 2) The `Entry` model gets mixed in to an existing model in your application
```ruby
# submitted_at :datetime
# responses :hstore
Expand All @@ -20,18 +31,29 @@ class Entry < ActiveRecord::Base
end
```

## Render a form
#### 3) Associate a form with an existing model (optional)
```ruby
class MovieTheater < ActiveRecord::Base

has_one :form, as: :formable

end
```

### Usage

#### Render a form
```erb
<%= Formbuilder::FormRenderer.new(@form, @entry).to_html %>
```

## Save an entry
#### Save an entry
```ruby
@entry = Entry.new(form: @form)
@entry.save_responses(params[:response_fields], @form.response_fields.not_admin_only)
@entry.save_responses(params[:response_fields], @form.response_fields) # validates automatically
```

## Validate an entry
#### Validate an entry
```ruby
@entry.valid?
# => false
Expand All @@ -40,6 +62,17 @@ end
# => "can't be blank"
```

#### Integrate with the [Formbuilder.js](https://github.com/dobtco/formbuilder) frontend
```ruby
# config/routes.rb
resources :forms, only: [:update]

# app/controllers/forms_controller.rb
class FormsController < Formbuilder::FormsController
...
end
```

#### License

MIT
5 changes: 4 additions & 1 deletion app/controllers/formbuilder/forms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ def update
@form.response_fields.find { |rf| rf.id == field_params[:id].to_i } :
@form.response_fields.build

response_field.update_attributes(pick(field_params, *ResponseField::ALLOWED_PARAMS).merge(sort_order: i, type: "ResponseFields::#{field_params[:field_type].camelize}"))
response_field.update_attributes(pick(field_params, *ResponseField::ALLOWED_PARAMS).merge(
sort_order: i,
type: "Formbuilder::ResponseField#{field_params[:field_type].camelize}"
))
response_field.cid = field_params[:cid]
existing_response_field_ids.push response_field.id
end
Expand Down

0 comments on commit c3a3b22

Please sign in to comment.