Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Add rendering check for namespaced components #675

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
2 participants

coreyti commented Oct 10, 2012

given a namespaced, complex wrapper:

b.wrapper :error, :tag => 'div', :class => 'tooltip' do |tip|
  e.wrapper :tag => 'div', :class => 'inner' do |inner|
    inner.use :error, :wrap_with => {
      :tag => 'span',
      :class => 'error'
    }
  end
end

if input responds to the namespace and it responds with nil, the entire component will not be rendered.

without this change, the following html would be rendered when there is no error:

<div class="tooltip">
  <div class="inner"></div>
</div>

with this change, input.error will respond with nil when there are no errors and the error/tooltip html will not, therefore, be rendered.

Add rendering check for namespaced components
given a namespaced, complex wrapper:

    b.wrapper :error, :tag => 'div', :class => 'tooltip' do |tip|
      e.wrapper :tag => 'div', :class => 'inner' do |inner|
        inner.use :error, :wrap_with => {
          :tag => 'span',
          :class => 'error'
      end
    end

if input responds to the namespace and it responds **with** nil, the entire component will not be rendered.

without this change, the following html would be rendered
when there is no error:

    <div class="tooltip">
      <div class="inner"></div>
    </div>

coreyti commented Oct 10, 2012

some alternatives, if it's unacceptable to add this implicit behavior to a wrapper's namespace:

  1. repurpose the deprecated support for passing a block to use, such that the block could be passed the input object for evaluation. this would break existing uses of the deprecated behavior.
  2. allow :wrap_with to reference a Proc/lambda with the same evaluation capabilities.
Collaborator

rafaelfranca commented Oct 14, 2012

How about changing the error component in your application to render this markup?

Collaborator

rafaelfranca commented Oct 14, 2012

Something like this:

module SimpleForm
  module Components
    module Errors
      def error
        if has_errors?
          template.content_tag(:div, class: 'tooltip') do
            template.content_tag(:div, class: 'inner') do
              template.content_tag(:span, error_text, class: 'error')
            end
          end
        end
      end
    end
  end
end

coreyti commented Oct 14, 2012

Ah! I really like that approach. Thank you @rafaelfranca!

@coreyti coreyti closed this Oct 14, 2012

Collaborator

rafaelfranca commented Oct 14, 2012

No problem. Glad I could help.

Between sorry for the delay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment