Class: Sluggable::Rails::Slug
- Inherits:
-
String
- Object
- String
- Sluggable::Rails::Slug
- Defined in:
- lib/sluggable/rails/slug.rb
Instance Attribute Summary collapse
-
#definition ⇒ Object
Returns the value of attribute definition.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#record ⇒ Object
Returns the value of attribute record.
Instance Method Summary collapse
- #base ⇒ Object
- #changed? ⇒ Boolean
-
#initialize(definition, record) ⇒ Slug
constructor
A new instance of Slug.
- #make ⇒ Object
- #makeable? ⇒ Boolean
- #needs_an_update? ⇒ Boolean
- #scope ⇒ Object
- #shifted? ⇒ Boolean
- #undefined? ⇒ Boolean
- #update ⇒ Object
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
#definition ⇒ Object
Returns the value of attribute definition.
2 3 4 |
# File 'lib/sluggable/rails/slug.rb', line 2 def definition @definition end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
3 4 5 |
# File 'lib/sluggable/rails/slug.rb', line 3 def offset @offset end |
#record ⇒ Object
Returns the value of attribute record.
2 3 4 |
# File 'lib/sluggable/rails/slug.rb', line 2 def record @record end |
Instance Method Details
#base ⇒ Object
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
41 42 43 |
# File 'lib/sluggable/rails/slug.rb', line 41 def changed? record.send "#{definition.origin}_changed?" end |
#make ⇒ Object
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
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
49 50 51 |
# File 'lib/sluggable/rails/slug.rb', line 49 def needs_an_update? undefined? || changed? end |
#scope ⇒ Object
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
37 38 39 |
# File 'lib/sluggable/rails/slug.rb', line 37 def shifted? !offset.zero? end |
#undefined? ⇒ Boolean
45 46 47 |
# File 'lib/sluggable/rails/slug.rb', line 45 def undefined? record.send(definition.attribute).blank? end |
#update ⇒ Object
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 |