Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class Quote < ApplicationRecord
belongs_to :user
belongs_to :speaker, class_name: "Artist"
belongs_to :topic, class_name: "Artist"
belongs_to :genre
delegate :medium, to: :genre, allow_nil: true
validates :user_id, presence: true
validates :text, presence: true, length: { maximum: 1200 }
validates :source, presence: true, length: { maximum: 420 },
url: { no_local: true, message: 'needs to be cited with
by linking to a real url. Make sure to use http://
or https://.' }
validates :speaker_id, presence: true
validates :topic_id, presence: true
validate :topic_cant_be_a_speaker
validate :text_cannot_have_double_quotes_included
private
def topic_cant_be_a_speaker
errors.add(:topic, 'can\'t be the same as the speaker. We don\'t allow
artists to talk about themselves ;)') if speaker == topic
end
def text_cannot_have_double_quotes_included
if text.scan('"').present?
errors.add(:text, 'for your quote doesn\'t require you to add the
"double quotes." We\'ll add those for you."')
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment