Skip to content

Instantly share code, notes, and snippets.

@ma2shita
Created December 1, 2014 09:24
Show Gist options
  • Save ma2shita/5d9ca9b3d4bd5e2d044d to your computer and use it in GitHub Desktop.
Save ma2shita/5d9ca9b3d4bd5e2d044d to your computer and use it in GitHub Desktop.
Extend Sinatra-App-Skelton for using DB(PostgreSQL)
diff --git Gemfile Gemfile
index a531c7c..2932ed2 100644
--- Gemfile
+++ Gemfile
@@ -4,6 +4,11 @@ gem "sinatra"
gem "grape"
gem "slim"
gem "sass"
+gem "activerecord"
+gem "sinatra-activerecord"
+group :production do
+ gem "pg"
+end
gem "rake"
@@ -21,6 +26,8 @@ group :development, :test do
gem "thin"
# for live reload
gem "guard-livereload" # install live-reload ext. for your browser.
+ # for Database
+ gem "database_cleaner"
end
group :test do
diff --git Rakefile Rakefile
index 36d8542..3191b0e 100644
--- Rakefile
+++ Rakefile
@@ -1,5 +1,6 @@
require "rspec/core"
require "rspec/core/rake_task"
+require "sinatra/activerecord/rake"
require_relative "init"
task :default => :spec
diff --git config.ru config.ru
index 5b13419..f341492 100644
--- config.ru
+++ config.ru
@@ -1,3 +1,4 @@
require_relative "init"
+use ActiveRecord::ConnectionAdapters::ConnectionManagement
run Rack::Cascade.new [App::Web, App::API]
diff --git config/database.yml config/database.yml
new file mode 100644
index 0000000..b5d3cea
--- /dev/null
+++ config/database.yml
@@ -0,0 +1,14 @@
+development:
+ adapter: postgresql
+ host: 127.0.0.1
+ username: postgres
+ password: postgres
+ database: development_db
+
+test:
+ adapter: postgresql
+ host: 127.0.0.1
+ username: postgres
+ password: postgres
+ database: test_db
+
diff --git db/migrate/20141201083350_create_examples.rb db/migrate/20141201083350_create_examples.rb
new file mode 100644
index 0000000..4b1199e
--- /dev/null
+++ db/migrate/20141201083350_create_examples.rb
@@ -0,0 +1,8 @@
+class CreateExamples < ActiveRecord::Migration
+ def change
+ create_table :examples do |t|
+ t.string :title
+ t.text :body
+ end
+ end
+end
diff --git init.rb init.rb
index 1a45b28..f8c7a66 100644
--- init.rb
+++ init.rb
@@ -4,6 +4,10 @@ require "sinatra/base"
require "grape"
require "slim"
require "sass"
+require "active_record"
+require "sinatra/activerecord"
+require "yaml"
+require "erb"
if Sinatra::Base.development? || Sinatra::Base.test?
require "pry-byebug"
require "pry-rescue"
@@ -13,10 +17,14 @@ if Sinatra::Base.development? || Sinatra::Base.test?
require "timecop"
end
+ActiveRecord::Base.configurations = YAML.load(ERB.new(File.read(File.join(File.expand_path(File.dirname(__FILE__)), "config", "database.yml"))).result).stringify_keys || {}
+ActiveRecord::Base.establish_connection(Sinatra::Base.environment)
+
Dir.chdir(File.expand_path(File.dirname(__FILE__))) do
Dir.glob(File.join("models", "**", "*.rb")) { |rb| require_relative rb }
Dir.glob(File.join("lib", "**", "*.rb")) { |rb| require_relative rb }
end
+
require_relative "web"
require_relative "api"
diff --git models/examples.rb models/examples.rb
new file mode 100644
index 0000000..1842c27
--- /dev/null
+++ models/examples.rb
@@ -0,0 +1,3 @@
+class Example < ActiveRecord::Base
+end
+
diff --git spec/examples_spec.rb spec/examples_spec.rb
new file mode 100644
index 0000000..ec69050
--- /dev/null
+++ spec/examples_spec.rb
@@ -0,0 +1,10 @@
+require "spec_helper"
+
+describe Example do
+ before {
+ Example.create :title => "hoge", :body => "日本語"
+ }
+ subject { Example.where(:title => "hoge") }
+ its(:count) { is_expected.to eq 1 }
+end
+
diff --git spec/spec_helper.rb spec/spec_helper.rb
index 8d5ede3..c24d344 100644
--- spec/spec_helper.rb
+++ spec/spec_helper.rb
@@ -5,6 +5,7 @@ require "json"
require "i18n"
I18n.enforce_available_locales = false
require "rspec-html-matchers"
+require "database_cleaner"
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
@@ -14,6 +15,15 @@ RSpec.configure do |config|
mocks.verify_partial_doubles = true
end
config.include RSpec::JsonMatcher
+ config.before(:suite) do
+ DatabaseCleaner.strategy = :transaction
+ DatabaseCleaner.clean_with :transaction
+ end
+ config.around(:each) do |example|
+ DatabaseCleaner.cleaning do
+ example.run
+ end
+ end
end
require_relative File.join("..", "init")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment