Sha256: 4c80656702e59d3ef673b71d74ac2c2a02f51bc1d52f6c3703c3f8f8ef08d468

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

module Fields
  class TextController < Volt::ModelController
    def index
      # Default to text fields
      if attrs.respond_to?(:type)
        @type = attrs.type
      else
        @type = 'text'
      end

      # Get the name of the field by looking at the method scope
      @field_name = attrs.value_last_method.gsub(/^[_]/, '')
    end

    def index_ready
      set_label
    end

    # Find the parent reactive value that produced the value
    # (usually just model._field)
    def model
      attrs.value_parent
    end

    def icon?
      return attrs.icon.present?
    end
    
    def label
      return attrs.label || @field_name.titleize
    end

    def label_id
      return label.gsub(/\s+/, "").downcase
    end

    def set_label
      input_item = Element[".input-field[item=#{label_id}]"]
      input_item_id = input_item.find('input').attr('id')
      input_item.find('label').attr('for', input_item_id)
    end

    # Find the errors for this field
    def errors
      model.marked_errors[@field_name]
    end

    # When a field goes out of focus, then we want to start checking a field
    def blur
      attrs.value_parent.mark_field!(@field_name)
    end

    def marked
      model.marked_fields[@field_name]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
volt-materialize-fields-0.0.2 app/fields/controllers/text_controller.rb
volt-materialize-fields-0.0.1 app/fields/controllers/text_controller.rb