Module: Ransack::Adapters::ActiveRecord::Base

Defined in:
lib/ransack_ui/ransack_overrides/adapters/active_record/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ransack_ui/ransack_overrides/adapters/active_record/base.rb', line 14

def self.extended(base)
  alias :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

Instance Method Details

#has_ransackable_associations(associations) ⇒ Object



26
27
28
# File 'lib/ransack_ui/ransack_overrides/adapters/active_record/base.rb', line 26

def has_ransackable_associations(associations)
  self._ransackable_associations = associations
end

#ransack_can_autocompleteObject



30
31
32
# File 'lib/ransack_ui/ransack_overrides/adapters/active_record/base.rb', line 30

def ransack_can_autocomplete
  self._ransack_can_autocomplete = true
end

#ransackable_associations(auth_object = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/ransack_ui/ransack_overrides/adapters/active_record/base.rb', line 34

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

#ransackable_attributes(auth_object = nil) ⇒ Object

Return array of attributes with [name, type] (Default to :string type for ransackers)



9
10
11
12
# File 'lib/ransack_ui/ransack_overrides/adapters/active_record/base.rb', line 9

def ransackable_attributes(auth_object = nil)
  columns.map{|c| [c.name, c.type] } +
  _ransackers.map {|k,v| [k, v.type || :string] }
end