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
- #exists_permalink?(column_name, permalink, options = {}) ⇒ Boolean
- #generate_permalink(column_name, permalink, options = {}) ⇒ Object
Instance Method Details
#exists_permalink?(column_name, permalink, options = {}) ⇒ 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, = {}) sql_clause, values = [], [] sql_clause << "#{column_name} = ?" values << permalink if [:scope] Array([: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 |
#generate_permalink(column_name, permalink, options = {}) ⇒ Object
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, = {}) tester, correction = permalink, 0 while exists_permalink?(column_name, tester, ) correction += 1 tester = [permalink, correction].select(&:present?).join("-") end tester end |