Sha256: 517dbeab9c1cc1f8c4047a5d40911c7a824faa723b4583747f0a0732decafafd

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

require 'ar_finder_form/attr'
module ArFinderForm
  module Attr
    class Base
      attr_reader :column, :name, :options
      attr_reader :nil_available, :array_separator
      def initialize(column, name, options)
        @column, @name = column, name
        @nil_available = options.delete(:nil_available)
        @options = options
        @attr_filter = options[:attr_filter] || method(:column_type_cast)
        @array_separator = options.delete(:array_separator) || /[\s\,]/
      end

      def table; column.table; end
      def nil_available?; !!@nil_available; end

      def column_name(context)
        context.single_table? ? column.name : "#{table.name}.#{column.name}"
      end

      def client_class_eval(script, &block)
        klass = column.table.root_table.client_class
        klass.module_eval(&block) if block
        klass.module_eval(script) if script
      end

      def match?(context)
        nil_available? ? true : !!context.form.send(name)
      end

      def form_value(context)
        result = context.form.send(name)
        @attr_filter ? @attr_filter.call(result) : result
      end

      def form_value_array(context)
        values = context.form.send(name)
        values = values.to_s.split(array_separator) unless values.is_a?(Array)
        @attr_filter ? values.map{|v| @attr_filter.call(v)} : values
      end

      def column_type_cast(value)
        column.model_column.type_cast(value)
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ar_finder_form-0.1.0 lib/ar_finder_form/attr/base.rb