Skip to content

Instantly share code, notes, and snippets.

@23inhouse
Created September 2, 2012 05:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 23inhouse/3594973 to your computer and use it in GitHub Desktop.
Save 23inhouse/3594973 to your computer and use it in GitHub Desktop.
1 more thing

Further customization

bundle open simple_form
<!-- app/view/blogs/_form.html.erb -->

<%= simple_form_for @blog, :html => { :class => 'form-horizontal' } do |f| %>

  <%= f.input_wrapper do |w| %>
    <%= w.content :append do |a| %>
      <div class="help-block">Some extra content inside the error wrapper</div>
    <% end %>
    <%= f.input :post_on, :wrapper => :bootstrap_block_content %>
  <% end %>

<% end %>
# config/initializers/simple_form/form_builder.rb

module SimpleForm
  module Extensions
    module Methods

      attr_accessor :block_content

      def input_wrapper(&block)
        @block_content = {}
        yield self
        nil
      end

      def block_content(component, &block)
        raise WrapperNotFound, "Use <% f.input_wrapper do |w| %>" if @block_content.nil?
        if block_given?
          @block_content[component] = template.capture(object, &block)
        end
        nil
      end
      alias_method :content, :block_content

      def block_content
        @block_content
      end
    end
  end
end


SimpleForm::FormBuilder.send(:include, SimpleForm::Extensions::Methods)
# config/initializers/simple_form/content_component.rb

module SimpleForm
  module Components
    module Append
      def append
        if @builder.block_content[:append].present?
          append_content = @builder.block_content[:append]
          return append_content.html_safe if append_content
        end

        if options[:append].present?
          append = options[:append]
          append_content = append.is_a?(String) ? append : translate(:appends)
          append_content.html_safe if append_content
        end
      end
    end
  end
end

SimpleForm::Inputs::Base.send(:include, SimpleForm::Components::Append)
# config/initializers/simple_form.rb

SimpleForm.setup do |config|
  config.wrappers :bootstrap_block_content, :tag => 'div', :class => 'control-group', :error_class => 'error' do |b|
    b.use :placeholder
    b.use :append
    b.use :label
    b.wrapper :tag => 'div', :class => 'controls' do |input|
      input.use :input
      input.use :error, :wrap_with => { :tag => 'p', :class => 'help-block' }
      input.use :hint,  :wrap_with => { :tag => 'p', :class => 'help-block' }
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment