mirror of
https://github.com/heartcombo/simple_form.git
synced 2022-11-09 12:19:26 -05:00
Make errors work with the new renderers.
This commit is contained in:
parent
78ff4a908b
commit
f0e0031385
6 changed files with 76 additions and 23 deletions
|
@ -10,6 +10,7 @@ module SimpleForm
|
|||
autoload :I18nCache, 'simple_form/i18n_cache'
|
||||
autoload :Inputs, 'simple_form/inputs'
|
||||
autoload :MapType, 'simple_form/map_type'
|
||||
autoload :Renderer, 'simple_form/renderer'
|
||||
|
||||
# Default tag used on hints.
|
||||
mattr_accessor :hint_tag
|
||||
|
|
|
@ -7,8 +7,14 @@ module SimpleForm
|
|||
enabled_error
|
||||
end
|
||||
|
||||
def error_tag
|
||||
options[:error_tag] || SimpleForm.error_tag
|
||||
protected
|
||||
|
||||
def enabled_error
|
||||
error_text if has_errors?
|
||||
end
|
||||
|
||||
def disabled_error
|
||||
nil
|
||||
end
|
||||
|
||||
def error_text
|
||||
|
@ -23,20 +29,6 @@ module SimpleForm
|
|||
options[:error_method] || SimpleForm.error_method
|
||||
end
|
||||
|
||||
def error_html_options
|
||||
html_options_for(:error, [SimpleForm.error_class])
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def enabled_error
|
||||
template.content_tag(error_tag, error_text, error_html_options) if has_errors?
|
||||
end
|
||||
|
||||
def disabled_error
|
||||
nil
|
||||
end
|
||||
|
||||
def errors
|
||||
@errors ||= (errors_on_attribute + errors_on_association).compact
|
||||
end
|
||||
|
|
|
@ -210,7 +210,11 @@ module SimpleForm
|
|||
options[:error_html] = options.except(:error_tag, :error_prefix, :error_method)
|
||||
column = find_attribute_column(attribute_name)
|
||||
input_type = default_input_type(attribute_name, column, options)
|
||||
SimpleForm::Inputs::Base.new(self, attribute_name, column, input_type, options).error
|
||||
SimpleForm::Renderer.new(
|
||||
:error,
|
||||
:error,
|
||||
:tag => SimpleForm.error_tag, :class => SimpleForm.error_class
|
||||
).render(SimpleForm::Inputs::Base.new(self, attribute_name, column, input_type, options))
|
||||
end
|
||||
|
||||
# Return the error but also considering its name. This is used
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
# TODO: Remove this once refactoring is done
|
||||
require 'simple_form/renderer'
|
||||
|
||||
module SimpleForm
|
||||
module Inputs
|
||||
class Base
|
||||
|
@ -64,8 +67,8 @@ module SimpleForm
|
|||
def render
|
||||
content = "".html_safe
|
||||
components_list.each do |component|
|
||||
next if options[component] == false
|
||||
rendered = send(component)
|
||||
next if options[component.namespace] == false
|
||||
rendered = component.render(self)
|
||||
content.safe_concat rendered.to_s if rendered
|
||||
end
|
||||
wrap(content)
|
||||
|
@ -82,7 +85,13 @@ module SimpleForm
|
|||
end
|
||||
|
||||
def components_list
|
||||
options[:components] || SimpleForm.components
|
||||
(options[:components] || SimpleForm.components).map do |component|
|
||||
if component == :error
|
||||
SimpleForm::Renderer.new(:error, :error, :tag => SimpleForm.error_tag, :class => SimpleForm.error_class)
|
||||
else
|
||||
component
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def has_autofocus?
|
||||
|
|
47
lib/simple_form/renderer.rb
Normal file
47
lib/simple_form/renderer.rb
Normal file
|
@ -0,0 +1,47 @@
|
|||
class Symbol
|
||||
def render(input)
|
||||
input.send(self)
|
||||
end
|
||||
|
||||
def namespace
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
module SimpleForm
|
||||
class Renderer
|
||||
attr_reader :namespace, :defaults, :components
|
||||
|
||||
def initialize(namespace, *args)
|
||||
options = args.extract_options!
|
||||
@tag = options[:tag]
|
||||
@class = Array.wrap(options[:class])
|
||||
|
||||
@namespace = namespace
|
||||
@components = args
|
||||
end
|
||||
|
||||
def render(input)
|
||||
content = "".html_safe
|
||||
options = input.options
|
||||
components.each do |component|
|
||||
next if options[component] == false
|
||||
rendered = component.render(input)
|
||||
content.safe_concat rendered.to_s if rendered
|
||||
end
|
||||
# TODO: Break this into single renderer and multiple renderer
|
||||
wrap(input, content) unless content.empty?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def wrap(input, content)
|
||||
tag = input.options[:"#{namespace}_tag"] || @tag
|
||||
return content unless tag
|
||||
|
||||
opts = input.options[:"#{namespace}_html"] || {}
|
||||
opts[:class] = (@class << opts[:class]).join(' ').strip unless @class.empty?
|
||||
input.template.content_tag(tag, content, opts)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -2,10 +2,10 @@ require 'test_helper'
|
|||
|
||||
class ErrorTest < ActionView::TestCase
|
||||
|
||||
def with_error_for(object, attribute_name, type, options={}, &block)
|
||||
def with_error_for(object, attribute_name, type, options={})
|
||||
with_concat_form_for(object) do |f|
|
||||
options[:reflection] = Association.new(Company, :company, {}) if options.delete(:setup_association)
|
||||
SimpleForm::Inputs::Base.new(f, attribute_name, nil, type, options).error.to_s
|
||||
f.error attribute_name, options.merge(:as => type)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -45,7 +45,7 @@ class ErrorTest < ActionView::TestCase
|
|||
end
|
||||
|
||||
test 'error should be able to pass html options' do
|
||||
with_error_for @user, :name, :string, :error_html => { :id => 'error', :class => 'yay' }
|
||||
with_error_for @user, :name, :string, :id => 'error', :class => 'yay'
|
||||
assert_select 'span#error.error.yay'
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue