diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2007-09-09 23:30:09 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2007-09-09 23:30:09 +0000 |
commit | 250f871b9012fc461f83d7767cc49fe40dbbc104 (patch) | |
tree | 284266c8e0fc1a99b201092a0e999c09084aab5f | |
parent | ff9ca2ca1e1646e22ff43ec1a5844a0b733d5ba3 (diff) | |
download | rails-250f871b9012fc461f83d7767cc49fe40dbbc104.tar.gz rails-250f871b9012fc461f83d7767cc49fe40dbbc104.tar.bz2 rails-250f871b9012fc461f83d7767cc49fe40dbbc104.zip |
Removed outdated benchmarks and examples
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7439 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rwxr-xr-x | activerecord/Rakefile | 2 | ||||
-rw-r--r-- | activerecord/benchmarks/benchmark.rb | 26 | ||||
-rw-r--r-- | activerecord/benchmarks/mysql_benchmark.rb | 19 | ||||
-rw-r--r-- | activerecord/examples/associations.rb | 87 | ||||
-rw-r--r-- | activerecord/examples/shared_setup.rb | 15 | ||||
-rw-r--r-- | activerecord/examples/validation.rb | 85 |
6 files changed, 1 insertions, 233 deletions
diff --git a/activerecord/Rakefile b/activerecord/Rakefile index 9a9e00747a..479c6286f7 100755 --- a/activerecord/Rakefile +++ b/activerecord/Rakefile @@ -136,7 +136,7 @@ end # Create compressed packages -dist_dirs = [ "lib", "test", "examples", "dev-utils" ] +dist_dirs = [ "lib", "test", "examples" ] spec = Gem::Specification.new do |s| s.name = PKG_NAME diff --git a/activerecord/benchmarks/benchmark.rb b/activerecord/benchmarks/benchmark.rb deleted file mode 100644 index de390c11b2..0000000000 --- a/activerecord/benchmarks/benchmark.rb +++ /dev/null @@ -1,26 +0,0 @@ -$:.unshift(File.dirname(__FILE__) + '/../lib') -if ARGV[2] - require 'rubygems' - gem 'activerecord', ARGV[2] -else - require 'active_record' -end - -ActiveRecord::Base.establish_connection(:adapter => "mysql", :database => "basecamp") - -class Post < ActiveRecord::Base; end - -require 'benchmark' - -RUNS = ARGV[0].to_i -if ARGV[1] == "profile" then require 'profile' end - -runtime = Benchmark::measure { - RUNS.times { - Post.find_all(nil,nil,100).each { |p| p.title } - } -} - -puts "Runs: #{RUNS}" -puts "Avg. runtime: #{runtime.real / RUNS}" -puts "Requests/second: #{RUNS / runtime.real}" diff --git a/activerecord/benchmarks/mysql_benchmark.rb b/activerecord/benchmarks/mysql_benchmark.rb deleted file mode 100644 index 2f9e0e6999..0000000000 --- a/activerecord/benchmarks/mysql_benchmark.rb +++ /dev/null @@ -1,19 +0,0 @@ -require 'mysql' - -conn = Mysql::real_connect("localhost", "root", "", "basecamp") - -require 'benchmark' - -require 'profile' if ARGV[1] == "profile" -RUNS = ARGV[0].to_i - -runtime = Benchmark::measure { - RUNS.times { - result = conn.query("SELECT * FROM posts LIMIT 100") - result.each_hash { |p| p["title"] } - } -} - -puts "Runs: #{RUNS}" -puts "Avg. runtime: #{runtime.real / RUNS}" -puts "Requests/second: #{RUNS / runtime.real}"
\ No newline at end of file diff --git a/activerecord/examples/associations.rb b/activerecord/examples/associations.rb deleted file mode 100644 index b0df367321..0000000000 --- a/activerecord/examples/associations.rb +++ /dev/null @@ -1,87 +0,0 @@ -require File.dirname(__FILE__) + '/shared_setup' - -logger = Logger.new(STDOUT) - -# Database setup --------------- - -logger.info "\nCreate tables" - -[ "DROP TABLE companies", "DROP TABLE people", "DROP TABLE people_companies", - "CREATE TABLE companies (id int(11) auto_increment, client_of int(11), name varchar(255), type varchar(100), PRIMARY KEY (id))", - "CREATE TABLE people (id int(11) auto_increment, name varchar(100), PRIMARY KEY (id))", - "CREATE TABLE people_companies (person_id int(11), company_id int(11), PRIMARY KEY (person_id, company_id))", -].each { |statement| - # Tables doesn't necessarily already exist - begin; ActiveRecord::Base.connection.execute(statement); rescue ActiveRecord::StatementInvalid; end -} - - -# Class setup --------------- - -class Company < ActiveRecord::Base - has_and_belongs_to_many :people, :class_name => "Person", :join_table => "people_companies", :table_name => "people" -end - -class Firm < Company - has_many :clients, :foreign_key => "client_of" - - def people_with_all_clients - clients.inject([]) { |people, client| people + client.people } - end -end - -class Client < Company - belongs_to :firm, :foreign_key => "client_of" -end - -class Person < ActiveRecord::Base - has_and_belongs_to_many :companies, :join_table => "people_companies" - def self.table_name() "people" end -end - - -# Usage --------------- - -logger.info "\nCreate fixtures" - -Firm.new("name" => "Next Angle").save -Client.new("name" => "37signals", "client_of" => 1).save -Person.new("name" => "David").save - - -logger.info "\nUsing Finders" - -next_angle = Company.find(1) -next_angle = Firm.find(1) -next_angle = Company.find_first "name = 'Next Angle'" -next_angle = Firm.find_by_sql("SELECT * FROM companies WHERE id = 1").first - -Firm === next_angle - - -logger.info "\nUsing has_many association" - -next_angle.has_clients? -next_angle.clients_count -all_clients = next_angle.clients - -thirty_seven_signals = next_angle.find_in_clients(2) - - -logger.info "\nUsing belongs_to association" - -thirty_seven_signals.has_firm? -thirty_seven_signals.firm?(next_angle) - - -logger.info "\nUsing has_and_belongs_to_many association" - -david = Person.find(1) -david.add_companies(thirty_seven_signals, next_angle) -david.companies.include?(next_angle) -david.companies_count == 2 - -david.remove_companies(next_angle) -david.companies_count == 1 - -thirty_seven_signals.people.include?(david)
\ No newline at end of file diff --git a/activerecord/examples/shared_setup.rb b/activerecord/examples/shared_setup.rb deleted file mode 100644 index 6ede4b1d35..0000000000 --- a/activerecord/examples/shared_setup.rb +++ /dev/null @@ -1,15 +0,0 @@ -# Be sure to change the mysql_connection details and create a database for the example - -$: << File.dirname(__FILE__) + '/../lib' - -require 'active_record' -require 'logger'; class Logger; def format_message(severity, timestamp, msg, progname) "#{msg}\n" end; end - -ActiveRecord::Base.logger = Logger.new(STDOUT) -ActiveRecord::Base.establish_connection( - :adapter => "mysql", - :host => "localhost", - :username => "root", - :password => "", - :database => "activerecord_examples" -) diff --git a/activerecord/examples/validation.rb b/activerecord/examples/validation.rb deleted file mode 100644 index e6a448afb8..0000000000 --- a/activerecord/examples/validation.rb +++ /dev/null @@ -1,85 +0,0 @@ -require File.dirname(__FILE__) + '/shared_setup' - -logger = Logger.new(STDOUT) - -# Database setup --------------- - -logger.info "\nCreate tables" - -[ "DROP TABLE people", - "CREATE TABLE people (id int(11) auto_increment, name varchar(100), pass varchar(100), email varchar(100), PRIMARY KEY (id))" -].each { |statement| - begin; ActiveRecord::Base.connection.execute(statement); rescue ActiveRecord::StatementInvalid; end # Tables doesn't necessarily already exist -} - - -# Class setup --------------- - -class Person < ActiveRecord::Base - # Using - def self.authenticate(name, pass) - # find_first "name = '#{name}' AND pass = '#{pass}'" would be open to sql-injection (in a web-app scenario) - find_first [ "name = '%s' AND pass = '%s'", name, pass ] - end - - def self.name_exists?(name, id = nil) - if id.nil? - condition = [ "name = '%s'", name ] - else - # Check if anyone else than the person identified by person_id has that user_name - condition = [ "name = '%s' AND id <> %d", name, id ] - end - - !find_first(condition).nil? - end - - def email_address_with_name - "\"#{name}\" <#{email}>" - end - - protected - def validate - errors.add_on_empty(%w(name pass email)) - errors.add("email", "must be valid") unless email_address_valid? - end - - def validate_on_create - if attribute_present?("name") && Person.name_exists?(name) - errors.add("name", "is already taken by another person") - end - end - - def validate_on_update - if attribute_present?("name") && Person.name_exists?(name, id) - errors.add("name", "is already taken by another person") - end - end - - private - def email_address_valid?() email =~ /\w[-.\w]*\@[-\w]+[-.\w]*\.\w+/ end -end - -# Usage --------------- - -logger.info "\nCreate fixtures" -david = Person.new("name" => "David Heinemeier Hansson", "pass" => "", "email" => "") -unless david.save - puts "There was #{david.errors.count} error(s)" - david.errors.each_full { |error| puts error } -end - -david.pass = "something" -david.email = "invalid_address" -unless david.save - puts "There was #{david.errors.count} error(s)" - puts "It was email with: " + david.errors.on("email") -end - -david.email = "david@loudthinking.com" -if david.save then puts "David finally made it!" end - - -another_david = Person.new("name" => "David Heinemeier Hansson", "pass" => "xc", "email" => "david@loudthinking") -unless another_david.save - puts "Error on name: " + another_david.errors.on("name") -end
\ No newline at end of file |