Skip to content

Instantly share code, notes, and snippets.

@ivan-leschinsky
Last active August 29, 2015 14:22
Show Gist options
  • Save ivan-leschinsky/dcecf75037a4a41cbb0c to your computer and use it in GitHub Desktop.
Save ivan-leschinsky/dcecf75037a4a41cbb0c to your computer and use it in GitHub Desktop.
Masked input helper for simple_form
// Requires http://digitalbush.com/projects/masked-input-plugin
element = $('[data-behaviour=masked-input]')
element.mask(element.data('mask'))
class MaskedInput < SimpleForm::Inputs::Base
# Generates masked input with the desired mask
#
# Params:
#
# mask - mask that will be tranfered to the masked input java script library, decimal mask used by default
#
# Examples:
#
# = f.input :multiplier, as: :masked, mask: '?9.3'
#
# <input class="form-control" data-behaviour="masked-input" data-mask="?9.9"
# id="model_multiplier" name="model[multiplier]"
# step="any" type="string" value="1.0" data-components-host="true">
#
def input(wrapper_options)
options = {
class: 'form-control',
type: :string,
data: {
behaviour: 'masked-input',
mask: input_options[:mask] || '?9.9'
}
}
options[:value] = input_options[:value] if input_options[:value].present?
@builder.input_field(attribute_name, options)
end
end
@ivan-leschinsky
Copy link
Author

Requires Masked Input js library

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment