Description
This gem were implemented for natural using hashes in web development. The gem divided in two parts: middleware and simple_form custom input. With Simphi::Middleware you can send params to server and they will be rebuilt to normal hash and with simple_form input the hash can be sended in proper format to server.
Middleware
The middleware relies to -simphi
part of the hash key.
"contacts-simphi": {
"don't matter what here": {
"key": "email",
"value": "beznosa@yahoo.com"
},
"don't matter what here too": {
"key": "skype",
"value": "mix_beznos"
}
}
# will be replaced with:
"contacts": {
"email": "beznosa@yahoo.com"
},
"skype": "mix_beznos"
}
}
Hash Input
Simphi Input is a SimpleForm custom input which creates a typical hash structure for input with key, value.
It can be used as usual custom input:
= simple_form_for do |f|
= f.input :contacts, as: :simphi
# Required fields can be passed as array of symbols or single symbol.
# It works as field which will be presetted even if 'contacts' will be empty.
= f.input :contacts, as: :simphi, required_fields: [:phone, :skype]
# Also it can be populated with options for key_input, value_input, remove_button, error, add_button
= f.input :contacts, as: :simphi, key_input: { class: 'shi_key' }, add_button: { id: 'custom_button_id', label: 'Add hash pair' }
Installation
Add this line to your application's Gemfile:
gem 'simphi'
And then execute:
$ bundle
Or install it yourself as:
$ gem install simphi
Usage
Middleware
To use middleware you need to add such lines to your config/application.rb
file:
require "simphi/middleware"
class Application < Rails::Application
config.middleware.use Simphi::Middleware
end
Simphi Input
Hash input can be used as simple_form input with as: :simphi
option. But you can reassign simphi
to another name:
# config/initializer/simphi.rb
module SimpleForm
class FormBuilder
map_type :name_which_you_want, to: Simphi::SimphiInput
end
end
But to make simphi
fully functional, assets should be included
// app/assets/javascripts/application.js
//= require simphi
// app/assets/stylesheets/application.css
*= require "simphi" // NOTE: this file is only for hidding 'sample' hash element which used for generating new hash pairs
To change value of add_button, i18n can be used:
# config/locales/en.yml
en:
simphi:
add_button: Add new hash button
remove_button: Delete hash element
Contributing
- Fork it ( https://github.com/AlexBeznos/simphi/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request