Instantly share code, notes, and snippets.

Embed
What would you like to do?
Single file Rails application
# Activate the gem you are reporting the issue against.
gem 'activerecord'
require 'active_record'
require 'minitest/autorun'
require 'logger'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
t.string :name
end
create_table :comments, force: true do |t|
t.integer :post_id
t.string :number
end
end
module Blog
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
end
end
class BugTest < Minitest::Test
def test_association_stuff
post = Blog::Post.create(name: "test")
assert_equal nil, post.comments.take
post.comments.to_a
assert_equal nil, post.comments.take
end
end
# the new and improved one-file rails app -- now including
require "action_controller/railtie"
class Tester < Rails::Application
config.session_store :cookie_store, :key => '_rails_session'
config.secret_token = '095f674153982a9ce59914b561f4522a'
end
class UsersController < ActionController::Base
def current
render text: current_user.server_id
end
def create
@u = User.create(params[:user])
@u.create_remotely!
session[:user_id] = @u.id
render text: "good!"
end
end
require 'sqlite3'
db_file = File.join(File.dirname(__FILE__), 'test.sqlite3')
db = SQLite3::Database.new( db_file )
db.execute( "drop table users;" ) rescue nil # maybe it didn't exist!
db.execute( "create table users (id integer primary key autoincrement, email varchar(100), server_id integer);" )
require 'active_record'
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => db_file)
class User < ActiveRecord::Base
include Whoami::Rails::ActiveRecord
end
Tester.routes.draw do
root to: 'users#current'
match '/users/current' => 'users#current'
resources :users
end
require './application'
run Tester
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment