Module: LexoRanker::RankableMethods::Base::ClassMethods

Defined in:
lib/lexoranker/rankable_methods/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rankable_columnString (readonly)

The column to hold the rank value

Returns:

  • (String)

    the rank value



14
15
16
# File 'lib/lexoranker/rankable_methods/base.rb', line 14

def rankable_column
  @rankable_column
end

#rankable_default_insert_posSymbol (readonly)

Default insert position for newly rank-created elements

Returns:

  • (Symbol)

    The default position



26
27
28
# File 'lib/lexoranker/rankable_methods/base.rb', line 26

def rankable_default_insert_pos
  @rankable_default_insert_pos
end

#rankable_rankerClass (readonly)

The ranker to use

Returns:

  • (Class)

    The ranker being used



22
23
24
# File 'lib/lexoranker/rankable_methods/base.rb', line 22

def rankable_ranker
  @rankable_ranker
end

#rankable_scopeString (readonly)

The scope of the rankings

Returns:

  • (String)

    the column the rankings are scoped to



18
19
20
# File 'lib/lexoranker/rankable_methods/base.rb', line 18

def rankable_scope
  @rankable_scope
end

Instance Method Details

#create_ranked(attributes, position: nil) {|instance| ... } ⇒ Object

Create a new instance of the rankable class with a ranking at ‘position`.

Parameters:

  • attributes (Hash)

    attributes for the newly created instance

  • position (Integer) (defaults to: nil)

    position that the instance should be created at

Yields:

  • (instance)

    The instance before it is saved.

Returns:

  • (Object)

    The record that was created



57
58
# File 'lib/lexoranker/rankable_methods/base.rb', line 57

def create_ranked(attributes, position: nil, &block)
end

#rankable_by(field: :rank, scope_by: nil, ranker: LexoRanker::Ranker.new, default_insert_pos: :bottom) ⇒ void Also known as: rankable

This method returns an undefined value.

Method to set up the rankable column and add the rankable scope and validations to the class including Rankable

Parameters:

  • field (String, Symbol) (defaults to: :rank)

    The field to use for the ranking column.

  • scope_by (String, Symbol) (defaults to: nil)

    The field that is used to scope the rankings

  • ranker (Class) (defaults to: LexoRanker::Ranker.new)

    The class used to determine rankings

  • default_insert_pos (Symbol) (defaults to: :bottom)

    the default position for newly created rankable elements to be placed.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lexoranker/rankable_methods/base.rb', line 35

def rankable_by(field: :rank, scope_by: nil, ranker: LexoRanker::Ranker.new, default_insert_pos: :bottom)
  unless i[top bottom].include?(default_insert_pos)
    raise ArgumentError,
      "#{default_insert_pos} is not a valid default_insert_position. Must be one of [:top, :bottom]"
  end
  @rankable_column = field
  @rankable_scope = scope_by
  @rankable_ranker = ranker
  @rankable_default_insert_pos = default_insert_pos

  set_ranked_scope(field)
  set_ranked_validations(field, @rankable_scope)
end