Brian Cardarella
# generator
rails g client_side_validations:install
# app/assets/javascripts/application.js
//= require rails.validations
# model
class User < ActiveRecord::Base
validates :name, presence: true
end
# form
= form_for @user, validate: true do |f|
= f.text_field :name
# spec it { should validate_presence_of :name } it { should validate_numericality_of :age} # model validates_presence_of :name validates_numericality_of :age
# spec
it { should have_valid(:name).when('Brian') }
it { should_not have_valid(:name).when(nil, '') }
it { should have_valid(:age).when(21) }
it { should_not have_valid(:age).when('twenty-one') }
# model
validates_presence_of :name
validates_numericality_of :age
# spec
it { should have_valid(:name).when('Brian') }
it { should_not have_valid(:name).when(nil, 'Bob') }
# model
validates_presence_of :name
validates_length_of :name, :greater_than => 3
feature 'Emailer' do background do # will clear the message queue clear_emails visit email_trigger_path # Will find an email sent to test@example.com # and set `current_email` open_email('test@example.com') end scenario 'following a link' do current_email.click_link 'your profile'
page.should have_content 'Profile page' end scenario 'testing for content' do current_email.should have_content 'Hello Joe!' end scenario 'view the email body in your browser' do # the `launchy` gem is required current_email.save_and_open end end
create_table :items do |t| t.string :names, :array => true, :default => [] end
i = Item.new(names: ['Brian', 'Dan'])
i.names += ['Doug', 'Amanda']
i.save
i.reload
i.names
=> ['Brian', 'Dan', 'Doug', 'Amanda']
# Find an item with 'Brian' as a name
Item.where.any(names: 'Brian')
=> []
# Overlap
ARRAY[1,2,3] && ARRAY[4,5,6]
-- f
ARRAY[1,2,3] && ARRAY[3,5,6]
-- t
Item.where.overlap(:names => ['Bob', 'Fred'])
=> [, ]
create_table :testing do |t|
t.inet :inet_column
t.cidr :cidr_column
t.macaddr :macaddr_column
t.uuid :uuid_column
t.citext :citext_column
end
add_index :table_name, :column, :where => 'column < 50'
# routes.rb
dismissible_helpers_routes
# app/assets/javascripts/application.js
//= require dismissed_helpers
$(function(){
$('.dismissible').dismissible();
});
# Template
<%= render_dismissible_helper 'sample_page.help_text' %>
# Migration
class AddDismissedHelpersToAccounts < ActiveRecord::Migration
def change
add_column :accounts, :dismissed_helpers, :text
# or if you are using postgres_ext
add_column :accounts, :dismissed_healpers, :string, :array => true, :default => []
end
end
# Model
class Account < ActiveRecord::Base
# Not necessary if you are using postgres_ext
serialize :dismissed_helpers, Array
end
$(function(){
$('.dismissible').dismissible({
success: function(helper){
helper.slideUp();
}
});
});
bug
. A new comment is added with relevant data on the application state.regression
tag is added. A new comment is added with relevant data on the application state.wontfix
the issue is not updated nor is a new issue created. No comments are added.
class UserController < ApplicationController include ContextValidations::Controller def create @user = User.new(user_params) @user.validations = validations(:create) if @user.save # happy path else # sad path end end private
def create_validations validates :password, :presence => true end def base_validations validates :first_name, :last_name, :presence => true validates :password, :confirmation => true validates :email, :uniqueness => true, :format => EmailFormat end end
describe UserController do
context 'create' do
subject { User.new(:password => 'password', :validations => validations_for(:create)) }
it { must have_valid(:name).when('Brian Cardarella') }
it { wont have_valid(:name).when(nil, '') }
end
end
(or even better)