Skip to content

Commit

Permalink
Move placeholders to a component.
Browse files Browse the repository at this point in the history
This commit changes Inputs::Base to handle input_html_options on initialization and allows components to return nil.
  • Loading branch information
josevalim committed Nov 7, 2010
1 parent f32b518 commit ebde237
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 57 deletions.
2 changes: 1 addition & 1 deletion lib/generators/simple_form/templates/simple_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
SimpleForm.setup do |config|
# Components used by the form builder to generate a complete input. You can remove
# any of them, change the order, or even add your own components to the stack.
# config.components = [ :label_input, :hint, :error ]
# config.components = [ :placeholder, :label_input, :hint, :error ]

# Default tag used on hints.
# config.hint_tag = :span
Expand Down
2 changes: 1 addition & 1 deletion lib/simple_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module SimpleForm

# Components used by the form builder.
mattr_accessor :components
@@components = [ :label_input, :hint, :error ]
@@components = [ :placeholder, :label_input, :hint, :error ]

# Series of attemps to detect a default label method for collection.
mattr_accessor :collection_label_methods
Expand Down
11 changes: 6 additions & 5 deletions lib/simple_form/components.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module SimpleForm
module Components
autoload :Errors, 'simple_form/components/errors'
autoload :Hints, 'simple_form/components/hints'
autoload :LabelInput, 'simple_form/components/label_input'
autoload :Labels, 'simple_form/components/labels'
autoload :Wrapper, 'simple_form/components/wrapper'
autoload :Errors, 'simple_form/components/errors'
autoload :Hints, 'simple_form/components/hints'
autoload :LabelInput, 'simple_form/components/label_input'
autoload :Labels, 'simple_form/components/labels'
autoload :Placeholders, 'simple_form/components/placeholders'
autoload :Wrapper, 'simple_form/components/wrapper'
end
end
22 changes: 22 additions & 0 deletions lib/simple_form/components/placeholders.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module SimpleForm
module Components
module Placeholders
def placeholder
input_html_options[:placeholder] ||= placeholder_text if has_placeholder?
nil
end

def has_placeholder?
false
end

def placeholder_present?
options[:placeholder] != false && placeholder_text.present?
end

def placeholder_text
@placeholder ||= options[:placeholder] || translate(:placeholders)
end
end
end
end
33 changes: 12 additions & 21 deletions lib/simple_form/inputs/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,22 @@ class Base
include SimpleForm::Components::Errors
include SimpleForm::Components::Hints
include SimpleForm::Components::LabelInput
include SimpleForm::Components::Placeholders
include SimpleForm::Components::Wrapper

attr_reader :attribute_name, :column, :input_type, :options
attr_reader :attribute_name, :column, :input_type, :options, :input_html_options

delegate :template, :object, :object_name, :reflection, :to => :@builder

def initialize(builder, attribute_name, column, input_type, options = {})
@builder = builder
@attribute_name = attribute_name
@column = column
@input_type = input_type
@options = options
@builder = builder
@attribute_name = attribute_name
@column = column
@input_type = input_type
@options = options
@input_html_options = html_options_for(:input, input_html_classes).tap do |o|
o[:required] = true if attribute_required?
end
end

def input
Expand All @@ -34,12 +38,6 @@ def input_options
options
end

def input_html_options
html_options = html_options_for(:input, input_html_classes)
html_options[:required] = true if attribute_required?
html_options
end

def input_html_classes
[input_type, required_class]
end
Expand All @@ -48,7 +46,8 @@ def render
content = "".html_safe
components_list.each do |component|
next if options[component] == false
content.safe_concat send(component).to_s
rendered = send(component)
content.safe_concat rendered.to_s if rendered
end
wrap(content)
end
Expand Down Expand Up @@ -81,14 +80,6 @@ def reflection_validators
reflection ? object.class.validators_on(reflection.name) : []
end

def has_placeholder?
options[:placeholder] != false && placeholder.present?
end

def placeholder
@placeholder ||= options[:placeholder] || translate(:placeholders)
end

def attribute_required_by_default?
SimpleForm.required_by_default
end
Expand Down
8 changes: 1 addition & 7 deletions lib/simple_form/inputs/mapping_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ def input
@builder.send(input_method, attribute_name, input_html_options)
end

def input_html_options
input_options = super
input_options[:placeholder] ||= placeholder if has_placeholder?
input_options
end

private

def input_method
Expand All @@ -26,7 +20,7 @@ def input_method
end

def has_placeholder?
(text? || password?) && super
(text? || password?) && placeholder_present?
end

def password?
Expand Down
24 changes: 11 additions & 13 deletions lib/simple_form/inputs/numeric_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,31 @@ module SimpleForm
module Inputs
class NumericInput < Base
def input
input_html_options[:type] ||= "number"
input_html_options[:size] ||= SimpleForm.default_input_size
input_html_options[:step] ||= 1 if integer?
infer_attributes_from_validations!
@builder.text_field(attribute_name, input_html_options)
end

def input_html_options
input_options = super
input_options[:type] ||= "number"
input_options[:size] ||= SimpleForm.default_input_size
input_options[:step] ||= 1 if integer?
input_options[:placeholder] ||= placeholder if has_placeholder?
infer_attributes_from_validations(input_options)
input_options
end

def input_html_classes
super.unshift("numeric")
end

protected

def infer_attributes_from_validations(input_options)
def has_placeholder?
placeholder_present?
end

def infer_attributes_from_validations!
return unless has_validators?

numeric_validator = find_numericality_validator or return
validator_options = numeric_validator.options

input_options[:min] ||= minimum_value(validator_options)
input_options[:max] ||= maximum_value(validator_options)
input_html_options[:min] ||= minimum_value(validator_options)
input_html_options[:max] ||= maximum_value(validator_options)
end

def integer?
Expand Down
17 changes: 8 additions & 9 deletions lib/simple_form/inputs/string_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ module SimpleForm
module Inputs
class StringInput < Base
def input
@builder.text_field(attribute_name, input_html_options)
end
input_html_options[:size] ||= [limit, SimpleForm.default_input_size].compact.min
input_html_options[:maxlength] ||= limit if limit
input_html_options[:type] ||= input_type unless input_type == :string

def input_html_options
input_options = super
input_options[:size] ||= [limit, SimpleForm.default_input_size].compact.min
input_options[:maxlength] ||= limit if limit
input_options[:type] ||= input_type unless input_type == :string
input_options[:placeholder] ||= placeholder if has_placeholder?
input_options
@builder.text_field(attribute_name, input_html_options)
end

def input_html_classes
Expand All @@ -23,6 +18,10 @@ def input_html_classes
def limit
column && column.limit
end

def has_placeholder?
placeholder_present?
end
end
end
end

0 comments on commit ebde237

Please sign in to comment.