Sha256: d85b1c1b89d9aaac8681390b0040d12df96c3965fb2a91ac73dca10ccae52467

Contents?: true

Size: 1.66 KB

Versions: 6

Compression:

Stored size: 1.66 KB

Contents

module Basepack
  module Forms
    module Groups
      class Base
        include Basepack::Delegation

        delegate_attrs :name, :label, :help

        attr_reader :form, :delegate
        attr_accessor :field_names

        def initialize(form, delegate_or_attributes = nil)
          @form = form
          @field_names = []
          update_attributes(delegate_or_attributes)
        end

        def update_attributes(delegate_or_attributes)
          if delegate_or_attributes.is_a? Hash
            delegate_or_attributes.each do |a, v|
              send("#{a}=", v)
            end
          else
            @delegate = delegate_or_attributes
          end
        end

        def field(name, delegate_or_attributes = nil, &block)
          field = @form.field(name, delegate_or_attributes, &block)
          @field_names << name if field and !@field_names.include?(name)
          field
        end

        def content_for_field(field_name, &block)
          field(field_name) do |f|
            f.content(&block)
          end
        end

        def fields
          @field_names.map {|f| @form.field(f)}.compact
        end

        def visible_fields
          @field_names.map {|f| @form.visible_field(f)}.compact
        end

        def remove
          @form.groups.delete self
        end

        def values
          result = {}
          visible_fields.each {|f| result[f.name] = f.value}
          result
        end

        def values_without_blank
          result = {}
          visible_fields.each do |f|
            val = f.value.presence
            result[f.name] = val unless val.nil?
          end
          result
        end

      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
basepack-1.0.1 lib/basepack/forms/groups/base.rb
basepack-1.0.0 lib/basepack/forms/groups/base.rb
basepack-1.0.0.pre.0 lib/basepack/forms/groups/base.rb
basepack-0.2.0 lib/basepack/forms/groups/base.rb
basepack-0.1.0 lib/basepack/forms/groups/base.rb
basepack-0.0.2 lib/basepack/forms/groups/base.rb