Module: Sequenceable

Defined in:
lib/sequenceable.rb,
lib/sequenceable/core.rb,
lib/sequenceable/version.rb

Defined Under Namespace

Modules: Core Classes: Error

Constant Summary collapse

VERSION =
"0.1.5"

Instance Method Summary collapse

Instance Method Details

#acts_in_sequence(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sequenceable.rb', line 15

def acts_in_sequence(options = {})
  raise ArgumentError, "acts_in_sequence => Hash expected, got #{options.class.name}" if !options.is_a?(Hash)

  class_attribute :sequencing_configuration

  self.sequencing_configuration = {
    scope: options[:scope],
    column_name: "sequence",
    default_order: "ASC"
  }

  if options[:column_name].present?
    sequencing_configuration[:column_name] = options[:column_name]
  end

  if options[:default_order].present?
    sequencing_configuration[:default_order] = options[:default_order].to_s.downcase == "desc" ? "DESC" : "ASC"
  end

  return if sequenceable?

  include Sequenceable::Core

  # Scopes
  default_scope { order(sequencing_configuration[:column_name] => sequencing_configuration[:default_order]) }
  scope :without_sequence_order, -> { reorder("") }

  # Assign sequence before create
  before_create :assign_sequence_before_create
end

#sequenceable?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/sequenceable.rb', line 11

def sequenceable?
  included_modules.include?(Sequenceable::Core)
end