From 5c78eae70e446048f2a2d88a7e313738d4523c7d Mon Sep 17 00:00:00 2001 From: Leonid Shevtsov Date: Wed, 11 Apr 2012 23:47:04 +0300 Subject: [PATCH 1/3] add integration example for Formtastic 2 --- README.rdoc | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/README.rdoc b/README.rdoc index ead58e5..9beff4b 100644 --- a/README.rdoc +++ b/README.rdoc @@ -502,7 +502,24 @@ Second, do not forget to add the +enum_attr+ macro to the generated model and mi === Formtastic integration -You can display the select input in a Formtastic form with a little monkey patching. First, extend Formtastic with an initializer: +You can make Formtastic generate a select with the enum value filled in. + +For Formtastic 2, define a custom input class: + + require 'formtastic' + + class EnumInput < Formtastic::Inputs::SelectInput + def raw_collection + @raw_collection ||= collection_from_options || collection_from_enum + end + + def collection_from_enum + @object.enums(method.to_sym).try(:select_options) || [] + choices = enum ? enum.select_options : [] + end + end + +For Formtastic 1, define a custom helper: require 'formtastic' From 6dcc4daa0b7da7fab60d9d0f9a3b1857f30e2b68 Mon Sep 17 00:00:00 2001 From: Leonid Shevtsov Date: Wed, 11 Apr 2012 23:48:07 +0300 Subject: [PATCH 2/3] fixes for Formtastic 2 example --- README.rdoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rdoc b/README.rdoc index 9beff4b..2583d95 100644 --- a/README.rdoc +++ b/README.rdoc @@ -502,7 +502,7 @@ Second, do not forget to add the +enum_attr+ macro to the generated model and mi === Formtastic integration -You can make Formtastic generate a select with the enum value filled in. +You can make Formtastic generate a select with the enum values filled in automatically. For Formtastic 2, define a custom input class: From 3c709490f08be017d0b675353c1055750e6ecf70 Mon Sep 17 00:00:00 2001 From: Leonid Shevtsov Date: Wed, 11 Apr 2012 23:50:51 +0300 Subject: [PATCH 3/3] fixes for Formtastic 2 example --- README.rdoc | 1 - 1 file changed, 1 deletion(-) diff --git a/README.rdoc b/README.rdoc index 2583d95..b6668de 100644 --- a/README.rdoc +++ b/README.rdoc @@ -515,7 +515,6 @@ For Formtastic 2, define a custom input class: def collection_from_enum @object.enums(method.to_sym).try(:select_options) || [] - choices = enum ? enum.select_options : [] end end