Class: Sluggable::Rails::Slug

Inherits:
String
  • Object
show all
Defined in:
lib/sluggable/rails/slug.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition, record) ⇒ Slug

Returns a new instance of Slug.



5
6
7
8
# File 'lib/sluggable/rails/slug.rb', line 5

def initialize(definition, record)
  self.definition = definition
  self.record = record
end

Instance Attribute Details

#definitionObject

Returns the value of attribute definition.



2
3
4
# File 'lib/sluggable/rails/slug.rb', line 2

def definition
  @definition
end

#offsetObject (readonly)

Returns the value of attribute offset.



3
4
5
# File 'lib/sluggable/rails/slug.rb', line 3

def offset
  @offset
end

#recordObject

Returns the value of attribute record.



2
3
4
# File 'lib/sluggable/rails/slug.rb', line 2

def record
  @record
end

Instance Method Details

#baseObject



29
30
31
# File 'lib/sluggable/rails/slug.rb', line 29

def base
  String.try_convert(record.send(definition.origin)).to_s.parameterize separator: definition.separator
end

#changed?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/sluggable/rails/slug.rb', line 41

def changed?
  record.send "#{definition.origin}_changed?"
end

#makeObject



19
20
21
22
23
24
25
26
27
# File 'lib/sluggable/rails/slug.rb', line 19

def make
  if makeable?
    @offset = 0
    generate until present? && unique?
    true
  else
    false
  end
end

#makeable?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/sluggable/rails/slug.rb', line 33

def makeable?
  definition.is_a?(Sluggable::Rails::Definition) && record.is_a?(ActiveRecord::Base)
end

#needs_an_update?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/sluggable/rails/slug.rb', line 49

def needs_an_update?
  undefined? || changed?
end

#scopeObject



53
54
55
# File 'lib/sluggable/rails/slug.rb', line 53

def scope
  definition.scope.map{ |attribute| [attribute, record.send(attribute)] }.to_h
end

#shifted?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/sluggable/rails/slug.rb', line 37

def shifted?
  !offset.zero?
end

#undefined?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/sluggable/rails/slug.rb', line 45

def undefined?
  record.send(definition.attribute).blank?
end

#updateObject



10
11
12
13
14
15
16
17
# File 'lib/sluggable/rails/slug.rb', line 10

def update
  if make
    record.send "#{definition.attribute}=", self.to_s
    true
  else
    false
  end
end