Sha256: 4a927dbe561d723a92590359f335a21cfa4d0d2bf6ebca93b34b8a8f7d65cee9

Contents?: true

Size: 735 Bytes

Versions: 2

Compression:

Stored size: 735 Bytes

Contents

require "active_support/concern"

module LookupBy
  module Hooks
    module SimpleForm
      extend ActiveSupport::Concern

      included do
        alias_method :input_without_lookup, :input
        alias_method :input, :input_with_lookup
      end

      def input_with_lookup(method, options = {}, &block)
        klass = object.class

        if klass.respond_to?(:lookups) && klass.lookups.include?(method.to_sym)
          target = method.to_s.classify.constantize

          options[:collection] ||= target.pluck(target.lookup.field) if target.lookup.has_cache?
        end

        input_without_lookup(method, options, &block)
      end
    end
  end
end

::SimpleForm::FormBuilder.send :include, LookupBy::Hooks::SimpleForm

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lookup_by-0.12.0 lib/lookup_by/hooks/simple_form.rb
lookup_by-0.11.2 lib/lookup_by/hooks/simple_form.rb