Skip to content

Commit

Permalink
Add configuration to add class globally to inputs.
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca committed Sep 27, 2011
1 parent c30d832 commit 7ff302b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
Expand Up @@ -85,4 +85,7 @@


# Default class for buttons # Default class for buttons
# config.button_class = 'button' # config.button_class = 'button'

# Default class for inputs
# config.input_class = nil
end end
6 changes: 5 additions & 1 deletion lib/simple_form.rb
Expand Up @@ -112,6 +112,10 @@ module SimpleForm
mattr_accessor :button_class mattr_accessor :button_class
@@button_class = 'button' @@button_class = 'button'


# Adds a class to each generated inputs
mattr_accessor :input_class
@@input_class = nil

## WRAPPER CONFIGURATION ## WRAPPER CONFIGURATION
@@wrappers = {} @@wrappers = {}


Expand Down Expand Up @@ -166,4 +170,4 @@ def self.setup
"Updating to the new API is easy and fast. Check for more info here: https://github.com/plataformatec/simple_form/wiki/Upgrading-to-Simple-Form-2.0" "Updating to the new API is easy and fast. Check for more info here: https://github.com/plataformatec/simple_form/wiki/Upgrading-to-Simple-Form-2.0"
end end
end end
end end
8 changes: 7 additions & 1 deletion lib/simple_form/inputs/base.rb
Expand Up @@ -58,7 +58,7 @@ def input_options
end end


def input_html_classes def input_html_classes
[input_type, required_class, disabled_class].compact [input_type, required_class, disabled_class, input_class].compact
end end


def has_autofocus? def has_autofocus?
Expand All @@ -75,6 +75,12 @@ def limit
column && column.limit column && column.limit
end end


def input_class
return if options[:input_class] == false

SimpleForm.input_class
end

# Find reflection name when available, otherwise use attribute # Find reflection name when available, otherwise use attribute
def reflection_or_attribute_name def reflection_or_attribute_name
reflection ? reflection.name : attribute_name reflection ? reflection.name : attribute_name
Expand Down
14 changes: 14 additions & 0 deletions test/inputs/general_test.rb
Expand Up @@ -15,6 +15,20 @@ class InputTest < ActionView::TestCase
assert_select 'select.datetime' assert_select 'select.datetime'
end end


test 'input should accepts input class configuration' do
swap SimpleForm, :input_class => :xlarge do
with_input_for @user, :name, :string
assert_select 'input.xlarge'
end
end

test 'input should disable the input class configuration with an option' do
swap SimpleForm, :input_class => :xlarge do
with_input_for @user, :name, :string, :input_class => false
assert_no_select 'input.xlarge'
end
end

test 'input should generate autofocus attribute based on the autofocus option' do test 'input should generate autofocus attribute based on the autofocus option' do
with_input_for @user, :name, :string, :autofocus => true with_input_for @user, :name, :string, :autofocus => true
assert_select 'input.string[autofocus]' assert_select 'input.string[autofocus]'
Expand Down

0 comments on commit 7ff302b

Please sign in to comment.