Skip to content

Commit

Permalink
remove support namespace for inputs/basic and inputs/timeish
Browse files Browse the repository at this point in the history
  • Loading branch information
justinfrench committed Jan 19, 2011
1 parent 2f81ae4 commit 41720ed
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 182 deletions.
140 changes: 69 additions & 71 deletions lib/formtastic/inputs/basic.rb
Original file line number Diff line number Diff line change
@@ -1,84 +1,82 @@
module Formtastic
module Inputs
module Support
module Basic

def basic_input_helper(form_helper_method, type, method, options) #:nodoc:
html_options = options.delete(:input_html) || {}
html_options = default_string_options(method, type).merge(html_options) if [:numeric, :string, :password, :text, :phone, :search, :url, :email].include?(type)
field_id = generate_html_id(method, "")
html_options[:id] ||= field_id
label_options = options_for_label(options)
label_options[:for] ||= html_options[:id]
label(method, label_options) <<
send(respond_to?(form_helper_method) ? form_helper_method : :text_field, method, html_options)
end

protected

# Generates default_string_options by retrieving column information from
# the database.
#
def default_string_options(method, type) #:nodoc:
def get_maxlength_for(method)
validation = validations_for(method).find do |validation|
(validation.respond_to?(:macro) && validation.macro == :validates_length_of) || # Rails 2 + 3 style validation
(validation.respond_to?(:kind) && validation.kind == :length) # Rails 3 style validator
end

if validation
validation.options[:maximum] || (validation.options[:within].present? ? validation.options[:within].max : nil)
else
nil
end
module Basic

def basic_input_helper(form_helper_method, type, method, options) #:nodoc:
html_options = options.delete(:input_html) || {}
html_options = default_string_options(method, type).merge(html_options) if [:numeric, :string, :password, :text, :phone, :search, :url, :email].include?(type)
field_id = generate_html_id(method, "")
html_options[:id] ||= field_id
label_options = options_for_label(options)
label_options[:for] ||= html_options[:id]
label(method, label_options) <<
send(respond_to?(form_helper_method) ? form_helper_method : :text_field, method, html_options)
end

protected

# Generates default_string_options by retrieving column information from
# the database.
#
def default_string_options(method, type) #:nodoc:
def get_maxlength_for(method)
validation = validations_for(method).find do |validation|
(validation.respond_to?(:macro) && validation.macro == :validates_length_of) || # Rails 2 + 3 style validation
(validation.respond_to?(:kind) && validation.kind == :length) # Rails 3 style validator
end

validation_max_limit = get_maxlength_for(method)
column = column_for(method)

if type == :text
{ :rows => default_text_area_height,
:cols => default_text_area_width }
elsif type == :numeric || column.nil? || !column.respond_to?(:limit) || column.limit.nil?
{ :maxlength => validation_max_limit,
:size => default_text_field_size }

if validation
validation.options[:maximum] || (validation.options[:within].present? ? validation.options[:within].max : nil)
else
{ :maxlength => validation_max_limit || column.limit,
:size => default_text_field_size }
nil
end
end

# Returns the active validations for the given method or an empty Array if no validations are
# found for the method.
#
# By default, the if/unless options of the validations are evaluated and only the validations
# that should be run for the current object state are returned. Pass :all to the last
# parameter to return :all validations regardless of if/unless options.
#
# Requires the ValidationReflection plugin to be present or an ActiveModel. Returns an epmty
# Array if neither is the case.
#
def validations_for(method, mode = :active)
# ActiveModel?
validations = if @object && @object.class.respond_to?(:validators_on)
@object.class.validators_on(method)

validation_max_limit = get_maxlength_for(method)
column = column_for(method)

if type == :text
{ :rows => default_text_area_height,
:cols => default_text_area_width }
elsif type == :numeric || column.nil? || !column.respond_to?(:limit) || column.limit.nil?
{ :maxlength => validation_max_limit,
:size => default_text_field_size }
else
{ :maxlength => validation_max_limit || column.limit,
:size => default_text_field_size }
end
end

# Returns the active validations for the given method or an empty Array if no validations are
# found for the method.
#
# By default, the if/unless options of the validations are evaluated and only the validations
# that should be run for the current object state are returned. Pass :all to the last
# parameter to return :all validations regardless of if/unless options.
#
# Requires the ValidationReflection plugin to be present or an ActiveModel. Returns an epmty
# Array if neither is the case.
#
def validations_for(method, mode = :active)
# ActiveModel?
validations = if @object && @object.class.respond_to?(:validators_on)
@object.class.validators_on(method)
else
# ValidationReflection plugin?
if @object && @object.class.respond_to?(:reflect_on_validations_for)
@object.class.reflect_on_validations_for(method)
else
# ValidationReflection plugin?
if @object && @object.class.respond_to?(:reflect_on_validations_for)
@object.class.reflect_on_validations_for(method)
else
[]
end
[]
end

