Last active
August 29, 2015 14:13
-
-
Save williamhqs/c127e5d7018aa61cb02a to your computer and use it in GitHub Desktop.
Sinatra with activerecord transaction warnning.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreateStudent < ActiveRecord::Migration | |
def change | |
create_table :users do |t| | |
t.string :name | |
t.integer :age | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "./myApp" | |
run MyApp.new |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "sinatra/activerecord" | |
set :database, { | |
:adapter => 'mysql2', | |
:encoding => 'utf8mb4', | |
:reconnect => true, | |
:database => 'test_development', | |
:pool => 5, | |
:username => 'root', | |
:password => '123123', | |
:host => 'localhost', | |
:socket => '/tmp/mysql.sock', | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Myapp | |
--------------------- | |
Gemfile | |
Rakefile | |
config.ru | |
db | |
----migrate | |
--------20150110033835_create_student.rb | |
----schema.rb | |
myApp.rb | |
Gemfile.lock | |
app | |
----controllers | |
----models | |
----student.rb | |
----views | |
database.rb | |
rspec | |
----myApp_spec.rb | |
----spec_helper.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'rake' | |
gem 'sinatra' | |
gem 'activerecord', '4.2', :require => 'active_record' | |
gem 'mysql2' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ROOT = File.expand_path('./..', __FILE__) unless defined?(ROOT) | |
require 'sinatra' | |
require 'active_record' | |
require './database' | |
['./app/controllers', './app/models', './app/views'].each do |path| | |
Dir.glob("#{ROOT}/#{path}/*.rb") do |filename| | |
require filename | |
end | |
end | |
class MyApp < Sinatra::Base | |
get '/' do | |
'Hello World!' | |
end | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require './spec_helper' | |
describe "Main API" do | |
it "should respond to GET" do | |
get '/' | |
expect(response.status).to eq 200 | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require File.join(File.dirname(__FILE__), '..', 'myApp.rb') | |
require 'sinatra' | |
require 'rack/test' | |
set :environment, :test | |
set :run, false | |
set :raise_errors, true | |
set :logging, false | |
def app | |
Sinatra::Application | |
end | |
RSpec.configure do |config| | |
config.include Rack::Test::Methods | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#after_commit with case the issue | |
class Student < ActiveRecord::Base | |
after_commit :print_hello | |
def print_hello | |
p "hello world" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment