Sha256: cdf1631c5b62f14913af21fa827b6a27d32e8b9cd9be51467212b1436d533f5b

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 KB

Contents

require 'yaml'

namespace :blogit do
  
  desc "Add a bunch of mock blog posts to the database for dev"
  task :seed => :environment do
    if Rails.env.production?
      warn "Can't seed your production database bro, too risky!"
    else
      load_all_models
      seed_blog_content.each do |blog_post_atts|
        state = Blogit.configuration.active_states.first
        begin
          blogger.first.blog_posts.create!(blog_post_atts.merge(state: state))
        rescue NoMethodError
          warn "Couldn't find a blogger model record :("
        end
      end      
    end
  end
  
  
  private
  
  
  def seed_blog_content
    YAML.load(File.read(File.join(File.dirname(__FILE__), "seed_blog_posts.yml")))
  end
  
  
  def load_all_models
    Dir[Rails.root.join("app/models/**/*.rb")].each { |file| load file }
  end
  
  # The fellow who's going to write these posts...
  def blogger
    ActiveRecord::Base.subclasses.detect(&:blogs?) ||
      warn("Can't seed your database, couldn't find a model that #blogs?")
  end
  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
blogit-1.1.2 lib/tasks/blogit.rake
blogit-1.1.1 lib/tasks/blogit.rake
blogit-1.1.0 lib/tasks/blogit.rake