Skip to content

Instantly share code, notes, and snippets.

@mskyle
mskyle / cash_validation.rb
Created January 29, 2019 18:19
Cash Validation for review
# The following is a Rails validation method on a model object
# Validations are pieces of code that ensure the data passed in to a model matches a defined set of rules
# ex. presence, uniqueness, within a certain range
# The validation should ensure the following constraints are met:
# 1.
# min_cash_pct:
# Allow null: no
# Value range: 0 to 100
require 'date'
class Officiant
def willing_to_officiate_wedding?(groom, bride, date, location)
if busy?(date)
return false
end
if too_far?(location)
return false
end
class MountainsController < ApplicationController
helper_method :sort_column, :sort_direction
before_filter :authorize, only: :edit
def new
@mountain = Mountain.new
end
def edit
@mountain = Mountain.find(params[:id])
def self.filter(filter_hash, user = nil, sort_column = "height", sort_direction = "desc")
query_start = "SELECT * FROM mountains "
query = query_start
if filter_hash.has_key?(:hiked)
hike_query = "INNER JOIN trip_mountains ON mountains.id = trip_mountains.mountain_id " +
"INNER JOIN trips ON trip_mountains.trip_id = trips.id " +
"INNER JOIN trip_participations ON trips.id = trip_participations.trip_id " +
"WHERE trip_participations.user_id = #{user.id} "
if filter_hash[:hiked] == :hiked
@mskyle
mskyle / email_username.rb
Last active December 24, 2015 13:09
validates presence of either email or username?
validate :has_unique_email_or_username, on: :create
def has_unique_email_or_username
if username.nil? && email.nil?
errors.add(:email, "valid email or username required")
elsif not email.match(/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/)
errors.add(:email, "please enter a valid email")
elsif User.where(email: email).count > 0
errors.add(:email, "that email has already been taken")
elsif User.where(username: username).count > 0
require 'spec_helper'
feature "User creates an issue", %q{
As a user
I want to create an issue
So that I can track the progress of the issue
} do
# Acceptance Criteria: