Let's call those thing wrappers.

This commit is contained in:
José Valim 2011-09-02 20:33:03 +02:00
parent f0e0031385
commit f62667ce61
9 changed files with 119 additions and 66 deletions

View File

@ -2,6 +2,18 @@ require 'action_view'
require 'simple_form/action_view_extensions/form_helper'
require 'simple_form/action_view_extensions/builder'
# TODO: This is temporary while we refactor some stuff.
class Symbol
def render(input)
input.send(self)
end
def namespace
self
end
end
module SimpleForm
autoload :Components, 'simple_form/components'
autoload :ErrorNotification, 'simple_form/error_notification'
@ -10,7 +22,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'
autoload :Wrappers, 'simple_form/wrappers'
# Default tag used on hints.
mattr_accessor :hint_tag
@ -44,10 +56,6 @@ module SimpleForm
mattr_accessor :error_notification_id
@@error_notification_id = nil
# Components used by the form builder.
mattr_accessor :components
@@components = [ :placeholder, :label_input, :hint, :error ]
# Series of attemps to detect a default label method for collection.
mattr_accessor :collection_label_methods
@@collection_label_methods = [ :to_label, :name, :title, :to_s ]
@ -143,4 +151,14 @@ module SimpleForm
def self.setup
yield self
end
## DEPRECATED STUFF
def self.components=(array)
@@components = SimpleForm::Wrappers.wrap(array)
end
# Components used by the form builder.
mattr_reader :components
self.components = [ :placeholder, :label_input, :hint, :error ]
end

View File

@ -210,11 +210,8 @@ 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::Renderer.new(
:error,
:error,
:tag => SimpleForm.error_tag, :class => SimpleForm.error_class
).render(SimpleForm::Inputs::Base.new(self, attribute_name, column, input_type, options))
SimpleForm::Wrappers.find(:error).
render(SimpleForm::Inputs::Base.new(self, attribute_name, column, input_type, options))
end
# Return the error but also considering its name. This is used

View File

@ -1,6 +1,3 @@
# TODO: Remove this once refactoring is done
require 'simple_form/renderer'
module SimpleForm
module Inputs
class Base
@ -85,12 +82,10 @@ module SimpleForm
end
def components_list
(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
if components = options[:components]
SimpleForm::Wrappers.wrap(components)
else
SimpleForm.components
end
end

View File

@ -1,47 +0,0 @@
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

View File

@ -0,0 +1,22 @@
module SimpleForm
module Wrappers
autoload :Many, 'simple_form/wrappers/many'
autoload :Root, 'simple_form/wrappers/root'
autoload :Single, 'simple_form/wrappers/single'
def self.find(name)
SimpleForm.components.find { |c| c.namespace == name } || SingleForm::Wrappers::Anonym.new(name)
end
def self.wrap(array)
array.map do |item|
case item
when :error
Single.new(:error, :tag => SimpleForm.error_tag, :class => SimpleForm.error_class)
else
item
end
end
end
end
end

View File

@ -0,0 +1,13 @@
module SimpleForm
module Wrappers
class Anonym < Many
def initialize(*args)
super(nil, *args)
end
def wrap(input, options, content)
content
end
end
end
end

View File

@ -0,0 +1,40 @@
module SimpleForm
module Wrappers
class Many
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
wrap(input, options, content)
end
private
def wrap(input, options, content)
tag = options[:"#{namespace}_tag"] || @tag
return content unless tag
opts = options[:"#{namespace}_html"] || {}
opts[:class] = (@class << opts[:class]).join(' ').strip unless @class.empty?
input.template.content_tag(tag, content, opts)
end
end
end
end

View File

View File

@ -0,0 +1,15 @@
module SimpleForm
module Wrappers
class Single < Many
def render(input)
options = input.options
if options[namespace] == false
nil
else
content = input.send(namespace)
wrap(input, options, content) if content
end
end
end
end
end