Converting to plugin, adding code.

This commit is contained in:
Mike Gunderloy 2008-09-15 20:08:50 -05:00
parent f2e151bf52
commit 4d040be1bd
15 changed files with 171 additions and 2 deletions

20
MIT-LICENSE Normal file
View File

@ -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.

1
README Normal file
View File

@ -0,0 +1 @@
TBD

22
Rakefile Normal file
View File

@ -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

28
db_populate.gemspec Normal file
View File

@ -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

1
init.rb Normal file
View File

@ -0,0 +1 @@
require File.dirname(__FILE__) + "/rails/init"

1
install.rb Normal file
View File

@ -0,0 +1 @@
# Install hook code here

18
lib/create_or_update.rb Normal file
View File

@ -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

1
rails/init.rb Normal file
View File

@ -0,0 +1 @@
require "create_or_update"

View File

@ -0,0 +1,4 @@
# desc "Explaining what the task does"
# task :user_event_logger do
# # Task goes here
# end

View File

@ -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

18
test/database.yml Normal file
View File

@ -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

8
test/db_populate_test.rb Normal file
View File

@ -0,0 +1,8 @@
require File.dirname(__FILE__) + '/test_helper.rb'
require 'test/unit'
require 'rubygems'
require 'mocha'
class UserEventLoggerTest < Test::Unit::TestCase
end

7
test/schema.rb Normal file
View File

@ -0,0 +1,7 @@
ActiveRecord::Schema.define(:version => 0) do
create_table "users", :force => true do |t|
t.string "name"
end
end

34
test/test_helper.rb Normal file
View File

@ -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'

1
uninstall.rb Normal file
View File

@ -0,0 +1 @@
# Uninstall hook code here