Sha256: 0c569dfe7346fed9beba07be6e36cdb0ae99236b45a9fd9d72a152a2f005b871

Contents?: true

Size: 1.48 KB

Versions: 3

Compression:

Stored size: 1.48 KB

Contents

require 'ransack/adapters/active_record/base'

module Ransack
  module Adapters
    module ActiveRecord
      module Base
        # Return array of attributes with [name, type]
        # (Default to :string type for ransackers)
        def ransackable_attributes(auth_object = nil)
          columns.map { |c| [c.name, c.type] } +
            _ransackers.map { |k, v| [k, v.type || :string] }
        end

        def self.extended(base)
          alias_method :search, :ransack unless base.method_defined? :search
          base.class_eval do
            class_attribute :_ransackers
            class_attribute :_ransackable_associations
            class_attribute :_ransack_can_autocomplete
            self._ransackers ||= {}
            self._ransackable_associations ||= []
            self._ransack_can_autocomplete = false
          end
        end

        def has_ransackable_associations(associations)
          self._ransackable_associations = associations
        end

        def ransack_can_autocomplete
          self._ransack_can_autocomplete = true
        end

        def ransackable_associations(auth_object = nil)
          all_associations = reflect_on_all_associations.map { |a| a.name.to_s }
          if self._ransackable_associations.any?
            # Return intersection of all associations, and associations defined on the model
            all_associations & self._ransackable_associations
          else
            all_associations
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ransack_ui-2.0.1 lib/ransack_ui/ransack_overrides/adapters/active_record/base.rb
ransack_ui-2.0.0 lib/ransack_ui/ransack_overrides/adapters/active_record/base.rb
ransack_ui-1.4.0 lib/ransack_ui/ransack_overrides/adapters/active_record/base.rb