From c3a3b2240d5f955154ea1f5812898731d8a398e5 Mon Sep 17 00:00:00 2001 From: adamjacobbecker Date: Thu, 26 Sep 2013 20:39:05 -0400 Subject: [PATCH] little bit of readme-ing --- README.md | 47 ++++++++++++++++--- .../formbuilder/forms_controller.rb | 5 +- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index aded8ff..3720f64 100644 --- a/README.md +++ b/README.md @@ -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) Coverage Status ======== -## 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 @@ -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 @@ -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 diff --git a/app/controllers/formbuilder/forms_controller.rb b/app/controllers/formbuilder/forms_controller.rb index 72f6b3c..ddfe889 100644 --- a/app/controllers/formbuilder/forms_controller.rb +++ b/app/controllers/formbuilder/forms_controller.rb @@ -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