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)
* 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)

View File

@ -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`

View File

@ -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 ||= {}

View File

@ -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'

View File

@ -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