Skip to content

Instantly share code, notes, and snippets.

View aldesantis's full-sized avatar

Alessandro Desantis aldesantis

View GitHub Profile
class Shoe < ActiveRecord::Base
validate :validate_size
private
def validate_size
return if size.blank?
return if size % 0.5 == 0
errors.add :size, 'is not a multiple of 0.5'
class Shoe < ActiveRecord::Base
validates :size, divisibility: { by: 0.5, allow_blank: true }
end
en:
errors:
messages:
not_divisible: 'is not a multiple of %{by}'
it:
errors:
messages:
not_divisible: 'non è un multiplo di %{by}'
<?php
$period = isset($_POST['period']) ? trim($_POST['period']) : '';
echo <<<EOF
<form method="post" action="{$_SERVER['REQUEST_URI']}">
<div>
<label for="period">Periodo:</label>
<input type="text" name="period" id="period" value="{$period}">
</div>
class Book < ActiveRecord::Base
has_many :reviews
end
class BooksController < ApplicationController
def top_ten
@books = Book.order('reviews_count DESC').limit(10)
end
end
RSpec.describe Review do
let(:book) { Book.create! }
subject { Review.new(book: book) }
it "increments the book's reviews_count when saving" do
expect {
subject.save!
}.to change(book, :reviews_count).by(1)
end
class Review < ActiveRecord::Base
belongs_to :book
trigger.after(:insert) do
<<-SQL
UPDATE books SET reviews_count = reviews_count + 1 WHERE id = NEW.book_id;
SQL
end
trigger.after(:update).of(:book_id) do
RSpec.describe Photo
let(:house) { House.create! }
subject { described_class.new(house: house) }
it 'is marked as cover when saved' do
expect {
subject.save!
subject.reload
}.to change(subject, :cover).to(true)
end
class House
has_many :photos
end