Skip to content

Instantly share code, notes, and snippets.

@Ragmaanir
Created June 6, 2011 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ragmaanir/1011318 to your computer and use it in GitHub Desktop.
Save Ragmaanir/1011318 to your computer and use it in GitHub Desktop.
rails new abcdefg -m http://datamapper.org/templates/rails.rb --skip-test-unit
cd abcdefg
# copy gemfile from this gist
wget https://raw.github.com/gist/1011318/56a64b96b329215697b78481c6b45463931eae6f/Gemfile -O Gemfile
bundle
rails g rspec:install
# Copy from this gist:
# config/application.rb
# spec/spec_helper.rb
# app/models/user.rb
# app/models/friendship.rb
# app/models/friendship_request.rb
# spec/models/friendship_spec.rb
wget https://raw.github.com/gist/1011318/10930a20546028381020153a7b271e3f4d6bec2f/application.rb -O config/application.rb
wget https://raw.github.com/gist/1011318/4f1a09637d1dd1262f0a450769ccadb1d80784e0 -O spec/spec_helper.rb
wget https://raw.github.com/gist/1011318/5d64a67165fe9919f057341d2605cadc63119afd -O app/models/friendship.rb
wget https://raw.github.com/gist/1011318/406a21d508bf395ab09930329574b0f9ff431a23 -O app/models/friendship_request.rb
wget https://raw.github.com/gist/1011318/13e7e2169b5c85bfa58bc55c77113c11bb89b6e1 -O app/models/user.rb
mkdir spec/models
wget https://raw.github.com/gist/1011318/86cfbdc3f853deed69bbbdc022a9e5fd3c092d07 -O spec/models/friendship_spec.rb
rspec spec/models/friendship_spec.rb
class Friendship
include DataMapper::Resource
belongs_to :user, 'User', :key => true
belongs_to :friend, 'User', :key => true
belongs_to :request, 'FriendshipRequest' #, :child_key => [:sender_id,:receiver_id]
# DataMapper.auto_migrate! result:
# CREATE TABLE "friendships" (
# "user_id" INTEGER NOT NULL,
# "friend_id" INTEGER NOT NULL,
# PRIMARY KEY("user_id", "friend_id")
# )
end
class FriendshipRequest
include DataMapper::Resource
belongs_to :sender, 'User', :key => true
belongs_to :receiver, 'User', :key => true
has 1, :friendship, :child_key => [:user_id,:friend_id]
end
class User
include DataMapper::Resource
property :id, Serial
has n, :friendship_requests,'FriendshipRequest', :child_key => [ :sender_id ]
has n, :friendship_offers, 'FriendshipRequest', :child_key => [ :receiver_id ]
has n, :friendships_requested, 'Friendship', :child_key => [:user_id]
has n, :friendships_offered, 'Friendship', :child_key => [:friend_id]
end
require File.expand_path('../boot', __FILE__)
# Pick the frameworks you want:
#require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "sprockets/railtie"
# require "rails/test_unit/railtie"
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
module Abcdefg
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named.
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
# Activate observers that should always be running.
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters += [:password]
# Enable the asset pipeline
config.assets.enabled = true
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
end
end
source 'http://rubygems.org'
gem 'rails', '3.1.1'
gem 'sqlite3'
DM_VERSION = '~> 1.2.0'
gem 'dm-rails', DM_VERSION
gem 'dm-sqlite-adapter', DM_VERSION
gem 'dm-migrations', DM_VERSION
group :development, :test do
gem "rspec-rails"
gem "rspec"
gem "rspec-core"
gem "rspec-expectations"
gem "rspec-mocks"
end
rspec spec/models/friendship_spec.rb
~ SQL (0.048ms) SELECT sqlite_version(*)
~ SQL (0.347ms) DROP TABLE IF EXISTS "friendships"
~ SQL (0.037ms) PRAGMA table_info("friendships")
~ SQL (31.930ms) CREATE TABLE "friendships" ("user_id" INTEGER NOT NULL, "friend_id" INTEGER NOT NULL, PRIMARY KEY("user_id", "friend_id"))
~ SQL (35.497ms) CREATE INDEX "index_friendships_user" ON "friendships" ("user_id")
~ SQL (4.074ms) CREATE INDEX "index_friendships_friend" ON "friendships" ("friend_id")
~ SQL (0.104ms) DROP TABLE IF EXISTS "friendship_requests"
~ SQL (0.027ms) PRAGMA table_info("friendship_requests")
~ SQL (10.820ms) CREATE TABLE "friendship_requests" ("sender_id" INTEGER NOT NULL, "receiver_id" INTEGER NOT NULL, PRIMARY KEY("sender_id", "receiver_id"))
~ SQL (2.776ms) CREATE INDEX "index_friendship_requests_sender" ON "friendship_requests" ("sender_id")
~ SQL (28.672ms) CREATE INDEX "index_friendship_requests_receiver" ON "friendship_requests" ("receiver_id")
~ SQL (0.107ms) DROP TABLE IF EXISTS "users"
~ SQL (0.028ms) PRAGMA table_info("users")
~ SQL (16.580ms) CREATE TABLE "users" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)
Friendship
~ SQL (26.612ms) INSERT INTO "users" DEFAULT VALUES
~ SQL (3.146ms) INSERT INTO "users" DEFAULT VALUES
~ SQL (25.233ms) INSERT INTO "friendship_requests" ("sender_id", "receiver_id") VALUES (1, 2)
~ SQL (2.940ms) INSERT INTO "friendships" ("user_id", "friend_id") VALUES (1, 2)
should not be nil (FAILED - 1)
Failures:
1) Friendship
Failure/Error: fs.request.should_not be_nil
expected: not nil
got: nil
# ./spec/models/friendship_spec.rb:16:in `block (2 levels) in <top (required)>'
Finished in 5.92 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/models/friendship_spec.rb:7 # Friendship
describe Friendship do
before(:all) do
DataMapper::Model.raise_on_save_failure = true
end
it do
user = User.create
friend = User.create
req = FriendshipRequest.create(:sender => user, :receiver => friend)
fs = Friendship.create(:user => user, :friend => friend, :request => req)
fs.request.should == req
fs.reload
# Since the request-property is not present in the DB, it is set to nil
fs.request.should_not be_nil
end
end
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.before(:suite) do
logger = DataMapper::Logger.new(STDOUT, :debug)
DataMapper.auto_migrate!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment