Support for citext, hstore, json & jsonb columns

This commit is contained in:
Stefan Wrobel 2017-12-08 15:31:01 -08:00
parent 6243a634f9
commit 87d88fbb71
5 changed files with 51 additions and 17 deletions

View File

@ -2,6 +2,7 @@
* Allow custom errors classes to inputs . [@feliperenan](https://github.com/feliperenan) * 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) * 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 ### Bug fix
* Fix horizontal form label position, from right to text-right. [@cavpollo](https://github.com/cavpollo) * Fix horizontal form label position, from right to text-right. [@cavpollo](https://github.com/cavpollo)

View File

@ -555,6 +555,7 @@ Mapping | Generated HTML Element | Database Column Type
--------------- |--------------------------------------|--------------------- --------------- |--------------------------------------|---------------------
`boolean` | `input[type=checkbox]` | `boolean` `boolean` | `input[type=checkbox]` | `boolean`
`string` | `input[type=text]` | `string` `string` | `input[type=text]` | `string`
`citext` | `input[type=text]` | `citext`
`email` | `input[type=email]` | `string` with `name =~ /email/` `email` | `input[type=email]` | `string` with `name =~ /email/`
`url` | `input[type=url]` | `string` with `name =~ /url/` `url` | `input[type=url]` | `string` with `name =~ /url/`
`tel` | `input[type=tel]` | `string` with `name =~ /phone/` `tel` | `input[type=tel]` | `string` with `name =~ /phone/`
@ -562,6 +563,9 @@ Mapping | Generated HTML Element | Database Column Type
`search` | `input[type=search]` | - `search` | `input[type=search]` | -
`uuid` | `input[type=text]` | `uuid` `uuid` | `input[type=text]` | `uuid`
`text` | `textarea` | `text` `text` | `textarea` | `text`
`hstore` | `textarea` | `hstore`
`json` | `textarea` | `json`
`jsonb` | `textarea` | `jsonb`
`file` | `input[type=file]` | `string` responding to file methods `file` | `input[type=file]` | `string` responding to file methods
`hidden` | `input[type=hidden]` | - `hidden` | `input[type=hidden]` | -
`integer` | `input[type=number]` | `integer` `integer` | `input[type=number]` | `integer`

View File

@ -18,20 +18,20 @@ module SimpleForm
extend MapType extend MapType
include SimpleForm::Inputs include SimpleForm::Inputs
map_type :text, to: SimpleForm::Inputs::TextInput map_type :text, :hstore, :json, :jsonb, to: SimpleForm::Inputs::TextInput
map_type :file, to: SimpleForm::Inputs::FileInput map_type :file, to: SimpleForm::Inputs::FileInput
map_type :string, :email, :search, :tel, :url, :uuid, to: SimpleForm::Inputs::StringInput map_type :string, :email, :search, :tel, :url, :uuid, :citext, to: SimpleForm::Inputs::StringInput
map_type :password, to: SimpleForm::Inputs::PasswordInput map_type :password, to: SimpleForm::Inputs::PasswordInput
map_type :integer, :decimal, :float, to: SimpleForm::Inputs::NumericInput map_type :integer, :decimal, :float, to: SimpleForm::Inputs::NumericInput
map_type :range, to: SimpleForm::Inputs::RangeInput map_type :range, to: SimpleForm::Inputs::RangeInput
map_type :check_boxes, to: SimpleForm::Inputs::CollectionCheckBoxesInput map_type :check_boxes, to: SimpleForm::Inputs::CollectionCheckBoxesInput
map_type :radio_buttons, to: SimpleForm::Inputs::CollectionRadioButtonsInput map_type :radio_buttons, to: SimpleForm::Inputs::CollectionRadioButtonsInput
map_type :select, to: SimpleForm::Inputs::CollectionSelectInput map_type :select, to: SimpleForm::Inputs::CollectionSelectInput
map_type :grouped_select, to: SimpleForm::Inputs::GroupedCollectionSelectInput map_type :grouped_select, to: SimpleForm::Inputs::GroupedCollectionSelectInput
map_type :date, :time, :datetime, to: SimpleForm::Inputs::DateTimeInput map_type :date, :time, :datetime, to: SimpleForm::Inputs::DateTimeInput
map_type :country, :time_zone, to: SimpleForm::Inputs::PriorityInput map_type :country, :time_zone, to: SimpleForm::Inputs::PriorityInput
map_type :boolean, to: SimpleForm::Inputs::BooleanInput map_type :boolean, to: SimpleForm::Inputs::BooleanInput
map_type :hidden, to: SimpleForm::Inputs::HiddenInput map_type :hidden, to: SimpleForm::Inputs::HiddenInput
def self.discovery_cache def self.discovery_cache
@discovery_cache ||= {} @discovery_cache ||= {}

View File

@ -142,6 +142,24 @@ class FormBuilderTest < ActionView::TestCase
assert_select 'form input#user_description.string' assert_select 'form input#user_description.string'
end 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 test 'builder generates a checkbox for boolean columns' do
with_form_for @user, :active with_form_for @user, :active
assert_select 'form input[type=checkbox]#user_active.boolean' assert_select 'form input[type=checkbox]#user_active.boolean'
@ -166,6 +184,11 @@ class FormBuilderTest < ActionView::TestCase
end end
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 test 'builder generates password fields for columns that matches password' do
with_form_for @user, :password with_form_for @user, :password
assert_select 'form input#user_password.password' assert_select 'form input#user_password.password'

View File

@ -90,7 +90,8 @@ class User
:avatar, :home_picture, :email, :status, :residence_country, :phone_number, :avatar, :home_picture, :email, :status, :residence_country, :phone_number,
:post_count, :lock_version, :amount, :attempts, :action, :credit_card, :gender, :post_count, :lock_version, :amount, :attempts, :action, :credit_card, :gender,
:extra_special_company_id, :pictures, :picture_ids, :special_pictures, :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 = {}) def self.build(extra_attributes = {})
attributes = { attributes = {
@ -141,7 +142,7 @@ class User
when :attempts then :integer when :attempts then :integer
when :action then :string when :action then :string
when :credit_card then :string when :credit_card then :string
when :uuid then :uuid else attribute.to_sym
end end
Column.new(attribute, column_type, limit) Column.new(attribute, column_type, limit)
end end
@ -175,6 +176,10 @@ class User
when 'action' then :string when 'action' then :string
when 'credit_card' then :string when 'credit_card' then :string
when 'uuid' 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 end
ActiveModel::Type.lookup(column_type, limit: limit) ActiveModel::Type.lookup(column_type, limit: limit)
@ -187,7 +192,8 @@ class User
when :name, :status, :password, :description, :age, when :name, :status, :password, :description, :age,
:credit_limit, :active, :born_at, :delivery_time, :credit_limit, :active, :born_at, :delivery_time,
:created_at, :updated_at, :lock_version, :home_picture, :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 else false
end end
end end