From f0ca3a5655f7a2dd14947a4f256796080b2b9dcc Mon Sep 17 00:00:00 2001 From: Apprentice Date: Thu, 14 Feb 2013 16:06:29 -0800 Subject: [PATCH 1/5] implemented change method --- db/migrate/20121011144238_create_students.rb | 12 +++++++++++- spec/migrate_create_table_spec.rb | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/db/migrate/20121011144238_create_students.rb b/db/migrate/20121011144238_create_students.rb index 8f5293c..753dea3 100644 --- a/db/migrate/20121011144238_create_students.rb +++ b/db/migrate/20121011144238_create_students.rb @@ -4,6 +4,16 @@ class CreateStudents < ActiveRecord::Migration def change - # HINT: checkout ActiveRecord::Migration.create_table + create_table :students do |t| + t.integer :id + t.string :first_name + t.string :last_name + t.string :gender + t.string :email + t.string :phone + t.date :birthday + # t.datetime :created_at + # t.datetime :updated_at + end end end diff --git a/spec/migrate_create_table_spec.rb b/spec/migrate_create_table_spec.rb index 52fc71c..e7aed17 100644 --- a/spec/migrate_create_table_spec.rb +++ b/spec/migrate_create_table_spec.rb @@ -19,7 +19,7 @@ } ActiveRecord::Base.connection.columns(:students).each do |col| - expected[col.type].include?(col.name).should be_true + expected[col.type].include?(col.name).should be_true unless expected[col.type].nil? end end end From f53a313427b636851289e3bfcdd6444124e9ff5f Mon Sep 17 00:00:00 2001 From: Apprentice Date: Thu, 14 Feb 2013 20:38:01 -0800 Subject: [PATCH 2/5] Added validations --- app/models/student.rb | 26 +++++++++++++++++++- db/migrate/20121011144238_create_students.rb | 2 +- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/app/models/student.rb b/app/models/student.rb index 8cce3f9..0925d7e 100644 --- a/app/models/student.rb +++ b/app/models/student.rb @@ -1,3 +1,27 @@ require_relative '../../db/config' -# implement your Student model here +class Student < ActiveRecord::Base + + validates :email, :uniqueness => true + validates :email, :format => { :with => /\S+[@]\w+[.]\w{2,}/, :message => "Invalid email" } + validates :age, :numericality => {:greater_than => 4} + validates :sanitized_number, :numericality => {:greater_than => 9} + + # Does it matter if age and sanitized_number are instance variables (@age and @sanitized_number)? + + + def name + name = "#{first_name} #{last_name}" + end + + def age + now = Date.today + @age = now.year - birthday.year - ((now.month > birthday.month || (now.month == birthday.month && now.day >= birthday.day)) ? 0 : 1) + end + + def sanitized_number + @sanitized_number = phone.gsub(/\D/, '').length + end +end + + diff --git a/db/migrate/20121011144238_create_students.rb b/db/migrate/20121011144238_create_students.rb index 753dea3..b5ffe44 100644 --- a/db/migrate/20121011144238_create_students.rb +++ b/db/migrate/20121011144238_create_students.rb @@ -9,7 +9,7 @@ def change t.string :first_name t.string :last_name t.string :gender - t.string :email + t.string :email , :unique => true #-> Why is this not working? t.string :phone t.date :birthday # t.datetime :created_at From 982141ce7975d34a9624d96e88cb0020d217684b Mon Sep 17 00:00:00 2001 From: Apprentice Date: Thu, 14 Feb 2013 20:59:01 -0800 Subject: [PATCH 3/5] What did I do? --- app/models/student.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/models/student.rb b/app/models/student.rb index 0925d7e..5e5d9e2 100644 --- a/app/models/student.rb +++ b/app/models/student.rb @@ -15,12 +15,11 @@ def name end def age - now = Date.today - @age = now.year - birthday.year - ((now.month > birthday.month || (now.month == birthday.month && now.day >= birthday.day)) ? 0 : 1) + now.year - birthday.year - ((now.month > birthday.month || (now.month == birthday.month && now.day >= birthday.day)) ? 0 : 1) end def sanitized_number - @sanitized_number = phone.gsub(/\D/, '').length + phone.gsub(/\D/, '').length end end From 97aa4e8f4d4f83f3cd4487c426e8be12450b2841 Mon Sep 17 00:00:00 2001 From: Apprentice Date: Thu, 14 Feb 2013 22:20:54 -0800 Subject: [PATCH 4/5] Added possibility to add fake teachers --- Gemfile | 1 + Rakefile | 12 ++++++++++++ db/migrate/20121011144238_create_students.rb | 3 +-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index dea7594..9db96ce 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,4 @@ source 'https://rubygems.org' gem 'activerecord', :require => 'active_record' +gem 'faker' diff --git a/Rakefile b/Rakefile index 5e92e3d..006acb2 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,7 @@ require 'rake' require 'rspec/core/rake_task' +require 'faker' +require_relative 'app/models/teacher' require_relative 'db/config' require_relative 'lib/students_importer' @@ -28,6 +30,16 @@ task "db:populate" do StudentsImporter.import end +task "db:populate_teacher" do + 10.times do + teacher = Teacher.create! ( + {:first_name => Faker::Name.first_name, + :last_name => Faker::Name.last_name, + :email => Faker::Internet.email, + :phone => Faker::PhoneNumber.phone_number }) + end +end + desc 'Retrieves the current schema version number' task "db:version" do puts "Current version: #{ActiveRecord::Migrator.current_version}" diff --git a/db/migrate/20121011144238_create_students.rb b/db/migrate/20121011144238_create_students.rb index b5ffe44..551fdd0 100644 --- a/db/migrate/20121011144238_create_students.rb +++ b/db/migrate/20121011144238_create_students.rb @@ -1,6 +1,5 @@ require_relative '../config' -# this is where you should use an ActiveRecord migration to class CreateStudents < ActiveRecord::Migration def change @@ -9,7 +8,7 @@ def change t.string :first_name t.string :last_name t.string :gender - t.string :email , :unique => true #-> Why is this not working? + t.string :email t.string :phone t.date :birthday # t.datetime :created_at From d46e4b6d17e5cda56188e8bdb735a1f4c9d56a86 Mon Sep 17 00:00:00 2001 From: Apprentice Date: Thu, 14 Feb 2013 22:34:29 -0800 Subject: [PATCH 5/5] Readded accidentally deleted files --- app/models/teachers.rb | 7 +++++++ db/migrate/20121011144238_create_teachers.rb | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 app/models/teachers.rb create mode 100644 db/migrate/20121011144238_create_teachers.rb diff --git a/app/models/teachers.rb b/app/models/teachers.rb new file mode 100644 index 0000000..4aa466c --- /dev/null +++ b/app/models/teachers.rb @@ -0,0 +1,7 @@ +require_relative '../../db/config' + +class Teacher < ActiveRecord::Base + + validates :email, :uniqueness => true + +end diff --git a/db/migrate/20121011144238_create_teachers.rb b/db/migrate/20121011144238_create_teachers.rb new file mode 100644 index 0000000..a7bc7f8 --- /dev/null +++ b/db/migrate/20121011144238_create_teachers.rb @@ -0,0 +1,17 @@ +require_relative '../config' + + +class CreateStudents < ActiveRecord::Migration + def change + create_table :teachers do |t| + t.integer :id + t.string :first_name + t.string :last_name + t.string :gender + t.string :email + t.string :phone + # t.datetime :created_at + # t.datetime :updated_at + end + end +end