2012-03-20_08.52.33
Look at rspec
to not include default testing framework:
rails new AppName -T
In Gemfile:
gem 'rspec-rails'
ericm@finance:~/ruby/MeatdownApp$ rails g rspec:install create .rspec create spec create spec/spec_helper.rb
rails g model User name:string invoke active_record create db/migrate/20120320135609_create_users.rb create app/models/user.rb invoke rspec create spec/models/user_spec.rb
ericm@finance:~/ruby/MeatdownApp$ rake db:test:prepare
to update test database
History for rails and rspec project:
2005 rails new MeatdownApp -T 2006 cd MeatdownApp/ 2007 bundle install 2008 rails g rspec:install 2009 rails g model User name:string 2010 rake db:migrate 2011 rspec spec/models/ 2012 bundle install 2013 rspec 2014 rake db:test:prepare 2015 rspec 2016 gem list --local 2017 bundle install 2018 rspec 2019 bundle install 2020 rspec 2021 bundle exec install 2022 bundle exec 2023 ruby -v 2024 rails g model Meetup title:string 2025 rake db:migrate 2026 rake db:test:prepare 2027 cp -v /home/ericm/github/digital_schoolhouse/.rvmrc . 2028 emacsnw .rvmrc 2029 cd ../ 2030 cd MeatdownApp/ 2031 bundle install 2032 rspec 2033 rails g migration AddUserIdToMeetup user_id:integer 2034 rake db:migrate 2035 more db/schema.rb 2036 rake db:test:prepare 2037 rake 2038 rspec 2039 history
Dave Hoover
He had to create his own software engineering education
His advice:
1. seek out mentors
2. kindred spirits
3. Community
He tries to be the worst person on the team
Keep diving deeper into Rails, know about testing
he uses TestUnit – no RSpec back then
Expand your bandwidth, drink from the firehose
Learning is fun, but debugging may not be
Learn how to expand/contract your bandwidth
After Code Academy: Don’t be discouraged
Very few people just walk right into a job
It takes some time
Reach out, ask people in the community, find out what is out there
Apprentice: 8th Light, Groupon, Trunk Club
You will need to hustle
——
.rspec file:
--format nested --color
We want a model without a database table
rails g model CoinChanger --no-migration
No migration generated, but it still has ActiveRecord in it
class CoinChanger < ActiveRecord::Base end
so take out “< ActiveRecord::Base”
If you just run “rake”, it will run your tests
Jeff do not test your views, just your models and controllers
2012-03-22_08.30.05
Modules: you can include or extend a module
you can extend a module and use its methods as class methods by calling with “extend”
in irb: you can require or include files
require "./modules"
if it is in same dir from which you called irb
You could also “load” a file
load "modules.rb"
This will reload the file if you call it again
in the file you can call “require relative ‘modules'” which goes from pwd of the file
Modules extend a class that extends Kernel
It is better to use composition than inheritance many times
inheritance cements the relationships. It only works for one kind of re-use: is-a
In modules you can make a method self.method_name and you can make those available at module level
No instance needed
self keyword: it refers to the current object
class scoped code is executed when code is read/loaded/class initialized
It is only run once
class Table end puts self
That second self will print “main” even though you are not in a class, “out in hyperspace” as Jeff puts it
In class scope, self returns name of table, inside an instance method, self refers to the instance
— look for a “collect” method
We could make a method called “method_missing” that will handle any calls to undefined methods
def method_missing(m, *args ) end
m is a symbol that is the name of the ghost method
*args is an array of args
def method_missing(m, *args ) puts "whoa! you tried to call #{m.to_s}, with these args:" puts args.inspect end
This may be how Rails does find_by_column name:
it uses regular expressions in method_missing
You could also call super in method_missing
Another use of modules: namespacing
module Jeff class Boat end end
b1 = Jeff::Boat.new b2 = John::Boat.new
If you have two classes with the same name, you will get the union of the classes
If the same method name is used, the last method defined wins
You could add to String, Array, Hash
modules package stuff for re-use and good for namespacing
You can call instance.respond_to?(length) to see if it will respond to that method call
s = "Hello" s.respond_to?(:length) s.send(:length)
You may get stuff in a variable
gem list acti
will give the gems with “acti” in the name
gem which will_paginate
will tell you where the gem was installed
gem list -d activesupport
In rails apps, there is a lib/tasks directory
Make a file called flights.rake
it must go in a block
task :jeff do puts "hi" end
Then you can call “rake jeff”
To see in the list, put
desc “sample rake task”
in the file – before line with :task
task :prereq do end task :jeff => :prereq do end
This will make the :jeff task run the :prereq task first
What if you deal with models?
task :list => :environment do puts "We have #{Airport.count} airports" end
You can namespace your tasks
namespace :air do task :jeff do end end
Image from World Digital Library, assumed allowed under Fair Use. Image from the Psalter of Frederick II, a 13th century manuscript in the Byzantine style, housed at Riccardiana Library of Florence, aka Biblioteca Riccardiana.