Skip to content

Instantly share code, notes, and snippets.

@patrick99e99
Created April 30, 2010 22:52
Show Gist options
  • Save patrick99e99/385859 to your computer and use it in GitHub Desktop.
Save patrick99e99/385859 to your computer and use it in GitHub Desktop.
class ResultList < ActiveRecord::Base
belongs_to :table
belongs_to :newsletter
has_many :contact_results, :dependent => :destroy
has_many :contacts, :through => :contact_results
def add_or_remove(length, remove)
remove ? (-1 * length) : length
end
def add_or_remove_checked_to_list(item_ids, remove)
item_ids = item_ids.split(",").map(&:to_i)
update_attributes(:job_item_count => contacts.count, :job_action_count => add_or_remove(item_ids.length, remove))
if remove
contact_results.each do |cr|
if item_ids.include?(cr.contact_id)
cr.destroy
item_ids.delete(cr.contact_id)
end
end
else
contact_ids = contact_results.map {|cr| cr.id unless item_ids.include?(cr.id)}
item_ids.each {|item_id| contacts << Contact.find(item_id)}
end
end
def add_or_remove_search_result(options, remove)
# options are search, scope, display, remove (true/false) and the model setup hash
search_results = Contact.search(options.merge(:no_paginate => true)).first
contact_ids = contacts.map(&:id)
update_attributes(:job_action_count => add_or_remove(search_results.length, remove), :job_item_count => contact_ids.length)
if remove
result_id_list = search_results.map(&:id)
contact_results.each do |cr|
if result_id_list.include?(cr.contact_id)
cr.destroy
result_id_list.delete(cr.contact_id)
end
end
else
search_results = search_results.reject {|sr| contact_ids.include?(sr.id)}
search_results.each do |contact|
contacts << contact
end
end
end
def clear
update_attributes(:job_action_count => -contacts.length, :job_item_count => contacts.length)
contact_results.map { |cr| cr.destroy }
end
end
require 'spec_helper'
describe ResultList do
before(:all) do
ResultList.all.map(&:destroy)
Contact.all.map(&:destroy)
@result_list = ResultList.create!
5.times do
Contact.create!(:first_name => "James", :last_name => "Kirk")
end
@contact_ids = Contact.all.map(&:id)
end
it "should add the contacts to the list" do
@result_list.add_or_remove_checked_to_list(@contact_ids.join(","), false)
@result_list.job_action_count.should == @contact_ids.length
@result_list.contacts.count.should == @contact_ids.length
end
it "should remove the contacts from the list" do
@result_list.add_or_remove_checked_to_list(@contact_ids.join(","), true)
@result_list.job_action_count.should == -@contact_ids.length
@result_list.contacts.count.should == 0
end
it "should add the search result to the list" do
@result_list.add_or_remove_search_result({:search => "Kirk",
:search_scope => "name",
:display => "all",
:model => Contact.setup}, false)
@result_list.job_action_count.should == @contact_ids.length
@result_list.contacts.count.should == @contact_ids.length
end
it "should remove the search result from the list" do
@result_list.add_or_remove_search_result({:search => "Kirk",
:search_scope => "name",
:display => "all",
:model => Contact.setup}, true)
@result_list.job_action_count.should == -@contact_ids.length
@result_list.contacts.count.should == 0
end
it "should clear the contact results" do
@result_list.add_or_remove_checked_to_list(@contact_ids.join(","), false)
@result_list.contacts.count.should == @contact_ids.length
@result_list.clear
@result_list.contacts.count.should == 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment