Class: SimpleSlug::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_slug/adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Adapter

Returns a new instance of Adapter.



6
7
8
9
10
# File 'lib/simple_slug/adapter.rb', line 6

def initialize(model)
  @model = model
  @options = model.simple_slug_options
  @locales = Array(@options[:locales] || [nil])
end

Instance Attribute Details

#current_localeObject

Returns the value of attribute current_locale.



4
5
6
# File 'lib/simple_slug/adapter.rb', line 4

def current_locale
  @current_locale
end

#localesObject (readonly)

Returns the value of attribute locales.



3
4
5
# File 'lib/simple_slug/adapter.rb', line 3

def locales
  @locales
end

#modelObject (readonly)

Returns the value of attribute model.



3
4
5
# File 'lib/simple_slug/adapter.rb', line 3

def model
  @model
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/simple_slug/adapter.rb', line 3

def options
  @options
end

Instance Method Details

#add_suffix(slug_value) ⇒ Object



80
81
82
# File 'lib/simple_slug/adapter.rb', line 80

def add_suffix(slug_value)
  "#{slug_value}--#{rand(99999)}"
end

#column_name(locale = I18n.locale) ⇒ Object



28
29
30
# File 'lib/simple_slug/adapter.rb', line 28

def column_name(locale=I18n.locale)
  [options[:slug_column], (locale if valid_locale?(locale))].compact.join('_')
end

#column_namesObject



24
25
26
# File 'lib/simple_slug/adapter.rb', line 24

def column_names
  locales.map{|l| column_name(l) }
end

#each_localeObject



44
45
46
47
48
# File 'lib/simple_slug/adapter.rb', line 44

def each_locale
  locales.each do |l|
    with_locale(l || I18n.default_locale) { yield }
  end
end

#finder_methodObject



12
13
14
# File 'lib/simple_slug/adapter.rb', line 12

def finder_method
  options[:history] ? :find_by : :find_by!
end

#generate(record, force: false) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/simple_slug/adapter.rb', line 62

def generate(record, force: false)
  each_locale do
    next unless force || record.should_generate_new_slug?
    simple_slug = normalize(slug_base(record))
    simple_slug = "__#{record.id || rand(9999)}" if simple_slug.blank? && options[:fallback_on_blank]
    return if simple_slug == get(record).to_s.sub(SimpleSlug::RESOLVE_SUFFIX_REGEXP, '')
    set(record, resolve(record, simple_slug))
  end
end

#get(record) ⇒ Object



32
33
34
# File 'lib/simple_slug/adapter.rb', line 32

def get(record)
  record.send(column_name)
end

#get_prev(record) ⇒ Object



36
37
38
# File 'lib/simple_slug/adapter.rb', line 36

def get_prev(record)
  record.send("#{column_name}_was")
end

#history_slug_exists?(record, slug_value) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
109
110
111
# File 'lib/simple_slug/adapter.rb', line 106

def history_slug_exists?(record, slug_value)
  return false unless options[:history]
  base_scope = SimpleSlug::HistorySlug.where(sluggable_type: record.class.name, slug: slug_value)
  base_scope = base_scope.where('sluggable_id != ?', record.id) if record.persisted?
  base_scope.exists?
end

#model_slug_exists?(record, slug_value) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
103
104
# File 'lib/simple_slug/adapter.rb', line 100

def model_slug_exists?(record, slug_value)
  base_scope = record.class.unscoped.where(column_name => slug_value)
  base_scope = base_scope.where('id != ?', record.id) if record.persisted?
  base_scope.exists?
end

#normalize(base) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/simple_slug/adapter.rb', line 72

def normalize(base)
  parameterize_args = ActiveSupport::VERSION::MAJOR > 4 ? {separator: '-'} : '-'
  normalized = I18n.transliterate(base).parameterize(**parameterize_args).downcase
  normalized = "_#{normalized}" if normalized =~ SimpleSlug::STARTS_WITH_NUMBER_REGEXP
  normalized = normalized[0..options[:max_length].pred] if options[:max_length]
  normalized
end

#reset(record) ⇒ Object



50
51
52
# File 'lib/simple_slug/adapter.rb', line 50

def reset(record)
  each_locale{ set record, get_prev(record) }
end

#resolve(record, slug_value) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/simple_slug/adapter.rb', line 88

def resolve(record, slug_value)
  return slug_value unless slug_exists?(record, slug_value)
  loop do
    slug_with_suffix = add_suffix(slug_value)
    break slug_with_suffix unless slug_exists?(record, slug_with_suffix)
  end
end

#save_history(record) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/simple_slug/adapter.rb', line 54

def save_history(record)
  each_locale do
    slug_was = record.saved_change_to_attribute(column_name).try!(:first)
    next if slug_was.blank?
    ::SimpleSlug::HistorySlug.where(sluggable_type: record.class.name, slug: slug_was, locale: current_locale).first_or_initialize.update(sluggable_id: record.id)
  end
end

#set(record, value) ⇒ Object



40
41
42
# File 'lib/simple_slug/adapter.rb', line 40

def set(record, value)
  record.send("#{column_name}=", value)
end

#slug_base(record) ⇒ Object



84
85
86
# File 'lib/simple_slug/adapter.rb', line 84

def slug_base(record)
  options[:slug_method].map{|m| record.send(m).to_s }.reject(&:blank?).join(' ')
end

#slug_exists?(record, slug_value) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/simple_slug/adapter.rb', line 96

def slug_exists?(record, slug_value)
  model_slug_exists?(record, slug_value) || history_slug_exists?(record, slug_value)
end

#valid_locale?(locale) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/simple_slug/adapter.rb', line 16

def valid_locale?(locale)
  locales.include?(locale)
end

#with_locale(locale) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/simple_slug/adapter.rb', line 113

def with_locale(locale)
  if defined? Globalize
    Globalize.with_locale(locale) do
      I18n.with_locale(locale) { yield }
    end
  else
    I18n.with_locale(locale) { yield }
  end
end