Module: ActsAsRankedList::ActiveRecord::Service

Defined in:
lib/acts_as_ranked_list/active_record/service.rb

Defined Under Namespace

Modules: InstanceMethods

Class Method Summary collapse

Class Method Details

.acts_as_ranked_list(user_options = {}) ⇒ void

This method returns an undefined value.

Add acts_as_ranked_list to an ::ActiveRecord model to use this gem. Please refer to the README for complete usage and examples.

Examples:

with an ::ActiveRecord model of the name TodoItem. Works when inheriting from ::ApplicationRecord.

class TodoItem << ::ActiveRecord::Base
  acts_as_ranked_list
end

when changing default options

class TodoItem << ::ActiveRecord::Base
  acts_as_ranked_list column: "position", touch_on_update: false, step_increment: 0.5, avoid_collisions: false, new_item_at: :highest
end

when using scope

class TodoItem << ::ActiveRecord::Base
  belongs_to :todo_list
  scope :recently_added, -> { where(created_at: ::Time.now - 12.hours..) }
  acts_as_ranked_list scopes: do # you may use as many scopes together
    column_name_one: "work", # items that have value `work` in column `column_name_one` are scoped together
    column_name_two: :personal, # using a symbol is overloaded, check parameters and example for more info
    column_name_three: 0, # this could be used to sub-rank items within a rank
    column_name_four: true,
    column_name_five: :recently_added, # ranks items within the `recently_added` scope
    column_name_six: ::Proc.new { "column_name = 'value'" }, # useful for complex scopes, you may pass any valid SQL scopes
    column_name_seven: ::Proc.new { where(column_name: value) }, # anonymous scopes
    todo_list(_id): nil # ranks items scoped to their respective todo lists, works with or without `(_id)`
  end
end

Parameters:

  • user_options (Hash) (defaults to: {})

    options

Options Hash (user_options):

  • :column (String)

    The column name to use for ranking

  • :touch_on_update (Boolean)

    Controls updating table's columns updated fields on any write operation

  • :step_increment (Float/Integer)

    The value to use for spreading ranks

  • :avoid_collisions (Boolean)

    Controls avoiding rank collisions

  • :new_item_at (Symbol)

    Controls where to add new items

  • :scopes (Hash)

    Scopes ranked items based on a column, and optional value

    • :any_key [String/Integer/Boolean/Float] Uses passed value in column to scope items
    • :any_key [Symbol] Uses passed value in column to scope items, or a named scope
    • :any_key [Proc] Calls passed block's value in column to scope items, or an anonymous scope
    • :any_key [NilClass] Scopes items to a relationship, by querying non-nil values in column or

Since:

  • 0.2.0



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/acts_as_ranked_list/active_record/service.rb', line 51

def acts_as_ranked_list(user_options = {})
  options = {
    column: "rank",
    touch_on_update: true,
    step_increment: 1024,
    avoid_collisions: true,
    new_item_at: :lowest,
    scopes: {}
  }
  options.update(user_options)

  ::ActsAsRankedList::ActiveRecord::PersistenceCallback.call(self)
  ::ActsAsRankedList::ActiveRecord::RankColumn.call(self, options[:column], options[:touch_on_update], options[:step_increment], options[:avoid_collisions], options[:new_item_at], options[:scopes])

  include ::ActsAsRankedList::ActiveRecord::Service::InstanceMethods
  include ::ActsAsRankedList::ActiveRecord::SkipPersistence
  include ::ActsAsRankedList::ActiveRecord::AvoidCollisions
end