2016-02-12 2 views
1

Вместо того, чтобы иметь почти два одинаковых пользовательских описания обертки, можно ли их высушить? для например:Высушить пользовательские обертки в simple_form

config.wrappers :inline_checkbox, :tag => 'div', :class => 'control-group', :error_class => 'error' do |b| 
    b.use :html5 
    b.wrapper :tag => 'div', :class => 'controls' do |ba| 
     ba.use :label_input, :wrap_with => { :class => 'checkbox inline' } 
     ba.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }   
    end 
    end 

и

config.wrappers :inline_checkbox_two, :tag => 'div', :class => 'control-group', :error_class => 'error' do |b| 
    b.use :hint, :wrap_with => { :tag => 'p', :class => 'help-block' } 
    # everything else should use the same definition as the above 
    end 

ответ

1

Используйте модуль СУХОЙ его:

module WrapperHelper 

    # options: 
    # - wrapper [Hash] 
    # - label_input [Hash] 
    # - error [Hash] 
    # - use [Array] an array of arguments to pass to b.use 
    def self.inline_checkbox(b, **kwargs) 
    if kwargs[:use] 
     b.use *kwargs[:use] 
    else 
     b.use :html5 
    end 
    wrapper_opts = { 
     tag: 'div', 
     class: 'control-group' 
     error_class: 'error' 
    }.merge(kwargs[:wrapper] || {}) # merge defaults with passed `wrapper` keyword argument 
    b.wrapper(wrapper_opts) do |ba| 
     ba.use :label_input, 
     { wrap_with: { class: 'checkbox inline' } }.merge(kwargs[:label_input] || {}) 
     ba.use :error, 
     { wrap_with: { tag: 'span', class: 'help-inline' } }.merge(kwargs[:error] || {}) 
    end 
    end 
end 

config.wrappers :html5_inline_checkbox do |b| 
    WrapperHelper.inline_checkbox(b) 
end 

config.wrappers :inline_checkbox_with_helper do |b| 
    WrapperHelper.inline_checkbox(b, use: [:hint, {wrap_with: { tag: 'p', class: 'help-block' }}]) 
end 
+0

Это использует немного деструктуризации и рубин 2.0 именованные аргументы. Подробнее об этом можно узнать здесь: http://tony.pitluga.com/2011/08/08/destructuring-with-ruby.html и здесь: https://robots.thoughtbot.com/ruby-2-keyword- аргументы – max

Смежные вопросы