Module: Iron::Lexorank::Rankable::HasRank

Defined in:
lib/iron/lexorank/rankable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#has_rank_appliedObject (readonly)

Returns the value of attribute has_rank_applied.



6
7
8
# File 'lib/iron/lexorank/rankable.rb', line 6

def has_rank_applied
  @has_rank_applied
end

#rank_columnObject (readonly)

Returns the value of attribute rank_column.



6
7
8
# File 'lib/iron/lexorank/rankable.rb', line 6

def rank_column
  @rank_column
end

#rank_scoped_byObject (readonly)

Returns the value of attribute rank_scoped_by.



6
7
8
# File 'lib/iron/lexorank/rankable.rb', line 6

def rank_scoped_by
  @rank_scoped_by
end

Instance Method Details

#has_rank(column: :rank, scoped_by: nil) ⇒ Object



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

def has_rank(column: :rank, scoped_by: nil)
  self.class_variable_set("@@rank_column", column)
  cattr_reader :rank_column, instance_reader: false

  self.class_variable_set("@@rank_scoped_by", grab_column(scoped_by))
  cattr_reader :rank_scoped_by, instance_reader: false

  @has_rank_applied = true

  scope :ranked, ->(direction: :asc) { where.not("#{column}": nil).order("#{column}": direction) }

  after_save_commit :request_rank_rebalance, if: -> {
    rank.present? && rank.length > Lexorank::LONG_RANK_SIZE
  }

  include InstanceMethods
  extend ClassMethods
end