Sha256: 3bfbfb38e1874690d60542f277637ab625365a519ce08f979bec4c9b88ff2a7d

Contents?: true

Size: 1.91 KB

Versions: 1

Compression:

Stored size: 1.91 KB

Contents

require 'polyblock'

module Polyblock
  module SimpleFormHelper
    
    # def self.simple_fields_for_polyblock(fb, title, *args, &block)
    #   ( '<div class="polyblock-fields">' <<
    #       fb.simple_fields_for(title, *args, &block) << '</div>' ).html_safe
    # end
    # def simple_fields_for_polyblock(fb, title, *args, &block)
    #   if title.is_a? FormBuilder
    #     return retrofit_simple_fields_for_polyblock(fb, title, *args, &block)
    #   end
    #   pb = pb_from_name(name, fb)
    #   new_block = block_given? ? block : ->(fb2) {
    #     render partial: 'polyblock/simple_fields_for',
    #            locals: { f: fb2 } }
    #   Polyblock::SimpleFormHelper.simple_fields_for_polyblock(
    #     fb, title, *args, &new_block)
    # end

    # Retrofit method
    # def retrofit_simple_fields_for_polyblock(name, f, options={}, &block)
    def simple_fields_for_polyblock(name, f, options={}, &block)
      pb = pb_from_name(name, f)
      settings = {
        :field => {
          :as => :ckeditor,
          :label => name.to_s.titleize,
          :input_html => {
            :value => pb.present? ? pb.content : nil
          }
        },
        :use_input_field => false
      }.deep_merge(options)

      # Merge in legacy fields
      [:label, :input_html].each{|o|
        settings[:field][o] = settings[o] if settings[o].present? }

      new_block = block_given? ? block : ->(fb2) {
        render partial: 'polyblock/simple_fields_for',
               locals: { f: fb2, pb: pb, name: name, options: settings } }
      new_block.call(f)
    end

    protected
    def pb_from_name(name, fb)
      if name.is_a?(Symbol)
        if fb.object.is_a?(Polyblock::Block) then fb.object
        else fb.object.send(name) end
      else
        foi_block = Block.fetch_or_initialize name
        return foi_block if foi_block.present?
        raise 'Invalid name passed to pb_from_name helper!'
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
polyblock-1.2.0 lib/polyblock/simple_form_helper.rb