initial commit
This commit is contained in:
commit
83da44aecb
17
.gitignore
vendored
Normal file
17
.gitignore
vendored
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
*.gem
|
||||||
|
*.rbc
|
||||||
|
.bundle
|
||||||
|
.config
|
||||||
|
.yardoc
|
||||||
|
Gemfile.lock
|
||||||
|
InstalledFiles
|
||||||
|
_yardoc
|
||||||
|
coverage
|
||||||
|
doc/
|
||||||
|
lib/bundler/man
|
||||||
|
pkg
|
||||||
|
rdoc
|
||||||
|
spec/reports
|
||||||
|
test/tmp
|
||||||
|
test/version_tmp
|
||||||
|
tmp
|
4
Gemfile
Normal file
4
Gemfile
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
source 'https://rubygems.org'
|
||||||
|
|
||||||
|
# Specify your gem's dependencies in seed-fu-mongoid.gemspec
|
||||||
|
gemspec
|
22
LICENSE.txt
Normal file
22
LICENSE.txt
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
Copyright (c) 2013 John Bintz
|
||||||
|
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
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.
|
21
README.md
Normal file
21
README.md
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
I missed [Seed Fu](https://github.com/mbleigh/seed-fu) when I was working on a Mongoid app, so I hacked this together in about 20 minutes.
|
||||||
|
No tests whatsoever but it works with my very simple Seed Fu-like seed data, so if you like it, contribute some tests!
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
Seed files in `db/fixtures/*.rb`:
|
||||||
|
|
||||||
|
``` ruby
|
||||||
|
PermissionType.seed(id: 1, type: 'Users')
|
||||||
|
PermissionType.seed(id: 2, type: 'Sites')
|
||||||
|
PermissionType.seed(id: 3, type: 'Cats')
|
||||||
|
```
|
||||||
|
|
||||||
|
Then `rake db:seed_fu` if you're in Rails.
|
||||||
|
|
||||||
|
Simple!
|
||||||
|
|
||||||
|
### Capistrano
|
||||||
|
|
||||||
|
`require 'seed-fu-mongoid/capistrano'` to have your data get re-seeded on deploy.
|
||||||
|
|
27
lib/seed-fu-mongoid.rb
Normal file
27
lib/seed-fu-mongoid.rb
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
require "seed-fu-mongoid/version"
|
||||||
|
require "seed-fu-mongoid/document_seeder"
|
||||||
|
require "seed-fu-mongoid/seed_proxy"
|
||||||
|
require 'mongoid/document'
|
||||||
|
|
||||||
|
if defined?(Rails::Railtie)
|
||||||
|
require 'seed-fu-mongoid/railtie'
|
||||||
|
end
|
||||||
|
|
||||||
|
# punch the duck out of Mongoid::Document
|
||||||
|
|
||||||
|
module Mongoid::Document
|
||||||
|
module ClassMethods
|
||||||
|
def seed_once(*constraints)
|
||||||
|
seeder = SeedFuMongoid::DocumentSeeder.new(self, constraints, block)
|
||||||
|
|
||||||
|
if seeder.new?
|
||||||
|
seeder.seed!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def seed(*constraints, &block)
|
||||||
|
SeedFuMongoid::DocumentSeeder.new(self, constraints, block).seed!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
10
lib/seed-fu-mongoid/capistrano.rb
Normal file
10
lib/seed-fu-mongoid/capistrano.rb
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
Capistrano::Configuration.instance.load do
|
||||||
|
namespace :db do
|
||||||
|
desc "Load seed data into Mongoid database"
|
||||||
|
task :seed_fu do
|
||||||
|
cd "#{current_path} && bundle exec rake RAILS_ENV=#{rails_env} db:seed_fu"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
after 'deploy:update_code', 'db:seed_fu'
|
||||||
|
end
|
63
lib/seed-fu-mongoid/document_seeder.rb
Normal file
63
lib/seed-fu-mongoid/document_seeder.rb
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
module SeedFuMongoid
|
||||||
|
class DocumentSeeder
|
||||||
|
class ConstraintNotDefined < StandardError ; end
|
||||||
|
|
||||||
|
attr_reader :constraints
|
||||||
|
|
||||||
|
def initialize(klass, constraints, block)
|
||||||
|
@klass, @constraints, @block = klass, constraints, block
|
||||||
|
|
||||||
|
if @constraints.empty?
|
||||||
|
@constraints = [ :id ]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def proxy
|
||||||
|
return @proxy if @proxy
|
||||||
|
|
||||||
|
@proxy = SeedProxy.new
|
||||||
|
|
||||||
|
@block.call(@proxy)
|
||||||
|
|
||||||
|
@proxy
|
||||||
|
end
|
||||||
|
|
||||||
|
def document
|
||||||
|
@doc ||= begin
|
||||||
|
@klass.find_by(constraint_search)
|
||||||
|
rescue => e
|
||||||
|
@klass.new
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def constraint_search
|
||||||
|
return @constraint_search if @constraint_search
|
||||||
|
|
||||||
|
@constraint_search = {}
|
||||||
|
constraints.each do |constraint|
|
||||||
|
if proxy[constraint]
|
||||||
|
@constraint_search[constraint] = proxy[constraint]
|
||||||
|
else
|
||||||
|
raise ConstraintNotDefined.new(constraint)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@constraint_search
|
||||||
|
end
|
||||||
|
|
||||||
|
def seed!
|
||||||
|
proxy.each do |key, value|
|
||||||
|
document[key] = value
|
||||||
|
end
|
||||||
|
|
||||||
|
puts "#{@klass.name} #{document.attributes}"
|
||||||
|
|
||||||
|
document.upsert
|
||||||
|
end
|
||||||
|
|
||||||
|
def new?
|
||||||
|
!document.persisted?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
17
lib/seed-fu-mongoid/railtie.rb
Normal file
17
lib/seed-fu-mongoid/railtie.rb
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
module SeedFuMongoid
|
||||||
|
class Railtie < ::Rails::Railtie
|
||||||
|
rake_tasks do
|
||||||
|
namespace :db do
|
||||||
|
desc "Load seed data from db/fixtures"
|
||||||
|
task :seed_fu do
|
||||||
|
Dir['db/fixtures/**/*.rb'].each do |file|
|
||||||
|
puts "== Seed from #{file}"
|
||||||
|
|
||||||
|
load file
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
26
lib/seed-fu-mongoid/seed_proxy.rb
Normal file
26
lib/seed-fu-mongoid/seed_proxy.rb
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
module SeedFuMongoid
|
||||||
|
class SeedProxy
|
||||||
|
attr_reader :data
|
||||||
|
|
||||||
|
def initialize
|
||||||
|
@data = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
def method_missing(method, value, *args)
|
||||||
|
if method.to_s[-1..-1] == '='
|
||||||
|
@data[method.to_s[0..-2].to_sym] = value
|
||||||
|
else
|
||||||
|
super
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def [](key)
|
||||||
|
@data[key]
|
||||||
|
end
|
||||||
|
|
||||||
|
def each(&block)
|
||||||
|
@data.each(&block)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
3
lib/seed-fu-mongoid/version.rb
Normal file
3
lib/seed-fu-mongoid/version.rb
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
module SeedFuMongoid
|
||||||
|
VERSION = "0.0.1"
|
||||||
|
end
|
21
seed-fu-mongoid.gemspec
Normal file
21
seed-fu-mongoid.gemspec
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
lib = File.expand_path('../lib', __FILE__)
|
||||||
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
||||||
|
require 'seed-fu-mongoid/version'
|
||||||
|
|
||||||
|
Gem::Specification.new do |gem|
|
||||||
|
gem.name = "seed-fu-mongoid"
|
||||||
|
gem.version = SeedFuMongoid::VERSION
|
||||||
|
gem.authors = ["John Bintz"]
|
||||||
|
gem.email = ["john@coswellproductions.com"]
|
||||||
|
gem.description = %q{Seed Fu-like functionality for Mongoid}
|
||||||
|
gem.summary = %q{Seed Fu-like functionality for Mongoid}
|
||||||
|
gem.homepage = ""
|
||||||
|
|
||||||
|
gem.files = `git ls-files`.split($/)
|
||||||
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
||||||
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
||||||
|
gem.require_paths = ["lib"]
|
||||||
|
|
||||||
|
gem.add_dependency 'mongoid'
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user