Skip to content

Instantly share code, notes, and snippets.

@leemcalilly
Created July 31, 2018 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leemcalilly/f1f44617574b9706e415f342913e04b0 to your computer and use it in GitHub Desktop.
Save leemcalilly/f1f44617574b9706e415f342913e04b0 to your computer and use it in GitHub Desktop.
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
accepts_nested_attributes_for :speaker
accepts_nested_attributes_for :topic
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
validate :check_medium
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
def check_medium
if genre.try(:medium) != medium
errors.add(:base, 'You need to select a genre that applies to the medium you\'ve selected')
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment