Sha256: abf75f34433079fb66a28ff0830460453a470889e1b61c8adceaac3f75311016

Contents?: true

Size: 1.89 KB

Versions: 3

Compression:

Stored size: 1.89 KB

Contents

require 'highline/import'

module Slices
  module Tasks
    class Exists < RuntimeError; end

    def self.with_message(text)
      print "#{text} ... "
      yield
      puts "ok"
    rescue Exists
      puts "(exists)"
    end

    def self.make_virtual(role, name, status)
      with_message("Creating #{status} page") do
        if Page.where(role: role).count > 0
          raise Exists
        end
        Page.make(role: role, name: name, active: true)
      end
    end

    def self.make_home
      with_message("Creating home page") do
        raise Exists if Page.where(path: '/').any?
        Page.make(name:         'Home',
                  permalink:    '',
                  show_in_nav:  true,
                  active:       true)
      end
    end

    def self.make_errors
      make_virtual('not_found', 'Page not found', 404)
      make_virtual('error', 'Whoops', 500)
    end

    def self.make_admin
      if Admin.exists?
        say "Creating admin user ... (exists)"
        return
      end

      say("Creating admin user ...")

      admin = Admin.create!(
        name: ask('Enter a name: '),
        email: ask('Enter an email address: '),
        password: ask('Enter a password: ') { |q| q.echo = false }
      )
      admin.super_user = true
      admin.save!

      say("... ok")
    end

    def self.make_all
      make_home
      make_errors
      make_admin
    end
  end
end

namespace :slices do
  namespace :seed do
    task :foo do
      puts "Bar"
    end

    desc "Create the home page"
    task :home => :environment do
      Slices::Tasks.make_home
    end

    desc "Create 404 and 500 pages"
    task :errors => :home do
      Slices::Tasks.make_errors
    end

    desc "Create admin user"
    task :admin => :environment do
      Slices::Tasks.make_admin
    end
  end

  desc "Seed home, errors and admin user"
  task :seed => :environment do
    Slices::Tasks.make_all
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
slices-2.0.2 lib/tasks/seeds.rake
slices-2.0.1 lib/tasks/seeds.rake
slices-2.0.0 lib/tasks/seeds.rake