Class: SimpleForm::Inputs::BankAccountNumberInput
- Inherits:
-
NumericInput
- Object
- NumericInput
- SimpleForm::Inputs::BankAccountNumberInput
- Defined in:
- lib/simple_form/bank_account_number.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#input ⇒ Object
requires to set :country and :value.
Class Method Details
.formatted_bank_account_number(full_number, country) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/simple_form/bank_account_number.rb', line 8 def self.formatted_bank_account_number(full_number, country) full_number = full_number.dup fields = number_format(country).map do |part| length = part[:format].source.scan(/\b\d}/).map(&:to_i).max full_number.slice!(0, length || full_number.length) end unless full_number.empty? raise ArgumentError, "format does not match (#{full_number.length} extra digits)" end fields.join("-") end |
Instance Method Details
#input ⇒ Object
requires to set :country and :value
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/simple_form/bank_account_number.rb', line 24 def input country = .delete(:country) || raise(ArgumentError, "You must set the :country key.") full_number = .fetch(:value) || "" parts = self.class.formatted_bank_account_number(full_number, country).split("-").reverse fields = self.class.number_format(country).map do |part| [:required] = "required" [:autocomplete] = "off" [:name] = "#{lookup_model_names.join("_")}[#{reflection_or_attribute_name}][]" [:pattern] = "^#{part[:format].source}$" [:title] = part[:label] [:style] = "width: auto; display: inline; margin-right: .2em" .delete(:country) # don't include it in the markup # Twitter Bootstrap tooltips ["data-toggle"] = "tooltip" ["data-original-title"] = part[:label] ["data-placement"] = "bottom" if length = part[:format].source.scan(/\b\d}/).map(&:to_i).max [:size] = length [:maxlength] = length else [:size] = nil [:maxlength] = nil end [:value] = parts.pop super end fields.join end |