Module: ActiveTools::ActiveRecord::WithPermalink

Extended by:
ActiveSupport::Concern
Included in:
OnLoadActiveRecord
Defined in:
lib/active_tools/active_record/with_permalink.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#exists_permalink?(column_name, permalink, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/active_tools/active_record/with_permalink.rb', line 66

def exists_permalink?(column_name, permalink, options = {})
  sql_clause, values = [], []
  sql_clause << "#{column_name} = ?"
  values << permalink
  if options[:scope]
    Array(options[:scope]).each do |field|
      if value = send(field)
        sql_clause << "#{field} = ?"
        values << value
      end
    end
  end
  if persisted?
    sql_clause  << "id != ?"
    values << self.id
  end
  self.class.exists?([sql_clause.join(" AND "), *values])
end


57
58
59
60
61
62
63
64
# File 'lib/active_tools/active_record/with_permalink.rb', line 57

def generate_permalink(column_name, permalink, options = {})
  tester, correction = permalink, 0
  while exists_permalink?(column_name, tester, options)
    correction += 1
    tester = [permalink, correction].select(&:present?).join("-")
  end
  tester
end