Module: Lexorank::Rankable::ClassMethods

Defined in:
lib/lexorank/rankable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ranking_columnObject (readonly)

Returns the value of attribute ranking_column.



8
9
10
# File 'lib/lexorank/rankable.rb', line 8

def ranking_column
  @ranking_column
end

#ranking_group_byObject (readonly)

Returns the value of attribute ranking_group_by.



8
9
10
# File 'lib/lexorank/rankable.rb', line 8

def ranking_group_by
  @ranking_group_by
end

Instance Method Details

#rank!(field: :rank, group_by: nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lexorank/rankable.rb', line 10

def rank!(field: :rank, group_by: nil)
  @ranking_column = check_column(field)
  if group_by
    @ranking_group_by = check_column(group_by)
    unless @ranking_group_by
      warn "The supplied grouping by \"#{group_by}\" is neither a column nor an association of the model!"
    end
  end

  if @ranking_column
    self.scope :ranked, ->(direction: :asc) { where.not("#{field}": nil).order("#{field}": direction) }
    self.include Lexorank
    self.include InstanceMethods
  else
    warn "The supplied ranking column \"#{field}\" is not a column of the model!"
  end
end