From 87d88fbb71c75765ecdf366ea62739088c2e53b8 Mon Sep 17 00:00:00 2001 From: Stefan Wrobel Date: Fri, 8 Dec 2017 15:31:01 -0800 Subject: [PATCH] Support for citext, hstore, json & jsonb columns --- CHANGELOG.md | 1 + README.md | 4 ++++ lib/simple_form/form_builder.rb | 28 ++++++++++++++-------------- test/form_builder/general_test.rb | 23 +++++++++++++++++++++++ test/support/models.rb | 12 +++++++++--- 5 files changed, 51 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bdc315a4..566750bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ * Allow custom errors classes to inputs . [@feliperenan](https://github.com/feliperenan) * Remove support from Rails 4.0, 4.1 and 4.2. [@feliperenan](https://github.com/feliperenan) +* Add support for citext, hstore, json & jsonb column types. [@swrobel](https://github.com/swrobel) ### Bug fix * Fix horizontal form label position, from right to text-right. [@cavpollo](https://github.com/cavpollo) diff --git a/README.md b/README.md index fba7d185..3bbc8bf8 100644 --- a/README.md +++ b/README.md @@ -555,6 +555,7 @@ Mapping | Generated HTML Element | Database Column Type --------------- |--------------------------------------|--------------------- `boolean` | `input[type=checkbox]` | `boolean` `string` | `input[type=text]` | `string` +`citext` | `input[type=text]` | `citext` `email` | `input[type=email]` | `string` with `name =~ /email/` `url` | `input[type=url]` | `string` with `name =~ /url/` `tel` | `input[type=tel]` | `string` with `name =~ /phone/` @@ -562,6 +563,9 @@ Mapping | Generated HTML Element | Database Column Type `search` | `input[type=search]` | - `uuid` | `input[type=text]` | `uuid` `text` | `textarea` | `text` +`hstore` | `textarea` | `hstore` +`json` | `textarea` | `json` +`jsonb` | `textarea` | `jsonb` `file` | `input[type=file]` | `string` responding to file methods `hidden` | `input[type=hidden]` | - `integer` | `input[type=number]` | `integer` diff --git a/lib/simple_form/form_builder.rb b/lib/simple_form/form_builder.rb index 19d1e31d..55176906 100644 --- a/lib/simple_form/form_builder.rb +++ b/lib/simple_form/form_builder.rb @@ -18,20 +18,20 @@ module SimpleForm extend MapType include SimpleForm::Inputs - map_type :text, to: SimpleForm::Inputs::TextInput - map_type :file, to: SimpleForm::Inputs::FileInput - map_type :string, :email, :search, :tel, :url, :uuid, to: SimpleForm::Inputs::StringInput - map_type :password, to: SimpleForm::Inputs::PasswordInput - map_type :integer, :decimal, :float, to: SimpleForm::Inputs::NumericInput - map_type :range, to: SimpleForm::Inputs::RangeInput - map_type :check_boxes, to: SimpleForm::Inputs::CollectionCheckBoxesInput - map_type :radio_buttons, to: SimpleForm::Inputs::CollectionRadioButtonsInput - map_type :select, to: SimpleForm::Inputs::CollectionSelectInput - map_type :grouped_select, to: SimpleForm::Inputs::GroupedCollectionSelectInput - map_type :date, :time, :datetime, to: SimpleForm::Inputs::DateTimeInput - map_type :country, :time_zone, to: SimpleForm::Inputs::PriorityInput - map_type :boolean, to: SimpleForm::Inputs::BooleanInput - map_type :hidden, to: SimpleForm::Inputs::HiddenInput + map_type :text, :hstore, :json, :jsonb, to: SimpleForm::Inputs::TextInput + map_type :file, to: SimpleForm::Inputs::FileInput + map_type :string, :email, :search, :tel, :url, :uuid, :citext, to: SimpleForm::Inputs::StringInput + map_type :password, to: SimpleForm::Inputs::PasswordInput + map_type :integer, :decimal, :float, to: SimpleForm::Inputs::NumericInput + map_type :range, to: SimpleForm::Inputs::RangeInput + map_type :check_boxes, to: SimpleForm::Inputs::CollectionCheckBoxesInput + map_type :radio_buttons, to: SimpleForm::Inputs::CollectionRadioButtonsInput + map_type :select, to: SimpleForm::Inputs::CollectionSelectInput + map_type :grouped_select, to: SimpleForm::Inputs::GroupedCollectionSelectInput + map_type :date, :time, :datetime, to: SimpleForm::Inputs::DateTimeInput + map_type :country, :time_zone, to: SimpleForm::Inputs::PriorityInput + map_type :boolean, to: SimpleForm::Inputs::BooleanInput + map_type :hidden, to: SimpleForm::Inputs::HiddenInput def self.discovery_cache @discovery_cache ||= {} diff --git a/test/form_builder/general_test.rb b/test/form_builder/general_test.rb index 9dc26ce9..a4367e8a 100644 --- a/test/form_builder/general_test.rb +++ b/test/form_builder/general_test.rb @@ -142,6 +142,24 @@ class FormBuilderTest < ActionView::TestCase assert_select 'form input#user_description.string' end + test 'builder generates text areas for hstore columns' do + with_form_for @user, :hstore + assert_no_select 'form input#user_hstore.string' + assert_select 'form textarea#user_hstore.text' + end + + test 'builder generates text areas for json columns' do + with_form_for @user, :json + assert_no_select 'form input#user_json.string' + assert_select 'form textarea#user_json.text' + end + + test 'builder generates text areas for jsonb columns' do + with_form_for @user, :jsonb + assert_no_select 'form input#user_jsonb.string' + assert_select 'form textarea#user_jsonb.text' + end + test 'builder generates a checkbox for boolean columns' do with_form_for @user, :active assert_select 'form input[type=checkbox]#user_active.boolean' @@ -166,6 +184,11 @@ class FormBuilderTest < ActionView::TestCase end end + test 'builder generates string fields for citext columns' do + with_form_for @user, :citext + assert_select 'form input#user_citext.string' + end + test 'builder generates password fields for columns that matches password' do with_form_for @user, :password assert_select 'form input#user_password.password' diff --git a/test/support/models.rb b/test/support/models.rb index 626ebc14..a8e9febf 100644 --- a/test/support/models.rb +++ b/test/support/models.rb @@ -90,7 +90,8 @@ class User :avatar, :home_picture, :email, :status, :residence_country, :phone_number, :post_count, :lock_version, :amount, :attempts, :action, :credit_card, :gender, :extra_special_company_id, :pictures, :picture_ids, :special_pictures, - :special_picture_ids, :uuid, :friends, :friend_ids, :special_tags, :special_tag_ids + :special_picture_ids, :uuid, :friends, :friend_ids, :special_tags, :special_tag_ids, + :citext, :hstore, :json, :jsonb def self.build(extra_attributes = {}) attributes = { @@ -141,7 +142,7 @@ class User when :attempts then :integer when :action then :string when :credit_card then :string - when :uuid then :uuid + else attribute.to_sym end Column.new(attribute, column_type, limit) end @@ -175,6 +176,10 @@ class User when 'action' then :string when 'credit_card' then :string when 'uuid' then :string + when 'citext' then :string + when 'hstore' then [:text, 200] + when 'json' then [:text, 200] + when 'jsonb' then [:text, 200] end ActiveModel::Type.lookup(column_type, limit: limit) @@ -187,7 +192,8 @@ class User when :name, :status, :password, :description, :age, :credit_limit, :active, :born_at, :delivery_time, :created_at, :updated_at, :lock_version, :home_picture, - :amount, :attempts, :action, :credit_card, :uuid then true + :amount, :attempts, :action, :credit_card, :uuid, + :citext, :hstore, :json, :jsonb then true else false end end