Permalink
Browse files

GH#3 meme merge from the console

* rake nag:meme:merge from="from name" to="to name"
  • Loading branch information...
1 parent 3bd9ecd commit 88f7e11ad34215d0f2f75bd66d473be108df994d @tardate committed Jan 2, 2012
Showing with 68 additions and 0 deletions.
  1. +11 −0 app/models/meme.rb
  2. +12 −0 lib/tasks/nag.rake
  3. +45 −0 spec/models/meme/merge_spec.rb
View
@@ -50,6 +50,17 @@ def non_trending_arel
where(m[:trending].eq(false))
end
+ # Merge +from_meme_name+ to +to_meme_name+
+ def merge(from_meme_name,to_meme_name)
+ to_meme = Meme.factory(to_meme_name)
+ from_meme = Meme.find_by_name(from_meme_name)
+ return unless to_meme && from_meme
+ # move existing notes to the new meme
+ from_meme.notes.update_all(:meme_id => to_meme.id)
+ # remove the old meme
+ from_meme.destroy
+ end
+
# Tries to coalesce different spellings to one standard form
def normalize_name(value)
case value
View
@@ -11,4 +11,16 @@ namespace :nag do
control.load_show(options[:show_number],options[:reload])
puts "nag:load: Done."
end
+
+ namespace :meme do
+ desc "Merge a meme (from=name to=name)"
+ task :merge => :environment do |t|
+ from_meme = ENV['from'].present? ? ENV['from'] : nil
+ to_meme = ENV['to'].present? ? ENV['to'] : nil
+
+ puts "nag:meme:merge: Processing task..."
+ Meme.merge(from_meme,to_meme)
+ puts "nag:meme:merge: Done."
+ end
+ end
end
@@ -0,0 +1,45 @@
+require 'spec_helper'
+
+describe Meme do
+ let!(:show) { Factory(:show) }
+ let!(:from_meme) { Factory(:meme) }
+ let!(:from_meme_name) { from_meme.name }
+ let!(:to_meme) { Factory(:meme) }
+ let!(:to_meme_name) { to_meme.name }
+ let!(:from_note_a) { Factory(:note, :meme => from_meme, :show => show) }
+ let!(:from_note_b) { Factory(:note, :meme => from_meme, :show => show) }
+ let!(:from_note_c) { Factory(:note, :meme => from_meme, :show => show) }
+ let!(:to_note_a) { Factory(:note, :meme => to_meme, :show => show) }
+
+ describe "##merge" do
+ context "with valid transfer" do
+ before { Meme.merge(from_meme_name,to_meme_name) }
+ describe "from_meme" do
+ subject { Meme.find_by_name(from_meme_name) }
+ it { should be_nil }
+ end
+ describe "notes" do
+ subject { to_meme.notes }
+ its(:count) { should eql(4) }
+ end
+ end
+ context "with new meme" do
+ let(:new_meme_name) { 'new meme name' }
+ let(:new_meme) { Meme.find_by_name(new_meme_name) }
+ before { Meme.merge(from_meme_name,new_meme_name) }
+ describe "from_meme" do
+ subject { Meme.find_by_name(from_meme_name) }
+ it { should be_nil }
+ end
+ describe "to_meme" do
+ subject { new_meme }
+ it { should be_a(Meme) }
+ describe "notes" do
+ subject { new_meme.notes }
+ its(:count) { should eql(3) }
+ end
+ end
+ end
+ end
+
+end

0 comments on commit 88f7e11

Please sign in to comment.