# Effective Form Inputs Multiple custom Rails Form Helper and SimpleForm inputs in one Rails engine. This gem contains numerous custom form inputs along with their Javascript/CSS assets. Each included form input is available to both the default Rails Form Helper and SimpleForm. Rails 3.2.x and 4.x ## Getting Started Add to your Gemfile: ```ruby gem 'effective_form_inputs' ``` Run the bundle command to install it: ```console bundle install ``` ### Install All Form Inputs This gem packages the javascript/css assets for numerous form inputs. The assets for these inputs may be included all at once or individually. To install all available inputs, add the following to your application.js: ```ruby //= require effective_form_inputs ``` and add the following to your application.css: ```ruby *= require effective_form_inputs ``` All of the included form inputs will now be available with no additional installation tasks. ### Options Passing to JavaScript All `:input_js => options` passed to any effective_form_input will be used to initialize the Javascript library For example: ```ruby = form_for @user do |f| = f.effective_date_time_picker :updated_at, :input_js => {:format => 'dddd, MMMM Do YYYY', :showTodayButton => true} ``` or ```ruby = simple_form_for @user do |f| = f.input :updated_at, :as => :effective_date_time_picker, :input_js => {:format => 'dddd, MMMM Do YYYY', :showTodayButton => true} ``` will result in the following call to the Javascript library: ```coffee $('input.effective_date_time_picker').datetimepicker format: 'dddd, MMMM Do YYYY', showTodayButton: true ``` Any options passed in this way will be used to initialize the underlying javascript libraries. ## Effective Date Time Picker This custom form input is based on the following awesome project: Bootstrap 3 Datepicker (https://github.com/Eonasdan/bootstrap-datetimepicker) ### Installation If you've already installed the 'All Form Inputs' assets (see above), there are no additional installation steps. To install this form input individually, add the following to your application.js: ```ruby //= require effective_date_time_picker/input ``` and add the following to your application.css: ```ruby *= require effective_date_time_picker/input ``` ### Usage As a Rails Form Helper input: ```ruby = form_for @user do |f| = f.effective_date_time_picker :updated_at ``` As a SimpleForm input: ```ruby = simple_form_for @user do |f| = f.input :updated_at, :as => :effective_date_time_picker ``` As a SimpleForm input without the input group (calendar glyphicon) ```ruby = simple_form_for @user do |f| = f.input :updated_at, :as => :effective_date_time_picker, :input_group => false ``` ### Options The default `:input_js => options` used to initialize this form input are as follows: ```ruby :input_js => {:format => 'YYYY-MM-DD h:mm A', :sideBySide => true} ``` For a full list of options, please refer to: http://eonasdan.github.io/bootstrap-datetimepicker/Options/ ## Effective Date Picker This custom form input is based on the following awesome project: Bootstrap 3 Datepicker (https://github.com/Eonasdan/bootstrap-datetimepicker) ### Installation If you've already installed the 'All Form Inputs' assets (see above), there are no additional installation steps. To install this form input individually, add the following to your application.js: ```ruby //= require effective_date_picker/input ``` and add the following to your application.css: ```ruby *= require effective_date_picker/input ``` ### Usage As a Rails Form Helper input: ```ruby = form_for @user do |f| = f.effective_date_picker :started_on ``` As a SimpleForm input: ```ruby = simple_form_for @user do |f| = f.input :started_on, :as => :effective_date_picker ``` As a SimpleForm input without the input group (calendar glyphicon) ```ruby = simple_form_for @user do |f| = f.input :updated_at, :as => :effective_date_picker, :input_group => false ``` ### Options The default `:input_js => options` used to initialize this form input are as follows: ```ruby :input_js => {:format => 'YYYY-MM-DD'} ``` For a full list of options, please refer to: http://eonasdan.github.io/bootstrap-datetimepicker/Options/ ### Set Date Use the following JavaScript to programatically set the date: ```javascript $('#start_at').data('DateTimePicker').date('2016-05-08') ``` ## Effective Email This custom form input makes sure the input is an email address. ### Installation If you've already installed the 'All Form Inputs' assets (see above), there are no additional installation steps. To install this form input individually, add the following to your application.js: ```ruby //= require effective_email/input ``` ### Usage As a Rails Form Helper input: ```ruby = form_for @user do |f| = f.effective_email :email ``` As a SimpleForm input: ```ruby = simple_form_for @user do |f| = f.input :email, :as => :effective_email ``` As a SimpleForm input without the input group (envelope glyphicon) ```ruby = simple_form_for @user do |f| = f.input :email, :as => :effective_email, :input_group => false ``` You should add a server side validation to enforce the format: ```ruby validates :email, format: { with: /\A.+@.+\..+\Z/ } validates :email, effective_email: true # Enforces same format as above ``` ### Options There are no javascript options for this input. ## Effective Static Control A bootstrap3 Static control input Renders `
value
` with the appropriate SimpleForm wrappings. ### Installation There are no installation steps required for this form input ### Usage As a Rails Form Helper input: ```ruby = form_for @user do |f| = f.effective_static_control :member_id ``` As a SimpleForm input: ```ruby = simple_form_for @user do |f| = f.input :member_id, :as => :effective_static_control ``` ### Options There are no default options for this form input ## Effective Select This custom form input is based on the following awesome project: Select2 (https://select2.github.io/) ### Installation If you've already installed the 'All Form Inputs' assets (see above), there are no additional installation steps. To install this form input individually, add the following to your application.js: ```ruby //= require effective_select/input ``` and add the following to your application.css: ```ruby *= require effective_select/input ``` ### Usage As a Rails Form Helper input: ```ruby = form_for @user do |f| = f.effective_select :category, 10.times.map { |x| "Category #{x}"} = f.effective_select :categories, 10.times.map { |x| "Category #{x}"}, :multiple => true = f.effective_select :categories, 10.times.map { |x| "Category #{x}"}, :tags => true ``` and as a SimpleForm input: ```ruby = simple_form_for @user do |f| = f.input :category, :as => :effective_select, :collection => 10.times.map { |x| "Category #{x}"} = f.input :categories, :as => :effective_select, :collection => 10.times.map { |x| "Category #{x}"}, :multiple => true = f.input :categories, :as => :effective_select, :collection => 10.times.map { |x| "Category #{x}"}, :tags => true ``` ### Modes The standard mode is a replacement for the default single select box. Passing `:multiple => true` will allow multiple selections to be made. Passing `:multiple => true, :tags => true` will allow multiple selections to be made, and new value options to be created. This will allow you to both select existing tags and create new tags in the same form control. Passing `:grouped => true` will enable optgroup support. When in this mode, the collection should be a Hash of ActiveRecord Relations or Array of Arrays ```ruby :collection => {'Active' => Post.active, 'Past' => Post.past} :collection => {'Active' => [['Post A', 1], ['Post B', 2]], 'Past' => [['Post C', 3], ['Post D', 4]]} ``` Passing `:polymorphic => true` will enable polymorphic support. In this mode, an additional 2 hidden input fields are created alongside the select field. So calling ```ruby = f.input :primary_contact, :polymorphic => true, :collection => User.all.to_a + Member.all.to_a ``` will internally translate the collection into: ```ruby [['User 1', 'User_1'], ['User 2', 'User_2'], ['Member 100', 'Member_100']] ``` and instead of posting to the server with the parameter `:primary_contact`, it will intead post `{:primary_contact_id => 2, :primary_contact_type => 'User'}`. Using both `:polymorphic => true` and `:grouped => true` is recommended. In this case the expected collection is as follows: ```ruby = f.input :primary_contact, :polymorphic => true, :grouped => true, :collection => {'Users' => User.all, 'Members' => 'Member.all'} ``` ### Options The default `:input_js => options` used to initialize this form input are as follows: ```ruby { :theme => 'bootstrap', :minimumResultsForSearch => 6, :tokenSeparators => [',', ' '], :width => 'style', :placeholder => 'Please choose', :allowClear => !(options[:multiple]) # Only display the Clear 'x' on a single selection box } ``` ### Interesting Available Options To limit the number of items that can be selected in a multiple select box: ```ruby :maximumSelectionLength => 2 ``` To hide the search box entirely: ```ruby :minimumResultsForSearch => 'Infinity' ``` For a full list of options, please refer to: https://select2.github.io/options.html The following `:input_js => options` are not part of the standard select2 API, and are custom `effective_select` functionality only: To add a css class to the select2 container or dropdown: ```ruby :containerClass => 'custom-container-class' :dropdownClass => 'custom-dropdown-class' ``` ### Additional Call with `single_selected: true` to ensure only the first selected option tag will be `