Skip to content

Instantly share code, notes, and snippets.

@PlanetRoast
Created November 8, 2017 10:37
Show Gist options
  • Save PlanetRoast/3f29150ac54cb842b96a297e8d2f4d13 to your computer and use it in GitHub Desktop.
Save PlanetRoast/3f29150ac54cb842b96a297e8d2f4d13 to your computer and use it in GitHub Desktop.
class CompetitionEntry < ActiveRecord::Base
belongs_to :competition
belongs_to :user
has_many :team_members
has_many :competition_entry_images
enum status: %i(semi_finals finals)
scope :all_finals, ->{ where(status: statuses.values_at(*%i(semi_finals finals))) }
mount_uploader :final_image, CompetitionEntryImageUploader
accepts_nested_attributes_for :team_members, :allow_destroy => true, reject_if: ->(params){
%i(name email).none?{|k| params[k].present? }
}
validates :first_name, :surname, presence: true, :unless => :new_record?
validates :town_or_city, :telephone, :postcode, :region, presence: true, on: :team, :unless => :new_record?
#validate :ensure_date_of_birth, on: :team, unless: :new_record?
validate :ensure_correct_team_members, on: :team
validate :ensure_correct_images, on: :images
def self.regions
all.map{|e| e.region.presence }.compact.uniq
end
def full_name
[first_name, surname].map(&:presence).compact.join(' ')
end
def all_valid?
valid? && valid?(:team) && valid?(:images)
end
def team_status
valid? && valid?(:team) ? 'complete' : 'incomplete'
end
def images_status
valid?(:images) ? 'complete' : 'incomplete'
end
def submit_status
if is_submitted
'submitted'
else
all_valid? ? 'complete' : 'incomplete'
end
end
def before_image
@before_image ||= competition_entry_images.where(category: 'before').first
end
def after_image
@after_image ||= competition_entry_images.where(category: 'after').first
end
def colourist_member
@colourist ||= team_members.where(category: 'colourist').first
end
def stylist_member
@stylist ||= team_members.where(category: 'stylist').first
end
def star_member
@star ||= team_members.where(category: 'star').first
end
def is_submitted
submitted_at.nil? ? false : true
end
private
def ensure_date_of_birth
return unless competition.star?
unless date_of_birth.present?
errors.add(:date_of_birth, :blank)
return
end
errors.add(:date_of_birth, 'must be between 16 and 25') unless (date_of_birth) < Date.new(1991,02,27) == false && (date_of_birth+16.years) <= Date.new(2017,02,28) == true
end
def ensure_correct_images
errors.add(:base, 'You must upload an before image') unless before_image
if competition.star? || competition.mizani?
errors.add(:base, 'You must upload an after image') unless after_image
end
end
def ensure_correct_team_members
if competition.star?
errors.add(:base, 'You must add a star team member') unless star_member && star_member.valid?
else
errors.add(:base, 'You must add a colourist team member') unless colourist_member && colourist_member.valid?
errors.add(:base, 'You must add a stylist team member') unless stylist_member && stylist_member.valid?
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment