Sha256: 83f58907c6a9f3e362c0ca2ffa7935f97f7a3b3e4e9b804aeceb8c62196ee220

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

module Slices
  module Tasks
    def self.create_indexes
      pattern = Rails.root.join('app/slices/**/*.rb')
      Dir.glob(pattern).each do |file|
        model = determine_model(file)
        if model
          model.create_indexes
          Logger.new($stdout).info("Generated indexes for #{model}")
        end
      end
    end

    def self.determine_model(file)
      segments = file[0 .. -4].split('/')
      start = if segments.include?('slices')
                segments.rindex('slices') + 2
              else
                segments.rindex('models') + 1
              end
      model_path = segments[start .. -1]
      klass = model_path.map { |path| path.camelize }.join('::').constantize
      if klass.ancestors.include?(::Mongoid::Document) && !klass.embedded
        return klass
      end
    end
  end
end

namespace :slices do
  namespace :db do
    desc "Create indexes for slices models"
    task create_indexes: "db:mongoid:create_indexes" do
      Slices::Tasks.create_indexes
    end
  end

  namespace :migrate do
    desc "Rename description field to meta_description"
    task meta_description: :environment do
      Page.collection.update({},
        {'$rename' => {'description' => 'meta_description'}},
        multi: true)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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