Sha256: 4df83781748b89ff3f178430724d19abaee221cd5a5c3d394acb81c03c9eb65b

Contents?: true

Size: 1.91 KB

Versions: 12

Compression:

Stored size: 1.91 KB

Contents

module AdminFormHelper
  
  class FormBuilderDecorator
    def initialize(decorated)
       @target = decorated
    end
    
    def errors(*args)
      @target.template.form_errors(@target, *args)
    end
    
    def method_missing(method, *args, &block)
      @target.send(method, *args, &block)
    end
    
    def actions(*args, &proc)
      @target.template.form_actions(&proc)      
    end
    
    def default_action
      action(@target.object.persisted? ? :update : :create, :primary => true)
    end
    
    def form_errors(options = {:exclude => [:slug]})
      f = this
      unless f.object.errors.empty?
        content_tag :div, :class => "alert alert-block" do
          link_to "×", :class => "close", :data => {:dismiss => "alert"}
          content_tag(:h4, t('fullstack.admin.form.correct_these_errors_and_retry', :default => "Correct these errors and retry"), :class => "alert-heading")
          f.semantic_errors *(f.object.errors.keys - (options[:exclude] || []))
        end
      end
    end
    
    def action(method, options = {})
      default_label = I18n.t("fullstack.admin_form.labels.#{method}", :default => "#{method}".humanize)
      options[:type] ||= !!options[:primary] ? :primary : nil
      @target.template.button((options.delete(:label) || default_label), options)
    end

  end
  
  def after_current_form(key = :default, options = {:replace => false})
    reset_after_current_form_hash! if @after_current_form.nil?
    if options[:replace]
      @after_current_form[key] = ""
    end
    
    @after_current_form[key]
  end
  
  def reset_after_current_form_hash!
    @after_current_form = ActiveSupport::OrderedHash.new
    @after_current_form[:default] = ""
  end
  
  def admin_form_for(record_or_name_or_array, *args)
     semantic_form_for(record_or_name_or_array, *args) do |f|
       yield(FormBuilderDecorator.new(f)) << @after_current_form.map { |k, v| v }.join().html_safe
     end
  end
  
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
fullstack-admin-0.1.17 app/helpers/admin_form_helper.rb
fullstack-admin-0.1.16 app/helpers/admin_form_helper.rb
fullstack-admin-0.1.15 app/helpers/admin_form_helper.rb
fullstack-admin-0.1.14 app/helpers/admin_form_helper.rb
fullstack-admin-0.1.12 app/helpers/admin_form_helper.rb
fullstack-admin-0.1.11 app/helpers/admin_form_helper.rb
fullstack-admin-0.1.10 app/helpers/admin_form_helper.rb
fullstack-admin-0.1.8 app/helpers/admin_form_helper.rb
fullstack-admin-0.1.7 app/helpers/admin_form_helper.rb
fullstack-admin-0.1.5 app/helpers/admin_form_helper.rb
fullstack-admin-0.1.3 app/helpers/admin_form_helper.rb
fullstack-admin-0.1.1 app/helpers/admin_form_helper.rb