code
stringlengths
1
1.73M
language
stringclasses
1 value
module BlogHelper end
Ruby
module HomeHelper end
Ruby
class Admin < ActiveRecord::Base end
Ruby
class Post < ActiveRecord::Base has_many :comments validates_presence_of :title, :body named_scope :recent, :limit => 3, :conditions => "created_at > '#{1.day.ago.to_s(:db)}'", :order => "created_at desc" named_scope :last_five, :limit => 5, :order => "created_at desc" def set_page(post) post.pa...
Ruby
class Comment < ActiveRecord::Base belongs_to :post attr_accessible :name, :body validates_presence_of :name, :body named_scope :end, :limit => 1, :order => "created_at desc" end
Ruby
class PostsController < ApplicationController def index if session[:admin].nil? redirect_to :controller => 'admin', :action => 'login' else @posts = Post.all respond_to do |format| format.html # index.html.erb format.xml { render :xml => @posts } end end end ...
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 helper :all # include all helpers, all the time protect_from_forgery # See ActionController::RequestForgeryPr...
Ruby
class ColophonController < ApplicationController layout "home" before_filter :clear_session def index @recent = Post.recent end end
Ruby
class BlogController < ApplicationController layout "home" before_filter :clear_session def index @last_five = Post.last_five @recent = Post.recent end def show @post = Post.find(params[:id]) @comments = @post.comments @recent = Post.recent end def whole_blog @posts = Post...
Ruby
class HomeController < ApplicationController before_filter :clear_session def index @recent = Post.recent end def show @post = Post.find(params[:id]) end end
Ruby
class ContactController < ApplicationController layout "home" before_filter :clear_session def index @recent = Post.recent end end
Ruby
class AdminController < ApplicationController def login if request.post? if params[:password] == "mom the meatloaf we want it now" session[:admin] = true redirect_to :controller => "posts" else flash[:notice] = "Invalid user/password combination" redirect_to :contro...
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. # 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
# 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
# Be sure to restart your server when you modify this file. # Your secret key for verifying the integrity of signed cookies. # If you change this key, all old signed cookies 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 atta...
Ruby
Time::DATE_FORMATS[:just_date] = "%b %d, %Y" Time::DATE_FORMATS[:just_time] = "%I:%M%p"
Ruby
ActionController::Routing::Routes.draw do |map| map.resources :posts # The priority is based upon order of creation: first created -> highest priority. # Sample of regular route: # map.connect 'products/:id', :controller => 'catalog', :action => 'view' # Keep in mind you can assign values other than :cont...
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
# 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.8' unless defined? RAILS_GEM_VERSION # Bootstrap the Rails environment, frameworks, and default configuration require File.join(File.dirname(__FILE__), 'boot') ...
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
# 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
# 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
class CreateProducts < ActiveRecord::Migration def self.up create_table :products do |t| t.string :title t.text :description t.string :image_url t.timestamps end end def self.down drop_table :products end end
Ruby
class CreateSessions < ActiveRecord::Migration def self.up create_table :sessions do |t| t.string :session_id, :null => false t.text :data t.timestamps end add_index :sessions, :session_id add_index :sessions, :updated_at end def self.down drop_table :sessions end end
Ruby
class CreateOrders < ActiveRecord::Migration def self.up create_table :orders do |t| t.string :name t.text :address t.string :email t.string :pay_type, :limit => 10 t.timestamps end end def self.down drop_table :orders end end
Ruby
class AddPriceToProduct < ActiveRecord::Migration def self.up add_column :products, :price, :decimal, :precision => 8, :scale => 2, :default => 0 end def self.down remove_column :products, :price end end
Ruby
class CreateLineItems < ActiveRecord::Migration def self.up create_table :line_items do |t| t.integer :product_id, :null => false, :options => "CONSTRAINT fk_line_item_products REFERENCES products(id)" t.integer :order_id, :null => false, :options => "CONSTRAINT fk_line_item_products R...
Ruby
class CreateQuestions < ActiveRecord::Migration def self.up create_table :questions do |t| t.text :question t.timestamps end end def self.down drop_table :questions end end
Ruby
class CreateUsers < ActiveRecord::Migration def self.up create_table :users do |t| t.string :name t.string :hashed_password t.string :salt t.timestamps end end def self.down drop_table :users end end
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
#!/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 require File.expand_path('../../config/boot', __FILE__) require 'commands/runner'
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/destroy'
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/console'
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__) $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/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/console'
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/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/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/generate'
Ruby
atom_feed do |feed| feed.title "Who bought #{@product.title}" feed.updated @orders.first.created_at for order in @orders feed.entry(order) do |entry| entry.title "Order #{order.id}" entry.summary :type => 'xhtml' do |xhtml| xhtml.p "Shipped to #{order.address}" xhtml.table do ...
Ruby
xml.order_list(:for_product => @product.title) do for o in @orders xml.order do xml.name(o.name) xml.email(o.email) end end end
Ruby
module LineItemsHelper end
Ruby
# Methods added to this helper will be available to all templates in the application. module ApplicationHelper end
Ruby
module InfoHelper end
Ruby
module StoreHelper def hidden_div_if(condition, attributes = {}, &block) if condition attributes["style"] = "display: none" end content_tag("div", attributes, &block) end end
Ruby
module OrdersHelper end
Ruby
module UsersHelper end
Ruby
module ProductsHelper end
Ruby
module AdminHelper end
Ruby
module QuestionsHelper end
Ruby
class LineItem < ActiveRecord::Base belongs_to :order belongs_to :product def self.from_cart_item(cart_item) li = self.new li.product = cart_item.product li.quantity = cart_item.quantity li.total_pric = cart_item.price li end end
Ruby
class Product < ActiveRecord::Base has_many :orders, :through => :line_items has_many :line_items def self.find_products_for_sale find(:all, :order => "title") end validates_presence_of :title, :description, :image_url validates_uniqueness_of :title validate :must_be_at_least_ten validat...
Ruby
class Question < ActiveRecord::Base validates_presence_of :question end
Ruby
class Cart attr_reader :items def initialize @items = [] end def add_product(product) current_item = @items.find {|item| item.product == product} if current_item current_item.increment_quantity else current_item = CartItem.new(product) @items << current_item end ...
Ruby
require 'digest/sha1' class User < ActiveRecord::Base validates_presence_of :name validates_uniqueness_of :name attr_accessor :password_conformation validates_confirmation_of :password validate :password_non_blank def password @password end def password=(pwd) @password = pwd ...
Ruby
class CartItem attr_reader :product, :quantity def initialize(product) @product = product @quantity = 1 end def increment_quantity @quantity += 1 end def decrement_quantity @quantity -= 1 if @quantity > 0 end def title @product.title end def price @product....
Ruby
class Order < ActiveRecord::Base PAYMENT_TYPES = [ [ "Check", "check"], [ "Credit Card", "cc"], [ "Purchase Order", "po"] ] validates_presence_of :name, :address, :email, :pay_type validates_inclusion_of :pay_type, :in => PAYMENT_TYPES.map {|disp, value| value} has_many :line_ite...
Ruby
class StoreController < ApplicationController before_filter :find_cart, :except => :empty_cart skip_before_filter :verify_authenticity_token def index @products = Product.find_products_for_sale end def add_to_cart product = Product.find(params[:id]) @current_item = @cart.add_product(prod...
Ruby
class ProductsController < ApplicationController # GET /products # GET /products.xml def index @products = Product.all respond_to do |format| format.html # index.html.erb format.xml { render :xml => @products } end end # GET /products/1 # GET /products/1.xml def show @produc...
Ruby
class QuestionsController < ApplicationController # GET /questions # GET /questions.xml def index @questions = Question.all respond_to do |format| format.html # index.html.erb format.xml { render :xml => @questions } end end # GET /questions/1 # GET /questions/1.xml def show ...
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 layout "store" before_filter :authorize, :except => :login helper :all # include all helpers, all the ti...
Ruby
class LineItemsController < ApplicationController # GET /line_items # GET /line_items.xml def index @line_items = LineItem.all respond_to do |format| format.html # index.html.erb format.xml { render :xml => @line_items } end end # GET /line_items/1 # GET /line_items/1.xml def sh...
Ruby
class UsersController < ApplicationController # GET /users # GET /users.xml def index @users = User.find(:all, :order => :name) respond_to do |format| format.html # index.html.erb format.xml { render :xml => @users } end end # GET /users/1 # GET /users/1.xml def show @user =...
Ruby
class OrdersController < ApplicationController # GET /orders # GET /orders.xml def index @orders = Order.all respond_to do |format| format.html # index.html.erb format.xml { render :xml => @orders } end end # GET /orders/1 # GET /orders/1.xml def show @order = Order.find(par...
Ruby
class AdminController < ApplicationController def login if request.post? user = User.authenticate(params[:name], params[:password]) if user session[:user_id] = user.id uri = session[:original_uri] session[:original_uri] = nil redirect_to(uri || { :controller => "sto...
Ruby
class InfoController < ApplicationController def who_bought @product = Product.find(params[:id]) @orders = @product.orders respond_to do |format| format.html format.json { render :layout => false, :json => @product.to_json(:include => :orders) } end end protected def authorize end...
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. # 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
# 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
I18n.default_locale = 'en' LOCALES_DIRECTORY = "#{RAILS_ROOT}/config/locales/" LANGUAGES = { 'English' => 'en', "Espa\xc3\xb1ol" => 'es' }
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
# Be sure to restart your server when you modify this file. # Your secret key for verifying the integrity of signed cookies. # If you change this key, all old signed cookies 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 atta...
Ruby
ActionController::Routing::Routes.draw do |map| map.resources :questions map.resources :questions map.resources :users map.resources :line_items map.resources :line_otems map.resources :orders map.resources :products # The priority is based upon order of creation: first created -> highest priorit...
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
# 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.8' unless defined? RAILS_GEM_VERSION # Bootstrap the Rails environment, frameworks, and default configuration require File.join(File.dirname(__FILE__), 'boot') ...
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
# 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
namespace :db do desc "Prints the migrated versions" task :schema_migrations => :environment do puts ActiveRecord::Base.connection.select_values( 'select version from schema_migrations order by version') end end
Ruby
# $ext_path: This should be the path of where the ExtJS SDK is installed # Generally this will be in a lib/extjs folder in your applications root # <root>/lib/extjs $ext_path = "../../" # sass_path: the directory your Sass files are in. THIS file should also be in the Sass folder # Generally this will be in a resource...
Ruby
# include the utils rb file which has extra functionality for the ext theme dir = File.dirname(__FILE__) require File.join(dir, 'lib', 'utils.rb') # register ext4 as a compass framework Compass::Frameworks.register 'ext4', dir
Ruby
# $ext_path: This should be the path of the Ext JS SDK relative to this file $ext_path = "../../extjs" # sass_path: the directory your Sass files are in. THIS file should also be in the Sass folder # Generally this will be in a resources/sass folder # <root>/resources/sass sass_path = File.dirname(__FILE__) # css_pat...
Ruby