module JqueryUiForm module Helpers module InputHelper def input(method, options={}) row_options = options.delete(:html_row) || {} options[:as] ||= default_input_type(method, options) options[:required] = method_required?(method) if options[:required].nil? row_options[:class] ||= "" row_options[:class] << " ui-#{options[:as]}-input" options[:class] ||= "" options[:class] << " ui-#{options[:as]}-input" options[:class] << " ui-input-error" if has_errors?(method) if use_autofocus && (!self.autofocus) && options[:as] != :hidden options[:autofocus] = true self.autofocus = true end column(row_options) do send(:"#{options.delete(:as)}_input", method, options) end end def basic_input_helper(form_helper_method, type, method, options) #:nodoc: label_options = options.delete(:html_label) || {} label_options[:required] = options[:required] label_options[:label] = options.delete(:label) hint = options[:placeholder] = options.delete(:hint) if options.delete(:no_label) "".html_safe else label(method, label_options) end << send(respond_to?(form_helper_method) ? form_helper_method : :text_field, method, options) << inline_hint(hint, type) << inline_error(method) end def inline_hint(hint, type = nil) return "" if hint.blank? if use_i18n hint = I18n.t(hint,:scope => [:helpers, :hints]) end return "" if html5_inputs && placeholder_elements.include?(type.to_s) template.content_tag(:div, hint, :class => "ui-input-hint") end protected # Determins if the attribute should be considered required or not. # # * if the :required option isn't provided in the options hash, and the object is # an ActiveModel, true is returned # if the validates_presence_of macro has been used in the class for this attribute, or false # otherwise. def method_required?(attribute) #:nodoc: attribute_sym = attribute.to_s.sub(/_id$/, '').to_sym if @object && @object.class.respond_to?(:validators_on) !@object.class.validators_on(attribute_sym).find{|validator| (validator.kind == :presence || validator.kind == :inclusion) && (validator.options.present? ? options_require_validation?(validator.options) : true)}.nil? else false end end # Determines whether the given options evaluate to true def options_require_validation?(options) #nodoc allow_blank = options[:allow_blank] return !allow_blank unless allow_blank.nil? if_condition = !options[:if].nil? condition = if_condition ? options[:if] : options[:unless] condition = if condition.respond_to?(:call) condition.call(@object) elsif condition.is_a?(::Symbol) && @object.respond_to?(condition) @object.send(condition) else condition end if_condition ? !!condition : !condition end def default_input_type(method, options) if column = column_for(method) # Special cases where the column type doesn't map to an input method. case column.type when :string return :password if method.to_s =~ /password/ if html5_inputs return :search if method.to_s =~ /^search$/ return :phone if method.to_s =~ /^phone$|fax$|mobile/ return :url if method.to_s =~ /^url$|^website$|_url$/ return :email if method.to_s =~ /email/ # return :date if method.to_s =~ /date$/ end when :integer return :select unless options.key?(:collection) return :number when :float, :decimal return :number when :timestamp return :datetime when :date return :date when :text return :text end return :select if column.type == :string && options.key?(:collection) return column.type else if @object return :select if reflection_for(method) end return :select if options.key?(:collection) return :password if method.to_s =~ /password/ return :text if method.to_s =~ /notes/ return :file if method.to_s =~ /attachment/ if html5_inputs return :email if method.to_s =~ /email/ return :phone if method.to_s =~ /^phone$|fax$|mobile/ return :url if method.to_s =~ /^url$|^website$|_url$/ return :search if method.to_s =~ /^search$/ return :number if method.to_s =~ /^amount|size/ end return :string end end # Get a column object for a specified attribute method - if possible. def column_for(method) #:nodoc: @object.column_for_attribute(method) if @object.respond_to?(:column_for_attribute) end def reflection_for(method) #:nodoc: @object.class.reflect_on_association(method) if @object.class.respond_to?(:reflect_on_association) end def name_to_id(name) name.to_s.gsub(/\s/, "_").gsub(/\W/, "").gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "").downcase end end end end