code
stringlengths
1
1.73M
language
stringclasses
1 value
#!/usr/bin/env ruby require File.dirname(__FILE__) + '/../config/boot' require 'commands/destroy'
Ruby
#!/usr/bin/env ruby require File.dirname(__FILE__) + '/../config/boot' $LOAD_PATH.unshift "#{RAILTIES_PATH}/builtin/rails_info" require 'commands/about'
Ruby
#!/usr/bin/env ruby if ARGV.any? {|arg| %w[--drb -X --generate-options -G --help -h --version -v].include?(arg)} require 'rubygems' unless ENV['NO_RUBYGEMS'] else gem 'test-unit', '1.2.3' if RUBY_VERSION.to_f >= 1.9 ENV["RAILS_ENV"] ||= 'test' require File.expand_path(File.dirname(__FILE__) + "/../config/enviro...
Ruby
#!/usr/bin/env ruby gem 'test-unit', '1.2.3' if RUBY_VERSION.to_f >= 1.9 puts "Loading Rails environment" ENV["RAILS_ENV"] ||= 'test' require File.expand_path(File.dirname(__FILE__) + "/../config/environment") unless defined?(RAILS_ROOT) require 'optparse' require 'spec/rails/spec_server'
Ruby
#!/usr/bin/env ruby require File.dirname(__FILE__) + '/../config/boot' require 'commands/dbconsole'
Ruby
#!/usr/bin/env ruby require File.dirname(__FILE__) + '/../config/boot' require 'commands/runner'
Ruby
#!/usr/bin/env ruby vendored_cucumber_binary = Dir[File.join(File.dirname(__FILE__), '..', 'vendor', '{gems,plugins}', 'cucumber*', ...
Ruby
#!/usr/bin/env ruby require File.dirname(__FILE__) + '/../config/boot' require 'commands/server'
Ruby
#!/usr/bin/env ruby require File.dirname(__FILE__) + '/../config/boot' require 'commands/generate'
Ruby
#!/usr/bin/env ruby require File.dirname(__FILE__) + '/../config/boot' require 'commands/console'
Ruby
#!/usr/bin/env ruby gem 'test-unit', '1.2.3' if RUBY_VERSION.to_f >= 1.9 ENV['RSPEC'] = 'true' # allows autotest to discover rspec ENV['AUTOTEST'] = 'true' # allows autotest to run w/ color on linux system((RUBY_PLATFORM =~ /mswin|mingw/ ? 'autotest.bat' : 'autotest'), *ARGV) || $stderr.puts("Unable to find auto...
Ruby
#!/usr/bin/env ruby gem 'test-unit', '1.2.3' if RUBY_VERSION.to_f >= 1.9 puts "Loading Rails environment" ENV["RAILS_ENV"] ||= 'test' require File.expand_path(File.dirname(__FILE__) + "/../config/environment") unless defined?(RAILS_ROOT) require 'optparse' require 'spec/rails/spec_server'
Ruby
#!/usr/bin/env ruby require File.dirname(__FILE__) + '/../config/boot' require 'commands/destroy'
Ruby
#!/usr/bin/env ruby require File.dirname(__FILE__) + '/../config/boot' require 'commands/generate'
Ruby
#!/usr/bin/env ruby require File.dirname(__FILE__) + '/../config/boot' require 'commands/server'
Ruby
#!/usr/bin/env ruby gem 'test-unit', '1.2.3' if RUBY_VERSION.to_f >= 1.9 ENV['RSPEC'] = 'true' # allows autotest to discover rspec ENV['AUTOTEST'] = 'true' # allows autotest to run w/ color on linux system((RUBY_PLATFORM =~ /mswin|mingw/ ? 'autotest.bat' : 'autotest'), *ARGV) || $stderr.puts("Unable to find auto...
Ruby
#!/usr/bin/env ruby require File.dirname(__FILE__) + '/../config/boot' $LOAD_PATH.unshift "#{RAILTIES_PATH}/builtin/rails_info" require 'commands/about'
Ruby
#!/usr/bin/env ruby require File.dirname(__FILE__) + '/../config/boot' require 'commands/plugin'
Ruby
#!/usr/bin/env ruby vendored_cucumber_binary = Dir[File.join(File.dirname(__FILE__), '..', 'vendor', '{gems,plugins}', 'cucumber*', ...
Ruby
#!/usr/bin/env ruby if ARGV.any? {|arg| %w[--drb -X --generate-options -G --help -h --version -v].include?(arg)} require 'rubygems' unless ENV['NO_RUBYGEMS'] else gem 'test-unit', '1.2.3' if RUBY_VERSION.to_f >= 1.9 ENV["RAILS_ENV"] ||= 'test' require File.expand_path(File.dirname(__FILE__) + "/../config/enviro...
Ruby
class CreateUsers < ActiveRecord::Migration def self.up create_table "users", :force => true do |t| t.column :login, :string, :limit => 40 t.column :name, :string, :limit => 100, :default => '', :null => true t.column :email, :string, ...
Ruby
class CreateMovies < ActiveRecord::Migration def self.up create_table :movies do |t| t.string :title t.text :description t.string :rating t.datetime :released_on t.timestamps end end def self.down drop_table :movies end end
Ruby
class CreateReviews < ActiveRecord::Migration def self.up create_table :reviews do |t| t.integer :score t.references :user t.references :movie t.timestamps end end def self.down drop_table :reviews end end
Ruby
class AddBirthdates < ActiveRecord::Migration def self.up add_column :users, :birthdate, :datetime end def self.down remove_column :users, :birthdate end end
Ruby
# This file is copied to ~/spec when you run 'ruby script/generate rspec' # from the project root directory. ENV["RAILS_ENV"] ||= 'test' require File.dirname(__FILE__) + "/../config/environment" unless defined?(RAILS_ROOT) require 'spec/autorun' require 'spec/rails' # Requires supporting files with custom matchers and...
Ruby
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe Review do before(:each) do @valid_attributes = { :user_id => 1, :movie_id => 1, :score => 1 } end it "should create a new instance given valid attributes" it "should not be valid with a score of 6" ...
Ruby
module AuthenticatedSystem protected # Returns true or false if the user is logged in. # Preloads @current_user with the user model if they're logged in. def logged_in? !!current_user end # Accesses the current user from the session. # Future calls avoid the database because nil is not ...
Ruby
gem 'test-unit', '1.2.3' if RUBY_VERSION.to_f >= 1.9 rspec_gem_dir = nil Dir["#{RAILS_ROOT}/vendor/gems/*"].each do |subdir| rspec_gem_dir = subdir if subdir.gsub("#{RAILS_ROOT}/vendor/gems/","") =~ /^(\w+-)?rspec-(\d+)/ && File.exist?("#{subdir}/lib/spec/rake/spectask.rb") end rspec_plugin_dir = File.expand_path(Fil...
Ruby
module AuthenticatedTestHelper # Sets the current user in the session from the user fixtures. def login_as(user) @request.session[:user_id] = user ? (user.is_a?(User) ? user.id : users(user).id) : nil end def authorize_as(user) @request.env["HTTP_AUTHORIZATION"] = user ? ActionController::HttpAuthentic...
Ruby
ENV["RAILS_ENV"] = "test" require File.expand_path(File.dirname(__FILE__) + "/../config/environment") require 'test_help' class ActiveSupport::TestCase # Transactional fixtures accelerate your tests by wrapping each test method # in a transaction that's rolled back on completion. This ensures that the # test da...
Ruby
module Enumerable def group_by ( function ) hash = {} for n in (0..self.size) if not hash[function(self[n])] == 0 list = [] list.append(self[n]) hash[function(self[n])] = list else list = hash[function(self[n])] list.append(self[n]) hash[function(self[n])] = list end end prin...
Ruby
class Object def blank? if self.nil? return true end if self.empty? return true else return false end return false end end
Ruby
class Fixnum def squared return self * self end end print 9.squared
Ruby
class Object def blank? if self.nil? return true end if self.empty? return true end return false end end x = "" print x.blank? print "\n" x = " " print x.blank? x = nil print "\n" print x.blank?
Ruby
require 'time' class Movie include Comparable attr_reader :name attr_reader :showtime def initialize(name,showtime) @name = name if showtime.is_a?(String) @showtime = Time.parse(showtime) else @showtime = showtime end end def name_and_showtime print self.name ...
Ruby
# Be sure to restart your server when you modify this file. # Add new inflection rules using the following format # (all these examples are active by default): # ActiveSupport::Inflector.inflections do |inflect| # inflect.plural /^(ox)$/i, '\1en' # inflect.singular /^(ox)en/i, '\1' # inflect.irregular 'person',...
Ruby
# Be sure to restart your server when you modify this file. # These settings change the behavior of Rails 2 apps and will be defaults # for Rails 3. You can remove this initializer when Rails 3 is released. if defined?(ActiveRecord) # Include Active Record class name as root for JSON serialized output. ActiveReco...
Ruby
# Be sure to restart your server when you modify this file. # Add new mime types for use in respond_to blocks: # Mime::Type.register "text/richtext", :rtf # Mime::Type.register_alias "text/html", :iphone
Ruby
# Be sure to restart your server when you modify this file. # Your secret key for verifying cookie session data integrity. # If you change this key, all old sessions will become invalid! # Make sure the secret is at least 30 characters and all random, # no regular words or you'll be exposed to dictionary attacks. Act...
Ruby
# A Site key gives additional protection against a dictionary attack if your # DB is ever compromised. With no site key, we store # DB_password = hash(user_password, DB_user_salt) # If your database were to be compromised you'd be vulnerable to a dictionary # attack on all your stupid users' passwords. With a site...
Ruby
# Be sure to restart your server when you modify this file. # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces. # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ } # You can also remove all the silencers if you're trying do debug a probl...
Ruby
# Settings specified here will take precedence over those in config/environment.rb # The production environment is meant for finished, "live" apps. # Code is not reloaded between requests config.cache_classes = true # Full error reports are disabled and caching is turned on config.action_controller.consider_all_reque...
Ruby
# Settings specified here will take precedence over those in config/environment.rb # In the development environment your application's code is reloaded on # every request. This slows down response time but is perfect for development # since you don't have to restart the webserver when you make code changes. config.ca...
Ruby
# Settings specified here will take precedence over those in config/environment.rb # The test environment is used exclusively to run your application's # test suite. You never need to work with it otherwise. Remember that # your test database is "scratch space" for the test suite and is wiped # and recreated between...
Ruby
# Be sure to restart your server when you modify this file # Specifies gem version of Rails to use when vendor/rails is not present RAILS_GEM_VERSION = '2.3.4' unless defined? RAILS_GEM_VERSION # Bootstrap the Rails environment, frameworks, and default configuration require File.join(File.dirname(__FILE__), 'boot') ...
Ruby
# Don't change this file! # Configure your app in config/environment.rb and config/environments/*.rb RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT) module Rails class << self def boot! unless booted? preinitialize pick_boot.run end end def booted? ...
Ruby
ActionController::Routing::Routes.draw do |map| map.dashboard '/dashboard', :controller => 'home', :action => 'dashboard' map.logout '/logout', :controller => 'sessions', :action => 'destroy' map.login '/login', :controller => 'sessions', :action => 'new' map.register '/register', :controller => 'users', ...
Ruby
# # Where to go # # # GET # Go to a given page. When "$actor goes to $path" do |actor, path| case path when 'the home page' then get '/' else get path end end # POST -- Ex: # When she creates a book with ISBN: '0967539854' and comment: 'I love this book' and rating: '4' # When she cre...
Ruby
# The flexible code for resource testing came out of code from Ben Mabey # http://www.benmabey.com/2008/02/04/rspec-plain-text-stories-webrat-chunky-bacon/ # # Construct resources # # # Build a resource as described, store it as an @instance variable. Ex: # "Given a user with login: 'mojojojo'" # produces a User in...
Ruby
Before do Fixtures.reset_cache fixtures_folder = File.join(RAILS_ROOT, 'spec', 'fixtures') Fixtures.create_fixtures(fixtures_folder, "users") end # Make visible for testing ApplicationController.send(:public, :logged_in?, :current_user, :authorized?)
Ruby
# If you have a global stories helper, move this line there: include AuthenticatedTestHelper # Most of the below came out of code from Ben Mabey # http://www.benmabey.com/2008/02/04/rspec-plain-text-stories-webrat-chunky-bacon/ # These allow exceptions to come through as opposed to being caught and having non-helpful...
Ruby
# # What you should see when you get there # # # Destinations. Ex: # She should be at the new kids page # Tarkin should be at the destroy alderaan page # The visitor should be at the '/lolcats/download' form # The visitor should be redirected to '/hi/mom' # # It doesn't know anything about actual routes -- it...
Ruby
RE_User = %r{(?:(?:the )? *(\w+) *)} RE_User_TYPE = %r{(?: *(\w+)? *)} # # Setting # Given "an anonymous user" do log_out! end Given "$an $user_type user with $attributes" do |_, user_type, attributes| create_user! user_type, attributes.to_hash_from_story end Given "$an $user_type user named '$login'" do |...
Ruby
# Methods added to this helper will be available to all templates in the application. module ApplicationHelper end
Ruby
module UsersHelper # # Use this to wrap view elements that the user can't access. # !! Note: this is an *interface*, not *security* feature !! # You need to do all access control at the controller level. # # Example: # <%= if_authorized?(:index, User) do link_to('List all users', users_path) end %> ...
Ruby
module SessionsHelper end
Ruby
module MoviesHelper end
Ruby
require 'digest/sha1' class User < ActiveRecord::Base include Authentication include Authentication::ByPassword include Authentication::ByCookieToken validates_presence_of :login, :birthdate validates_length_of :login, :within => 3..40 validates_uniqueness_of :login validates_fo...
Ruby
class Movie < ActiveRecord::Base validates_presence_of :title, :description validates_uniqueness_of :title validates_length_of :description, :minimum => 10 validates_inclusion_of :rating, :in => %w{ G PG PG-13 R NC-17 } def appropriate_for_birthdate?(birthdate) if %w{ R NC-17 }.include?(ratin...
Ruby
class HomeController < ApplicationController include AuthenticatedSystem def dashboard @user = current_user @movies = Movie.find_all_appropriate_for_birthdate(current_user.birthdate) end end
Ruby
class MoviesController < ApplicationController before_filter :logged_in # GET /movies # GET /movies.xml def index @movies = Movie.all respond_to do |format| format.html # index.html.erb format.xml { render :xml => @movies } end end # GET /movies/1 # GET /movies/1...
Ruby
# This controller handles the login/logout function of the site. class SessionsController < ApplicationController # Be sure to include AuthenticationSystem in Application Controller instead include AuthenticatedSystem # render new.rhtml def new end def create logout_keeping_session! user = User....
Ruby
# Filters added to this controller apply to all controllers in the application. # Likewise, all the methods added will be available for all controllers. class ApplicationController < ActionController::Base include AuthenticatedSystem helper :all # include all helpers, all the time protect_from_forgery # Se...
Ruby
class UsersController < ApplicationController # Be sure to include AuthenticationSystem in Application Controller instead include AuthenticatedSystem # render new.rhtml def new @user = User.new end def create logout_keeping_session! @user = User.new(params[:user]) success = @user && @u...
Ruby
# Add your own tasks in files placed in lib/tasks ending in .rake, # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. require(File.join(File.dirname(__FILE__), 'config', 'boot')) require 'rake' require 'rake/testtask' require 'rake/rdoctask' require 'tasks/rails'
Ruby
#!/usr/bin/env ruby require File.expand_path('../../config/boot', __FILE__) require 'commands/dbconsole'
Ruby
#!/usr/bin/env ruby require File.expand_path('../../config/boot', __FILE__) require 'commands/console'
Ruby
#!/usr/bin/env ruby require File.expand_path('../../config/boot', __FILE__) require 'commands/runner'
Ruby
#!/usr/bin/env ruby require File.expand_path('../../../config/boot', __FILE__) require 'commands/performance/benchmarker'
Ruby
#!/usr/bin/env ruby require File.expand_path('../../../config/boot', __FILE__) require 'commands/performance/benchmarker'
Ruby
#!/usr/bin/env ruby require File.expand_path('../../../config/boot', __FILE__) require 'commands/performance/profiler'
Ruby
#!/usr/bin/env ruby require File.expand_path('../../../config/boot', __FILE__) require 'commands/performance/profiler'
Ruby
#!/usr/bin/env ruby require File.expand_path('../../config/boot', __FILE__) require 'commands/plugin'
Ruby
#!/usr/bin/env ruby require File.expand_path('../../config/boot', __FILE__) require 'commands/destroy'
Ruby
#!/usr/bin/env ruby require File.expand_path('../../config/boot', __FILE__) $LOAD_PATH.unshift "#{RAILTIES_PATH}/builtin/rails_info" require 'commands/about'
Ruby
#!/usr/bin/env ruby if ARGV.any? {|arg| %w[--drb -X --generate-options -G --help -h --version -v].include?(arg)} require 'rubygems' unless ENV['NO_RUBYGEMS'] else gem 'test-unit', '1.2.3' if RUBY_VERSION.to_f >= 1.9 ENV["RAILS_ENV"] ||= 'test' require File.expand_path(File.dirname(__FILE__) + "/../config/enviro...
Ruby
#!/usr/bin/env ruby gem 'test-unit', '1.2.3' if RUBY_VERSION.to_f >= 1.9 puts "Loading Rails environment" ENV["RAILS_ENV"] ||= 'test' require File.expand_path(File.dirname(__FILE__) + "/../config/environment") unless defined?(RAILS_ROOT) require 'optparse' require 'spec/rails/spec_server'
Ruby
#!/usr/bin/env ruby require File.expand_path('../../config/boot', __FILE__) require 'commands/dbconsole'
Ruby
#!/usr/bin/env ruby require File.expand_path('../../config/boot', __FILE__) require 'commands/runner'
Ruby
#!/usr/bin/env ruby require File.expand_path('../../config/boot', __FILE__) require 'commands/server'
Ruby
#!/usr/bin/env ruby require File.expand_path('../../config/boot', __FILE__) require 'commands/generate'
Ruby
#!/usr/bin/env ruby require File.expand_path('../../config/boot', __FILE__) require 'commands/console'
Ruby
#!/usr/bin/env ruby gem 'test-unit', '1.2.3' if RUBY_VERSION.to_f >= 1.9 ENV['RSPEC'] = 'true' # allows autotest to discover rspec ENV['AUTOTEST'] = 'true' # allows autotest to run w/ color on linux system((RUBY_PLATFORM =~ /mswin|mingw/ ? 'autotest.bat' : 'autotest'), *ARGV) || $stderr.puts("Unable to find auto...
Ruby
#!/usr/bin/env ruby gem 'test-unit', '1.2.3' if RUBY_VERSION.to_f >= 1.9 puts "Loading Rails environment" ENV["RAILS_ENV"] ||= 'test' require File.expand_path(File.dirname(__FILE__) + "/../config/environment") unless defined?(RAILS_ROOT) require 'optparse' require 'spec/rails/spec_server'
Ruby
#!/usr/bin/env ruby require File.expand_path('../../config/boot', __FILE__) require 'commands/destroy'
Ruby
#!/usr/bin/env ruby require File.expand_path('../../config/boot', __FILE__) require 'commands/generate'
Ruby
#!/usr/bin/env ruby require File.expand_path('../../config/boot', __FILE__) require 'commands/server'
Ruby
#!/usr/bin/env ruby gem 'test-unit', '1.2.3' if RUBY_VERSION.to_f >= 1.9 ENV['RSPEC'] = 'true' # allows autotest to discover rspec ENV['AUTOTEST'] = 'true' # allows autotest to run w/ color on linux system((RUBY_PLATFORM =~ /mswin|mingw/ ? 'autotest.bat' : 'autotest'), *ARGV) || $stderr.puts("Unable to find auto...
Ruby
#!/usr/bin/env ruby require File.expand_path('../../config/boot', __FILE__) $LOAD_PATH.unshift "#{RAILTIES_PATH}/builtin/rails_info" require 'commands/about'
Ruby
#!/usr/bin/env ruby require File.expand_path('../../config/boot', __FILE__) require 'commands/plugin'
Ruby
#!/usr/bin/env ruby if ARGV.any? {|arg| %w[--drb -X --generate-options -G --help -h --version -v].include?(arg)} require 'rubygems' unless ENV['NO_RUBYGEMS'] else gem 'test-unit', '1.2.3' if RUBY_VERSION.to_f >= 1.9 ENV["RAILS_ENV"] ||= 'test' require File.expand_path(File.dirname(__FILE__) + "/../config/enviro...
Ruby
class CreateUsers < ActiveRecord::Migration def self.up create_table "users", :force => true do |t| t.column :login, :string, :limit => 40 t.column :name, :string, :limit => 100, :default => '', :null => true t.column :email, :string, ...
Ruby
class CreateMovies < ActiveRecord::Migration def self.up create_table :movies do |t| t.string :title t.text :description t.string :rating t.datetime :released_on t.timestamps end end def self.down drop_table :movies end end
Ruby
class AddBirthdates < ActiveRecord::Migration def self.up add_column :users, :birthdate, :datetime end def self.down end end
Ruby
# This file should contain all the record creation needed to seed the database with its default values. # The data can then be loaded with the rake db:seed (or created alongside the db with db:setup). # # Examples: # # cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }]) # Major.create(:name...
Ruby
# This file is copied to ~/spec when you run 'ruby script/generate rspec' # from the project root directory. ENV["RAILS_ENV"] ||= 'test' require File.dirname(__FILE__) + "/../config/environment" unless defined?(RAILS_ROOT) require 'spec/autorun' require 'spec/rails' # Requires supporting files with custom matchers and...
Ruby
module AuthenticatedSystem protected # Returns true or false if the user is logged in. # Preloads @current_user with the user model if they're logged in. def logged_in? !!current_user end # Accesses the current user from the session. # Future calls avoid the database because nil is not ...
Ruby
gem 'test-unit', '1.2.3' if RUBY_VERSION.to_f >= 1.9 rspec_gem_dir = nil Dir["#{RAILS_ROOT}/vendor/gems/*"].each do |subdir| rspec_gem_dir = subdir if subdir.gsub("#{RAILS_ROOT}/vendor/gems/","") =~ /^(\w+-)?rspec-(\d+)/ && File.exist?("#{subdir}/lib/spec/rake/spectask.rb") end rspec_plugin_dir = File.expand_path(Fil...
Ruby
module AuthenticatedTestHelper # Sets the current user in the session from the user fixtures. def login_as(user) @request.session[:user_id] = user ? (user.is_a?(User) ? user.id : users(user).id) : nil end def authorize_as(user) @request.env["HTTP_AUTHORIZATION"] = user ? ActionController::HttpAuthentic...
Ruby
ENV["RAILS_ENV"] = "test" require File.expand_path(File.dirname(__FILE__) + "/../config/environment") require 'test_help' class ActiveSupport::TestCase # Transactional fixtures accelerate your tests by wrapping each test method # in a transaction that's rolled back on completion. This ensures that the # test da...
Ruby