Sha256: 4eeb90b6c2d1c4151afcc672dab2e7821e8032a5fe951b66b282cef98c9d488a

Contents?: true

Size: 913 Bytes

Versions: 1

Compression:

Stored size: 913 Bytes

Contents

module SimpleFormPolymorphicAssociations
  module Model
    # Example:
    #
    #     # app/models/person.rb
    #     class Person < ActiveRecord::Base
    #       include SimpleFormPolymorphicAssociations::AutocompleteConcern
    #       autocomplete scope: ->(matcher) { where("people.firstname LIKE :term", term: "%#{matcher.downcase}%") }, id_method: :id, text_method: :human
    #     end
    #
    module AutocompleteConcern
      extend ActiveSupport::Concern

      class_methods do
        def autocomplete(options)
          self.send(:scope, :autocomplete, options[:scope])
          @autocomplete_options = options
        end

        def autocomplete_options
          @autocomplete_options
        end
      end

      def as_autocomplete_json
        { id: send(self.class.autocomplete_options[:id_method]), text: send(self.class.autocomplete_options[:text_method]) }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simple_form-polymorphic_associations-0.0.1 app/concerns/simple_form_polymorphic_associations/model/autocomplete_concern.rb