http://guides.rubyonrails.org/migrations.html
- add_column
- add_index
- change_column
- change_table
- create_table
- drop_table
| =Navigating= | |
| visit('/projects') | |
| visit(post_comments_path(post)) | |
| =Clicking links and buttons= | |
| click_link('id-of-link') | |
| click_link('Link Text') | |
| click_button('Save') | |
| click('Link Text') # Click either a link or a button | |
| click('Button Value') |
| # Goal: Allow addition of instances to a collection in a factory-built object | |
| # when those instances require references to the parent. | |
| # Typically occurs in Rails when one model has_many instances of another | |
| # See more at: | |
| # http://stackoverflow.com/questions/2937326/populating-an-association-with-children-in-factory-girl | |
| class Factory | |
| def has_many(collection) | |
| # after_build is where you add instances to the factory-built collection. |
| # autoload concerns | |
| module YourApp | |
| class Application < Rails::Application | |
| config.autoload_paths += %W( | |
| #{config.root}/app/controllers/concerns | |
| #{config.root}/app/models/concerns | |
| ) | |
| end | |
| end |
| #Model | |
| @user.should have(1).error_on(:username) # Checks whether there is an error in username | |
| @user.errors[:username].should include("can't be blank") # check for the error message | |
| #Rendering | |
| response.should render_template(:index) | |
| #Redirecting | |
| response.should redirect_to(movies_path) |
| class UrlValidator < ActiveModel::EachValidator | |
| def validate_each(record, attribute, value) | |
| valid = begin | |
| URI.parse(value).kind_of?(URI::HTTP) | |
| rescue URI::InvalidURIError | |
| false | |
| end | |
| unless valid | |
| record.errors[attribute] << (options[:message] || "is an invalid URL") |
| # db/migrate/20120625030355_add_deleted_at_to_user.rb | |
| class AddDeletedAtToUser < ActiveRecord::Migration | |
| def change | |
| add_column :users, :deleted_at, :time | |
| end | |
| end |
| ##Userable | |
| module Userable | |
| def self.included(base) | |
| base.has_one :user, :as => :userable, :dependent => :destroy, :autosave => true | |
| base.validate :user_must_be_valid | |
| base.alias_method_chain :user, :autobuild | |
| base.extend ClassMethods | |
| base.define_user_accessors | |
| end | |
| <?xml version="1.0" encoding="UTF-8"?> | |
| <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
| <plist version="1.0"> | |
| <dict> | |
| <key>comment</key> | |
| <string> | |
| TODO: unresolved issues | |
| text: |
| # This is a skeleton for testing models including examples of validations, callbacks, | |
| # scopes, instance & class methods, associations, and more. | |
| # Pick and choose what you want, as all models don't NEED to be tested at this depth. | |
| # | |
| # I'm always eager to hear new tips & suggestions as I'm still new to testing, | |
| # so if you have any, please share! | |
| # | |
| # This skeleton also assumes you're using the following gems: | |
| # | |
| # rspec-rails: https://github.com/rspec/rspec-rails |
http://guides.rubyonrails.org/migrations.html