Module: RansackAbbreviator::Adapters::ActiveRecord::Base

Defined in:
lib/ransack_abbreviator/adapters/active_record/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



6
7
8
9
10
# File 'lib/ransack_abbreviator/adapters/active_record/base.rb', line 6

def self.extended(base)
  base.class_eval do
    class_attribute :_ransack_column_abbreviations, :_ransack_assoc_abbreviations
  end
end

Instance Method Details

#ransack_abbreviate_assoc(assoc_name, assoc_abbr) ⇒ Object



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

def ransack_abbreviate_assoc(assoc_name, assoc_abbr)
  ransack_abbreviate_assoc!(assoc_name, assoc_abbr) rescue {}
end

#ransack_abbreviate_assoc!(assoc_name, assoc_abbr) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/ransack_abbreviator/adapters/active_record/base.rb', line 30

def ransack_abbreviate_assoc!(assoc_name, assoc_abbr)
  unless reflect_on_all_associations.map{|a| a.name.to_s}.include?(assoc_name) 
    raise ActiveModel::MissingAttributeError, "missing association: #{assoc_name}" 
  end
  if self.ransackable_assoc_abbreviations.has_value?(assoc_abbr)
    raise "Association #{self.ransackable_assoc_abbreviations.key(assoc_abbr)} already has abbreviaiton #{assoc_abbr} Association abbreviations need to be unique" 
  end
  self._ransack_assoc_abbreviations[assoc_name] = assoc_abbr
end

#ransack_abbreviate_column(column_name, column_abbr) ⇒ Object



12
13
14
# File 'lib/ransack_abbreviator/adapters/active_record/base.rb', line 12

def ransack_abbreviate_column(column_name, column_abbr)
  ransack_abbreviate_column!(column_name, column_abbr) rescue {}
end

#ransack_abbreviate_column!(column_name, column_abbr) ⇒ Object



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

def ransack_abbreviate_column!(column_name, column_abbr)
  unless column_names.include?(column_name) 
    raise ActiveModel::MissingAttributeError, "missing attribute: #{column_name}" 
  end
  if self.ransackable_column_abbreviations.has_value?(column_abbr)
    raise "Column #{self.ransackable_column_abbreviations.key(column_abbr)} already has abbreviaiton #{column_abbr}. Column abbreviations need to be unique" 
  end
  self._ransack_column_abbreviations[column_name] = column_abbr
end

#ransackable_assoc_abbr_for(assoc) ⇒ Object



76
77
78
79
# File 'lib/ransack_abbreviator/adapters/active_record/base.rb', line 76

def ransackable_assoc_abbr_for(assoc)
  assoc = assoc.to_s
  self.ransackable_assoc_abbreviations.has_key?(assoc) ? self.ransackable_assoc_abbreviations[assoc] : assoc
end

#ransackable_assoc_abbreviationsObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/ransack_abbreviator/adapters/active_record/base.rb', line 50

def ransackable_assoc_abbreviations
  self._ransack_assoc_abbreviations ||= begin 
    self._ransack_assoc_abbreviations = {}
    associations = reflect_on_all_associations.map{|a| a.name.to_s}
    RansackAbbreviator.assoc_abbreviations.select{ |key, val| associations.include?(key) }.each do |assoc, abbr|
      raise "Association #{self._ransack_assoc_abbreviations.key(abbr)} already has abbreviaiton #{abbr}" if self._ransack_assoc_abbreviations.has_value?(abbr)
      self._ransack_assoc_abbreviations[assoc] = abbr
    end
  end
end

#ransackable_assoc_name_for(possible_abbr) ⇒ Object



71
72
73
74
# File 'lib/ransack_abbreviator/adapters/active_record/base.rb', line 71

def ransackable_assoc_name_for(possible_abbr)
  possible_abbr = possible_abbr.to_s
  reflect_on_all_associations.map{|a| a.name.to_s}.include?(possible_abbr) ? possible_abbr : self.ransackable_assoc_abbreviations.key(possible_abbr)
end

#ransackable_column_abbr_for(column_name) ⇒ Object



66
67
68
69
# File 'lib/ransack_abbreviator/adapters/active_record/base.rb', line 66

def ransackable_column_abbr_for(column_name)
  column_name = column_name.to_s
  self.ransackable_column_abbreviations.has_key?(column_name) ? self.ransackable_column_abbreviations[column_name] : column_name
end

#ransackable_column_abbreviationsObject



40
41
42
43
44
45
46
47
48
# File 'lib/ransack_abbreviator/adapters/active_record/base.rb', line 40

def ransackable_column_abbreviations
  self._ransack_column_abbreviations ||= begin
    self._ransack_column_abbreviations = {}
    RansackAbbreviator.column_abbreviations.select{ |key, val| column_names.include?(key) }.each do |col, abbr|
      raise "Column #{self._ransack_column_abbreviations.key(abbr)} already has abbreviaiton #{abbr}" if self._ransack_column_abbreviations.has_value?(abbr)
      self._ransack_column_abbreviations[col] = abbr
    end
  end
end

#ransackable_column_name_for(possible_abbr) ⇒ Object



61
62
63
64
# File 'lib/ransack_abbreviator/adapters/active_record/base.rb', line 61

def ransackable_column_name_for(possible_abbr)
  possible_abbr = possible_abbr.to_s
  column_names.include?(possible_abbr) ? possible_abbr : self.ransackable_column_abbreviations.key(possible_abbr)
end