Skip to content

Instantly share code, notes, and snippets.

View ratnikov's full-sized avatar

Dmitry Ratnikov ratnikov

  • wonder.com
  • New York, NY
View GitHub Profile
class Admin::AuditsController < ApplicationController
def create
@audit = Audit.new params[:audit]
if @audit.valid?
# ...
else
render :action => 'new'
end
end
class User < ActiveRecord::Base
EMAIL_FORMAT = /\A[a-z0-9][a-z0-9._-]*\z/i.freeze
validates :username, :presence => true, :uniqueness => true, :length => {:minimum => 4, :maximum => 16 }, :format => { :with => EMAIL_FORMAT }
validates :phone_number, :length => {:minimum => 6, :maximum => 25}, :format => { :with => /\A\S[0-9\+\/\(\)\s\-]*\z/i }, :allow_blank => true
#----------------#
# _form.html.erb
<%= form_for @profile do |f| %>
<% if @profile.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@profile.errors.count, "error") %> prohibited this profile from being saved:</h2>
<ul>
<% @profile.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
#!/usr/bin/env ruby
require 'logger'
require 'rubygems'
require 'ncurses'
class Ncurses::WINDOW
def initialize( height, width, starty, startx )
raise 'Boo!'
w = super( height, width, starty, startx )
w.clear
# Create users from csv file
def create_students_from_csv
@csv_file = CSV::Reader.parse(params[:csv_file])
# Did we get a file?
if @csv_file
# Remove headings
@csv_file.shift
count_saved = @csv_file.inject(0) do |acc, row|
class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
def initialize_with_perl_utils(*args)
returning(initialize_without_perl_utils(*args)) do
execute('SELECT prepare_perl_utils()')
end
end
alias_method_chain :initialize, :perl_utils
end
class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
alias_method :originit, :initialize
def initialize(connection, logger, connection_parameters, config)
ret = originit(connection, logger, connection_parameters, config)
self.execute('SELECT prepare_perl_utils()')
return ret
end
end
1 class CreateSurveyResponseAlternatives < ActiveRecord::Migration
2 def self.up
3 create_table :survey_response_alternatives do |t|
4 t.string :name
5 t.string :value
6 t.integer :position
7 t.references :survey_item
8 end
9
10 #SurveyResponseAlternative.reset_column_information
@ratnikov
ratnikov / gist:289926
Created January 29, 2010 17:31 — forked from rsl/gist:289924
context "#increment" do
setup { @object.create! }
evaluate { @object.increment! :attribute }
should_change("attribute", :from => 0, :to => 1) { @object.attribute }
end
## I seem to have lost my base models - I had obtained them already from pre-existing tables.
Basically, this relates to one table, a model and a subclass, which I thought would be quite short.
class Person < ActiveRecord::Base
has_one :address
has_one :phone
belongs_to :district
def member?
mem # and if you really require true/false: !!mem