Sha256: 8378a0a6fb880a8c58ada48f24f60389e3e52854e89dba3468b0f89d9e6a119f

Contents?: true

Size: 1.8 KB

Versions: 3

Compression:

Stored size: 1.8 KB

Contents

module ExpressTemplates
  module Components
    module Forms
      class FormComponent < Base
        include Capabilities::Configurable
        include Capabilities::Adoptable

        def compile(*args)
          raise "#{self.class} requires a parent ExpressForm" if parent.nil? or parent_form.nil?
          super(*args)
        end

        # Lookup the resource_name from the parent ExpressForm.
        def resource_name
          parent_form.resource_name
        end

        def resource_var
          resource_name.to_sym
        end

        def resource_class
          parent_form.resource_class
        end

        # Return the name attribute for the lable
        def label_name
          "#{resource_name.singularize}_#{field_name}"
        end

        # Return the text content for the label
        def label_text
          @config[:label] || field_name.titleize
        end

        # Return the field_name as a string.  This taken from the first argument
        # to the component macro in the template or fragment.
        def field_name
          (@config[:id] || (@args.first.is_a?(String) && @args.first)).to_s
        end

        # Return the field name attribute.  Currently handles only simple attributes
        # on the resource.  Does not handle attributes for associated resources.
        def field_name_attribute
          "#{resource_name.singularize}[#{field_name}]"
        end

        def field_wrapper_class
          @config[:wrapper_class] || 'field-wrapper'
        end

        # Search the parent graph until we find an ExpressForm.  Returns nil if none found.
        def parent_form
          @my_form ||= parent
          until @my_form.nil? || @my_form.kind_of?(ExpressForm)
            @my_form = @my_form.parent
          end
          return @my_form
        end

      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
express_admin-1.2.1 vendor/gems/express_templates/lib/express_templates/components/forms/form_component.rb
express_admin-1.2.0 vendor/gems/express_templates/lib/express_templates/components/forms/form_component.rb
express_templates-0.5.0 lib/express_templates/components/forms/form_component.rb