Permalink
Browse files

Converting to plugin, adding code.

  • Loading branch information...
1 parent f2e151b commit 4d040be1bd3acd98cd5cb766ac28f6cf78633dd5 @ffmike committed Sep 16, 2008
Showing with 171 additions and 2 deletions.
  1. +20 −0 MIT-LICENSE
  2. +1 −0 README
  3. +22 −0 Rakefile
  4. +28 −0 db_populate.gemspec
  5. +1 −0 init.rb
  6. +1 −0 install.rb
  7. +18 −0 lib/create_or_update.rb
  8. +1 −0 rails/init.rb
  9. +4 −0 tasks/db_populate_tasks.rake
  10. +7 −2 tasks/populate.rake
  11. +18 −0 test/database.yml
  12. +8 −0 test/db_populate_test.rb
  13. +7 −0 test/schema.rb
  14. +34 −0 test/test_helper.rb
  15. +1 −0 uninstall.rb
View
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2008 [name of plugin creator]
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
1 README
@@ -0,0 +1 @@
+TBD
View
22 Rakefile
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the db_populate plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the db_populate plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'DbPopulate'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
View
28 db_populate.gemspec
@@ -0,0 +1,28 @@
+Gem::Specification.new do |s|
+ s.name = "db_populate"
+ s.version = "0.1.0"
+ s.date = "2008-09-15"
+ s.summary = "Seed data populator for Rails"
+ s.email = "MikeG1@larkfarm.com"
+ s.homepage = "http://github.com/ffmike/db_populate"
+ s.description = "db_populate provides rake and code support for adding seed data to Rails projects. Forked from a rake task by
+ Josh Knowles, plus code by Luke Franci."
+ s.has_rdoc = false
+ s.authors = ["Mike Gunderloy", "Josh Knowles", "Luke Franci"]
+ s.files = [
+ "MIT-LICENSE",
+ "README",
+ "Rakefile",
+ "init.rb",
+ "install.rb",
+ "lib/create_or_update.rb",
+ "rails/init.rb"
+ "tasks/populate.rake",
+ "tasks/db_populate_tasks.rake"
+ "test/database.yml",
+ "test/schema.rb",
+ "test/test_helper.rb",
+ "test/db_populate_test.rb",
+ "uninstall.rb"
+ ]
+end
View
1 init.rb
@@ -0,0 +1 @@
+require File.dirname(__FILE__) + "/rails/init"
View
1 install.rb
@@ -0,0 +1 @@
+# Install hook code here
View
18 lib/create_or_update.rb
@@ -0,0 +1,18 @@
+class ActiveRecord::Base
+ # given a hash of attributes including the ID, look up the record by ID.
+ # If it does not exist, it is created with the rest of the options.
+ # If it exists, it is updated with the given options.
+ #
+ # Raises an exception if the record is invalid to ensure seed data is loaded correctly.
+ #
+ # Returns the record.
+ def self.create_or_update(options = {})
+ id = options.delete(:id)
+ record = find_by_id(id) || new
+ record.id = id
+ record.attributes = options
+ record.save!
+
+ record
+ end
+end
View
1 rails/init.rb
@@ -0,0 +1 @@
+require "create_or_update"
View
4 tasks/db_populate_tasks.rake
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :user_event_logger do
+# # Task goes here
+# end
View
9 tasks/populate.rake
@@ -1,7 +1,8 @@
-amespace :db do
+namespace :db do
+
desc "Loads initial database models for the current environment."
task :populate => :environment do
- require File.join(File.dirname(__FILE__), '/../../lib', 'create_or_update')
+ require File.join(File.dirname(__FILE__), '/../lib', 'create_or_update')
Dir[File.join(RAILS_ROOT, 'db', 'populate', '*.rb')].sort.each do |fixture|
load fixture
puts "Loaded #{fixture}"
@@ -11,4 +12,8 @@ amespace :db do
puts "Loaded #{fixture}"
end
end
+
+ desc "Runs migrations and then loads seed data"
+ task :migrate_and_load => [ 'db:migrate', 'db:populate' ]
+
end
View
18 test/database.yml
@@ -0,0 +1,18 @@
+sqlite:
+ :adapter: sqlite
+ :dbfile: db_populate_plugin.sqlite.db
+sqlite3:
+ :adapter: sqlite3
+ :dbfile: db_populate_plugin.sqlite3.db
+postgresql:
+ :adapter: postgresql
+ :username: postgres
+ :password: postgres
+ :database: db_populate_plugin_test
+ :min_messages: ERROR
+mysql:
+ :adapter: mysql
+ :host: localhost
+ :username: rails
+ :password:
+ :database: db_populate_plugin_test
View
8 test/db_populate_test.rb
@@ -0,0 +1,8 @@
+require File.dirname(__FILE__) + '/test_helper.rb'
+require 'test/unit'
+require 'rubygems'
+require 'mocha'
+
+class UserEventLoggerTest < Test::Unit::TestCase
+end
+
View
7 test/schema.rb
@@ -0,0 +1,7 @@
+ActiveRecord::Schema.define(:version => 0) do
+
+ create_table "users", :force => true do |t|
+ t.string "name"
+ end
+
+end
View
34 test/test_helper.rb
@@ -0,0 +1,34 @@
+ENV['RAILS_ENV'] = 'test'
+ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
+
+require 'test/unit'
+require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
+
+config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
+ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
+
+db_adapter = ENV['DB']
+
+# no db passed, try one of these fine config-free DBs before bombing.
+db_adapter ||= begin
+ require 'rubygems'
+ require 'sqlite'
+ 'sqlite'
+ rescue MissingSourceFile
+ begin
+ require 'sqlite3'
+ 'sqlite3'
+ rescue MissingSourceFile
+ end
+end
+
+if db_adapter.nil?
+ raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
+end
+
+ActiveRecord::Base.establish_connection(config[db_adapter])
+
+load(File.dirname(__FILE__) + "/schema.rb")
+
+require File.dirname(__FILE__) + '/../init.rb'
+
View
1 uninstall.rb
@@ -0,0 +1 @@
+# Uninstall hook code here

0 comments on commit 4d040be

Please sign in to comment.