validations = validations.select do |validation|
(validation.options.present? ? options_require_validation?(validation.options) : true)
end unless mode == :all

return validations
end


validations = validations.select do |validation|
(validation.options.present? ? options_require_validation?(validation.options) : true)
end unless mode == :all

return validations
end

end
end
end
2 changes: 1 addition & 1 deletion lib/formtastic/inputs/date_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Formtastic
module Inputs
module DateInput
include Formtastic::Inputs::Base
include Support::Timeish
include Formtastic::Inputs::Timeish

# Outputs a fieldset with a legend for the method label, and a ordered list (ol) of list
# items (li), one for each fragment for the date (year, month, day). Each li contains a label
Expand Down
2 changes: 1 addition & 1 deletion lib/formtastic/inputs/datetime_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Formtastic
module Inputs
module DatetimeInput
include Formtastic::Inputs::Base
include Support::Timeish
include Formtastic::Inputs::Timeish

# Outputs a fieldset with a legend for the method label, and a ordered list (ol) of list
# items (li), one for each fragment for the date (year, month, day, hour, min, sec). Each li
Expand Down
2 changes: 1 addition & 1 deletion lib/formtastic/inputs/email_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Formtastic
module Inputs
module EmailInput
include Formtastic::Inputs::Base
include Support::Basic
include Formtastic::Inputs::Basic

# Outputs a label and a standard Rails email field inside the wrapper.
def email_input(method, options)
Expand Down
2 changes: 1 addition & 1 deletion lib/formtastic/inputs/file_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Formtastic
module Inputs
module FileInput
include Formtastic::Inputs::Base
include Support::Basic
include Formtastic::Inputs::Basic

# Outputs a label and a standard Rails file field inside the wrapper.
def file_input(method, options)
Expand Down
2 changes: 1 addition & 1 deletion lib/formtastic/inputs/numeric_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Formtastic
module Inputs
module NumericInput
include Formtastic::Inputs::Base
include Support::Basic
include Formtastic::Inputs::Basic

# Outputs a label and standard Rails text field inside the wrapper.
def numeric_input(method, options)
Expand Down
2 changes: 1 addition & 1 deletion lib/formtastic/inputs/password_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Formtastic
module Inputs
module PasswordInput
include Formtastic::Inputs::Base
include Support::Basic
include Formtastic::Inputs::Basic

# Outputs a label and standard Rails password field inside the wrapper.
def password_input(method, options)
Expand Down
2 changes: 1 addition & 1 deletion lib/formtastic/inputs/phone_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Formtastic
module Inputs
module PhoneInput
include Formtastic::Inputs::Base
include Support::Basic
include Formtastic::Inputs::Basic

# Outputs a label and a standard Rails phone field inside the wrapper.
def phone_input(method, options)
Expand Down
2 changes: 1 addition & 1 deletion lib/formtastic/inputs/search_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Formtastic
module Inputs
module SearchInput
include Formtastic::Inputs::Base
include Support::Basic
include Formtastic::Inputs::Basic

# Outputs a label and a standard Rails search field inside the wrapper.
def search_input(method, options)
Expand Down
2 changes: 1 addition & 1 deletion lib/formtastic/inputs/string_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Formtastic
module Inputs
module StringInput
include Formtastic::Inputs::Base
include Support::Basic
include Formtastic::Inputs::Basic
# Outputs a label and standard Rails text field inside the wrapper.
def string_input(method, options)
basic_input_helper(:text_field, :string, method, options)
Expand Down
2 changes: 1 addition & 1 deletion lib/formtastic/inputs/text_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Formtastic
module Inputs
module TextInput
include Support::Basic
include Formtastic::Inputs::Basic

# Ouputs a label and standard Rails text area inside the wrapper.
def text_input(method, options)
Expand Down
2 changes: 1 addition & 1 deletion lib/formtastic/inputs/time_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Formtastic
module Inputs
module TimeInput
include Support::Timeish
include Formtastic::Inputs::Timeish
# Outputs a fieldset with a legend for the method label, and a ordered list (ol) of list
# items (li), one for each fragment for the time (hour, minute, second). Each li contains a label
# (eg "Hour") and a select box. Overwriting the label is possible by adding the :labels option.
Expand Down
Loading

0 comments on commit 41720ed

Please sign in to comment.