Permalink
Browse files

Added active record and entry model to teaser engine.

That includes:
- adding active record to the engine
- using engine's migrations in the main app
- add a bit of support to migrate the main app and the engine with one command
  • Loading branch information...
1 parent cb6bf07 commit 3a103eea0b032b8dde5554a3e18a871c6bfb3a03 Stephan Hagemann committed Sep 16, 2012
View
@@ -7,4 +7,5 @@ gem "teaser", path: "engines/teaser"
group :test, :development do
gem 'rspec-rails'
gem 'capybara'
+ gem 'sqlite3'
end
View
@@ -124,6 +124,7 @@ GEM
hike (~> 1.2)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
+ sqlite3 (1.3.6)
thor (0.16.0)
tilt (1.3.3)
treetop (1.4.10)
@@ -140,4 +141,5 @@ DEPENDENCIES
capybara
rails
rspec-rails
+ sqlite3
teaser!
View
@@ -1,3 +1,11 @@
#!/usr/bin/env rake
require File.expand_path('../config/application', __FILE__)
TheNextBigThing::Application.load_tasks
+
+require "rspec/core/rake_task"
+
+RSpec::Core::RakeTask.new(:rspec) do |spec|
+ spec.pattern = 'spec/**/*_spec.rb'
+end
+
+task :default => :rspec
View
@@ -1,11 +1,11 @@
echo "*** Running container app specs"
source ~/.rvm/scripts/rvm
-bundle exec rake
+bundle exec rake db:migrate db:test:prepare rspec
result=$?
echo "*** Running teaser engine specs"
cd engines/teaser
-bundle exec rake
+bundle exec rake db:migrate app:db:test:prepare rspec
result+=$?
exit $result
View
@@ -17,6 +17,9 @@
module TheNextBigThing
class Application < Rails::Application
+
+ config.paths['db/migrate'] += Teaser::Engine.paths['db/migrate']
+
# 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.
View
@@ -0,0 +1,11 @@
+development:
+ adapter: sqlite3
+ database: db/development.sqlite3
+ pool: 5
+ timeout: 5000
+
+test:
+ adapter: sqlite3
+ database: db/test.sqlite3
+ pool: 5
+ timeout: 5000
View
@@ -0,0 +1,22 @@
+# encoding: UTF-8
+# This file is auto-generated from the current state of the database. Instead
+# of editing this file, please use the migrations feature of Active Record to
+# incrementally modify your database, and then regenerate this schema definition.
+#
+# Note that this schema.rb definition is the authoritative source for your
+# database schema. If you need to create the application database on another
+# system, you should be using db:schema:load, not running all the migrations
+# from scratch. The latter is a flawed and unsustainable approach (the more migrations
+# you'll amass, the slower it'll run and the greater likelihood for issues).
+#
+# It's strongly recommended to check this file into your version control system.
+
+ActiveRecord::Schema.define(:version => 20120915205848) do
+
+ create_table "teaser_entries", :force => true do |t|
+ t.string "email"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+end
View
@@ -3,4 +3,7 @@ gemspec
gem "jquery-rails"
-gem "rspec-rails"
+gem "rspec-rails"
+gem "shoulda-matchers"
+
+gem "sqlite3"
@@ -103,10 +103,13 @@ GEM
railties (~> 3.2.0)
sass (>= 3.1.10)
tilt (~> 1.3)
+ shoulda-matchers (1.3.0)
+ activesupport (>= 3.0.0)
sprockets (2.1.3)
hike (~> 1.2)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
+ sqlite3 (1.3.6)
thor (0.16.0)
tilt (1.3.3)
treetop (1.4.10)
@@ -120,4 +123,6 @@ PLATFORMS
DEPENDENCIES
jquery-rails
rspec-rails
+ shoulda-matchers
+ sqlite3
teaser!
View
@@ -20,6 +20,8 @@ RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_files.include('lib/**/*.rb')
end
+APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
+load 'rails/tasks/engine.rake'
@@ -31,4 +33,4 @@ RSpec::Core::RakeTask.new(:rspec) do |spec|
spec.pattern = 'spec/**/*_spec.rb'
end
-task :default => :rspec
+task :default => :rspec
@@ -0,0 +1,5 @@
+module Teaser
+ class Entry < ActiveRecord::Base
+ validates :email, presence: true
+ end
+end
@@ -0,0 +1,9 @@
+class CreateEntry < ActiveRecord::Migration
+ def change
+ create_table :teaser_entries do |t|
+ t.string :email
+
+ t.timestamps
+ end
+ end
+end
@@ -0,0 +1,10 @@
+module Rails
+ class Engine < Railtie
+ def load_seed
+ seed_files = paths["db/seeds"]
+ seed_files.each do |seed_file|
+ load(seed_file)
+ end
+ end
+ end
+end
@@ -1,5 +1,8 @@
require "teaser/engine"
+require "rails/all"
require "haml"
+require File.expand_path("../monkey_patches/engine.rb", __FILE__)
+
module Teaser
end
@@ -1,5 +1,7 @@
module Teaser
class Engine < ::Rails::Engine
isolate_namespace Teaser
+
+ config.paths['db/migrate'] << File.join(Teaser::Engine.paths['db/migrate'].first)
end
end
@@ -1,12 +1,6 @@
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"
+require "rails/all"
Bundler.require
require "teaser"
@@ -53,7 +47,7 @@ class Application < Rails::Application
# This will create an empty whitelist of attributes available for mass-assignment for all models
# in your app. As such, your models will need to explicitly whitelist or blacklist accessible
# parameters by using an attr_accessible or attr_protected declaration.
- #config.active_record.whitelist_attributes = true
+ config.active_record.whitelist_attributes = true
# Enable the asset pipeline
config.assets.enabled = true
@@ -1,15 +1,11 @@
development:
- adapter: postgresql
- encoding: unicode
- database: teaser_development
+ adapter: sqlite3
+ database: db/development.sqlite3
pool: 5
- username: stephan.hagemann
- password:
+ timeout: 5000
test:
- adapter: postgresql
- encoding: unicode
- database: teaser_test
+ adapter: sqlite3
+ database: db/test.sqlite3
pool: 5
- username: stephan.hagemann
- password:
+ timeout: 5000
@@ -30,7 +30,7 @@
config.action_mailer.delivery_method = :test
# Raise exception on mass assignment protection for Active Record models
- #config.active_record.mass_assignment_sanitizer = :strict
+ config.active_record.mass_assignment_sanitizer = :strict
# Print deprecation notices to the stderr
config.active_support.deprecation = :stderr
@@ -1,4 +1,3 @@
Rails.application.routes.draw do
-
mount Teaser::Engine => "/teaser"
end
@@ -11,6 +11,12 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 0) do
+ActiveRecord::Schema.define(:version => 20120915205848) do
+
+ create_table "teaser_entries", :force => true do |t|
+ t.string "email"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
end
Binary file not shown.
No changes.
@@ -0,0 +1,7 @@
+require "spec_helper"
+
+module Teaser
+ describe Teaser::Entry do
+ it { should validate_presence_of :email }
+ end
+end
@@ -20,4 +20,5 @@ Gem::Specification.new do |s|
s.add_dependency "haml-rails"
s.add_dependency "sass-rails"
+ s.add_development_dependency "sqlite3"
end
View
@@ -0,0 +1,10 @@
+#!/bin/bash
+
+# IMPORTANT: Source RVM as a function into local environment.
+# Otherwise switching gemsets won't work.
+[ -s "$HOME/.rvm/scripts/rvm" ] && . "$HOME/.rvm/scripts/rvm"
+
+echo "migrating teaser"
+cd engines/teaser && rake db:migrate app:db:test:prepare && cd ../..
+echo "migrating wrapper rails app"
+rake db:migrate db:test:prepare

0 comments on commit 3a103ee

Please sign in to comment.