forked from mongoid/mongoid-slug
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refs mongoid#177 added rake task to set slug for legacy data and test…
… cases
- Loading branch information
Anuja Joshi
committed
Nov 3, 2015
1 parent
59fced3
commit adb26ba
Showing
8 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Mongoid::Slug | ||
class Railtie < Rails::Railtie | ||
rake_tasks do | ||
Dir[File.join(File.dirname(__FILE__), '../../tasks/*.rake')].each { |f| load f } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace :mongoid_slug do | ||
|
||
desc 'Goes though all documents and sets slug if not already set' | ||
task :set => :environment do |t, args| | ||
::Rails.application.eager_load! if defined?(Rails) | ||
|
||
unless Mongoid::Slug.classes.blank? | ||
models = args.extras | ||
klasses = Mongoid::Slug.classes | ||
klasses = (klasses.map(&:to_s) & models.map(&:classify)).map(&:constantize) unless models.blank? | ||
klasses.each do |klass| | ||
# set slug for objects having blank slug | ||
klass.each { |object| object.set_slug! unless object.slugs? } | ||
end | ||
end | ||
p "Completed mongoid_slug:set" | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class UninitalizedSlugFirst | ||
include Mongoid::Document | ||
field :_id, type: Integer | ||
field :name, type: String | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class UninitalizedSlugSecond | ||
include Mongoid::Document | ||
field :_id, type: Integer | ||
field :name, type: String | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class UninitalizedSlugThird | ||
include Mongoid::Document | ||
field :_id, type: Integer | ||
field :name, type: String | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require 'spec_helper' | ||
require 'rake' | ||
|
||
describe "mongoid_slug:set" do | ||
before :all do | ||
load File.expand_path("../../../lib/tasks/mongoid_slug.rake", __FILE__) | ||
Rake::Task.define_task(:environment) | ||
end | ||
|
||
context 'when models parameter is passed' do | ||
let!(:uninitalized_slug1) { UninitalizedSlugFirst.create(id: 455, name: 'uninitalized_slug1') } | ||
let!(:uninitalized_slug2) { UninitalizedSlugSecond.create(id: 456, name: 'uninitalized_slug2') } | ||
|
||
it 'Goes though all documents of passed models and sets slug if not already set' do | ||
|
||
class UninitalizedSlugFirst | ||
include Mongoid::Slug | ||
slug :name | ||
end | ||
|
||
class UninitalizedSlugSecond | ||
include Mongoid::Slug | ||
slug :name | ||
end | ||
|
||
expect(uninitalized_slug1.slugs).to be_nil | ||
expect(uninitalized_slug2.slugs).to be_nil | ||
|
||
Rake::Task['mongoid_slug:set'].reenable | ||
Rake::Task['mongoid_slug:set'].invoke('UninitalizedSlugFirst') | ||
|
||
expect(uninitalized_slug1.reload.slugs).to eq(['uninitalized-slug1']) | ||
expect(uninitalized_slug2.reload.slugs).to eq [] | ||
end | ||
end | ||
|
||
context 'when models parameter is not passed' do | ||
let!(:uninitalized_slug3) { UninitalizedSlugThird.create(id: 70, name: 'uninitalized_slug3') } | ||
|
||
it 'Goes though all documents and sets slug if not already set' do | ||
|
||
class UninitalizedSlugThird | ||
include Mongoid::Slug | ||
slug :name | ||
end | ||
expect(uninitalized_slug3.slugs).to be_nil | ||
|
||
Rake::Task['mongoid_slug:set'].reenable | ||
Rake::Task['mongoid_slug:set'].invoke | ||
|
||
expect(uninitalized_slug3.reload.slugs).to eq(['uninitalized-slug3']) | ||
end | ||
end | ||
end |