class ActivePermalink::Generator
Public Class Methods
generate(record, value, locale = nil)
click to toggle source
# File lib/active_permalink/generator.rb, line 6 def generate(record, value, locale = nil) return if value.nil? && !record.slug_should_generate? options = record.permalink_options.merge(locale: locale) generator = Generator.new(record, options) generator.generate(value) record.permalinks = generator.permalinks end
new(record, options = {})
click to toggle source
# File lib/active_permalink/generator.rb, line 17 def initialize(record, options = {}) options[:locale] = options.fetch(:locale, I18n.locale).to_s @record = record @options = options @field = options[:field] @scope = options.fetch(:scope, :global) end
Public Instance Methods
generate(new_value)
click to toggle source
# File lib/active_permalink/generator.rb, line 26 def generate(new_value) @new_value = new_value return unless changed? deactivate_active_permalink assign_active_permalink end
permalinks()
click to toggle source
# File lib/active_permalink/generator.rb, line 34 def permalinks @permalinks ||= @record.permalinks end
Private Instance Methods
active_permalink()
click to toggle source
# File lib/active_permalink/generator.rb, line 74 def active_permalink available_permalinks.find { |item| item.active == true } end
assign_active_permalink()
click to toggle source
# File lib/active_permalink/generator.rb, line 118 def assign_active_permalink if old_permalink old_permalink.write_attribute(:active, true) else build_permalink.write_attribute(:slug, scope_unique_slug) end end
available_permalinks()
click to toggle source
# File lib/active_permalink/generator.rb, line 66 def available_permalinks if localized? permalinks.select { |item| item.send(locale_column) == locale } else permalinks end end
build_permalink()
click to toggle source
# File lib/active_permalink/generator.rb, line 78 def build_permalink params = localize(scope: @scope, active: true) active_permalink || permalinks.build(params) end
changed?()
click to toggle source
# File lib/active_permalink/generator.rb, line 44 def changed? @new_value.blank? || @new_value != active_permalink.try(:slug) end
deactivate_active_permalink()
click to toggle source
# File lib/active_permalink/generator.rb, line 112 def deactivate_active_permalink available_permalinks.each do |item| item.active = false unless item.new_record? end end
exists?(slug)
click to toggle source
# File lib/active_permalink/generator.rb, line 56 def exists?(slug) params = localize(scope: @scope, slug: slug) Permalink.where(params).count.zero? end
locale()
click to toggle source
# File lib/active_permalink/generator.rb, line 40 def locale @options[:locale] end
locale_column()
click to toggle source
# File lib/active_permalink/generator.rb, line 52 def locale_column @options[:locale_column] end
localize(**params)
click to toggle source
# File lib/active_permalink/generator.rb, line 61 def localize(**params) params[locale_column] = locale if localized? params end
localized?()
click to toggle source
# File lib/active_permalink/generator.rb, line 48 def localized? @options[:localized].present? end
old_permalink()
click to toggle source
# File lib/active_permalink/generator.rb, line 83 def old_permalink params = localize(slug: slug_from_column) available_permalinks.find do |item| params.all? { |key, value| item.send(key) == value } end end
scope_unique_slug()
click to toggle source
# File lib/active_permalink/generator.rb, line 101 def scope_unique_slug unique = slug_from_column index = 1 while not exists?(unique) unique = "#{slug_from_column}-#{(index += 1)}" end unique end
slug_from_column()
click to toggle source
# File lib/active_permalink/generator.rb, line 91 def slug_from_column @slug_from_column ||= begin value = @new_value.presence || @record.send(@field) return if value.blank? value = AnyAscii.transliterate(value) value.parameterize end end