This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Shoe < ActiveRecord::Base | |
validates :size, divisibility: { by: 0.5, allow_blank: true } | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
en: | |
errors: | |
messages: | |
not_divisible: 'is not a multiple of %{by}' | |
it: | |
errors: | |
messages: | |
not_divisible: 'non è un multiplo di %{by}' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Book < ActiveRecord::Base | |
has_many :reviews | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BooksController < ApplicationController | |
def top_ten | |
@books = Book.order('reviews_count DESC').limit(10) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class House | |
has_many :photos | |
end |