Class: FriendlyId::SequentiallySlugged::SequentialSlugCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/friendly_id/sequentially_slugged.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, slug, slug_column, sequence_separator, base_class) ⇒ SequentialSlugCalculator

Returns a new instance of SequentialSlugCalculator.


20
21
22
23
24
25
26
# File 'lib/friendly_id/sequentially_slugged.rb', line 20

def initialize(scope, slug, slug_column, sequence_separator, base_class)
  @scope = scope
  @slug = slug
  table_name = scope.connection.quote_table_name(base_class.arel_table.name)
  @slug_column = "#{table_name}.#{scope.connection.quote_column_name(slug_column)}"
  @sequence_separator = sequence_separator
end

Instance Attribute Details

#scopeObject

Returns the value of attribute scope


18
19
20
# File 'lib/friendly_id/sequentially_slugged.rb', line 18

def scope
  @scope
end

#sequence_separatorObject

Returns the value of attribute sequence_separator


18
19
20
# File 'lib/friendly_id/sequentially_slugged.rb', line 18

def sequence_separator
  @sequence_separator
end

#slugObject

Returns the value of attribute slug


18
19
20
# File 'lib/friendly_id/sequentially_slugged.rb', line 18

def slug
  @slug
end

#slug_columnObject

Returns the value of attribute slug_column


18
19
20
# File 'lib/friendly_id/sequentially_slugged.rb', line 18

def slug_column
  @slug_column
end

Instance Method Details

#conflict_queryObject (private)


53
54
55
56
57
58
59
# File 'lib/friendly_id/sequentially_slugged.rb', line 53

def conflict_query
  base = "#{slug_column} = ? OR #{slug_column} LIKE ?"
  # Awful hack for SQLite3, which does not pick up '\' as the escape character
  # without this.
  base << " ESCAPE '\\'" if scope.connection.adapter_name =~ /sqlite/i
  base
end

#last_sequence_numberObject (private)


38
39
40
41
42
43
44
45
# File 'lib/friendly_id/sequentially_slugged.rb', line 38

def last_sequence_number
  regexp = /#{slug}#{sequence_separator}(\d+)\z/
  # Reject slug_conflicts that doesn't come from the first_candidate
  # Map all sequence numbers and take the maximum
  slug_conflicts.reject{ |slug_conflict| !regexp.match(slug_conflict) }.map do |slug_conflict|
    regexp.match(slug_conflict)[1].to_i
  end.max
end

#next_sequence_numberObject (private)


34
35
36
# File 'lib/friendly_id/sequentially_slugged.rb', line 34

def next_sequence_number
  last_sequence_number ? last_sequence_number + 1 : 2
end

#next_slugObject


28
29
30
# File 'lib/friendly_id/sequentially_slugged.rb', line 28

def next_slug
  slug + sequence_separator + next_sequence_number.to_s
end

#ordering_queryObject (private)

Return the unnumbered (shortest) slug first, followed by the numbered ones in ascending order.


70
71
72
73
74
# File 'lib/friendly_id/sequentially_slugged.rb', line 70

def ordering_query
  length_command = "LENGTH"
  length_command = "LEN" if scope.connection.adapter_name =~ /sqlserver/i
  "#{length_command}(#{slug_column}) ASC, #{slug_column} ASC"
end

#sequential_slug_matcherObject (private)


61
62
63
64
65
66
# File 'lib/friendly_id/sequentially_slugged.rb', line 61

def sequential_slug_matcher
  # Underscores (matching a single character) and percent signs (matching
  # any number of characters) need to be escaped. While this looks like
  # an excessive number of backslashes, it is correct.
  "#{slug}#{sequence_separator}".gsub(/[_%]/, '\\\\\&') + '%'
end

#slug_conflictsObject (private)


47
48
49
50
51
# File 'lib/friendly_id/sequentially_slugged.rb', line 47

def slug_conflicts
  scope.
    where(conflict_query, slug, sequential_slug_matcher).
    order(Arel.sql(ordering_query)).pluck(Arel.sql(slug_column))
end