Files
nairobi/app/models/concerns/sluggable.rb
Javi Martín 49e55b4dc4 Apply Rails/DynamicFindBy rubocop rule
We were already using `find_by` most of the time.

Since there are false positives related to our `find_by_slug_or_id!` and
`find_by_manger_login` methods, which cannot be replaced with `find_by`,
I'm adding it indicating the "refactor" severity.
2019-10-23 20:05:40 +02:00

20 lines
416 B
Ruby

module Sluggable
extend ActiveSupport::Concern
included do
before_validation :generate_slug, if: :generate_slug?
def self.find_by_slug_or_id(slug_or_id)
find_by(slug: slug_or_id) || find_by(id: slug_or_id)
end
def self.find_by_slug_or_id!(slug_or_id)
find_by(slug: slug_or_id) || find(slug_or_id)
end
end
def generate_slug
self.slug = name.to_s.parameterize
end
end