Organizando plugins e gems.

This commit is contained in:
2009-07-16 11:51:47 -03:00
parent 4e22c87074
commit dcfc38eb09
506 changed files with 10538 additions and 45562 deletions

View File

@@ -1,3 +1,8 @@
* (16 Apr 2009)
Allow :with_deleted and :only_deleted options to work with count and calculate.
Fixes compatibility with will_paginate. [James Le Cuirot]
* (4 Oct 2007)
Update for Edge rails: remove support for legacy #count args

View File

@@ -1,26 +1,5 @@
= acts_as_paranoid
Overrides some basic methods for the current model so that calling #destroy sets a 'deleted_at' field to the
current timestamp. ActiveRecord is required.
Overrides some basic methods for the current model so that calling #destroy sets a 'deleted_at' field to the current timestamp. ActiveRecord is required.
== Resources
Install
* gem install acts_as_paranoid
Rubyforge project
* http://rubyforge.org/projects/ar-paranoid
RDocs
* http://ar-paranoid.rubyforge.org
Subversion
* http://techno-weenie.net/svn/projects/acts_as_paranoid
Collaboa
* http://collaboa.techno-weenie.net/repository/browse/acts_as_paranoid
http://github.com/technoweenie/acts_as_paranoid

View File

@@ -1,41 +1,10 @@
== Creating the test database
1. Pick Rails version. Either dump this plugin in a Rails app and run it from there, or specify it as an ENV var:
The default name for the test databases is "activerecord_paranoid". If you
want to use another database name then be sure to update the connection
adapter setups you want to test with in test/connections/<your database>/connection.rb.
When you have the database online, you can import the fixture tables with
the test/fixtures/db_definitions/*.sql files.
RAILS=2.2.2 rake
RAILS=2.2.2 ruby test/paranoid_test.rb
Make sure that you create database objects with the same user that you specified in i
connection.rb otherwise (on Postgres, at least) tests for default values will fail.
2. Setup your database. By default sqlite3 is used, and no further setup is necessary. You can pick any of the listed databases in test/database.yml. Be sure to create the database first.
== Running with Rake
DB=mysql rake
The easiest way to run the unit tests is through Rake. The default task runs
the entire test suite for all the adapters. You can also run the suite on just
one adapter by using the tasks test_mysql_ruby, test_ruby_mysql, test_sqlite,
or test_postresql. For more information, checkout the full array of rake tasks with "rake -T"
Rake can be found at http://rake.rubyforge.org
== Running by hand
Unit tests are located in test directory. If you only want to run a single test suite,
or don't want to bother with Rake, you can do so with something like:
cd test; ruby -I "connections/native_mysql" base_test.rb
That'll run the base suite using the MySQL-Ruby adapter. Change the adapter
and test suite name as needed.
== Faster tests
If you are using a database that supports transactions, you can set the
"AR_TX_FIXTURES" environment variable to "yes" to use transactional fixtures.
This gives a very large speed boost. With rake:
rake AR_TX_FIXTURES=yes
Or, by hand:
AR_TX_FIXTURES=yes ruby -I connections/native_sqlite3 base_test.rb
3. Profit!!

View File

@@ -11,6 +11,24 @@ class << ActiveRecord::Base
end
end
def has_many_without_deleted(association_id, options = {}, &extension)
with_deleted = options.delete :with_deleted
returning has_many_with_deleted(association_id, options, &extension) do
if options[:through] && !with_deleted
reflection = reflect_on_association(association_id)
collection_reader_method(reflection, Caboose::Acts::HasManyThroughWithoutDeletedAssociation)
collection_accessor_methods(reflection, Caboose::Acts::HasManyThroughWithoutDeletedAssociation, false)
end
end
end
alias_method_chain :belongs_to, :deleted
alias_method :has_many_with_deleted, :has_many
alias_method :has_many, :has_many_without_deleted
alias_method :exists_with_deleted?, :exists?
end
ActiveRecord::Base.send :include, Caboose::Acts::Paranoid
ActiveRecord::Base.send :include, Caboose::Acts::ParanoidFindWrapper
class << ActiveRecord::Base
alias_method_chain :acts_as_paranoid, :find_wrapper
end
ActiveRecord::Base.send :include, Caboose::Acts::Paranoid

View File

@@ -0,0 +1,27 @@
module Caboose # :nodoc:
module Acts # :nodoc:
class HasManyThroughWithoutDeletedAssociation < ActiveRecord::Associations::HasManyThroughAssociation
protected
def current_time
ActiveRecord::Base.default_timezone == :utc ? Time.now.utc : Time.now
end
def construct_conditions
return super unless @reflection.through_reflection.klass.paranoid?
table_name = @reflection.through_reflection.table_name
conditions = construct_quoted_owner_attributes(@reflection.through_reflection).map do |attr, value|
"#{table_name}.#{attr} = #{value}"
end
deleted_attribute = @reflection.through_reflection.klass.deleted_attribute
quoted_current_time = @reflection.through_reflection.klass.quote_value(
current_time,
@reflection.through_reflection.klass.columns_hash[deleted_attribute.to_s])
conditions << "#{table_name}.#{deleted_attribute} IS NULL OR #{table_name}.#{deleted_attribute} > #{quoted_current_time}"
conditions << sql_conditions if sql_conditions
"(" + conditions.join(') AND (') + ")"
end
end
end
end

View File

@@ -16,8 +16,8 @@ module Caboose #:nodoc:
# Widget.find_with_deleted(:all)
# # SELECT * FROM widgets
#
# Widget.find(:all, :with_deleted => true)
# # SELECT * FROM widgets
# Widget.find_only_deleted(:all)
# # SELECT * FROM widgets WHERE widgets.deleted_at IS NOT NULL
#
# Widget.find_with_deleted(1).deleted?
# # Returns true if the record was previously destroyed, false if not
@@ -31,6 +31,9 @@ module Caboose #:nodoc:
# Widget.count_with_deleted
# # SELECT COUNT(*) FROM widgets
#
# Widget.count_only_deleted
# # SELECT COUNT(*) FROM widgets WHERE widgets.deleted_at IS NOT NULL
#
# Widget.delete_all
# # UPDATE widgets SET deleted_at = '2005-09-17 17:46:36'
#
@@ -87,16 +90,49 @@ module Caboose #:nodoc:
end
end
def find_only_deleted(*args)
options = args.extract_options!
validate_find_options(options)
set_readonly_option!(options)
options[:only_deleted] = true # yuck!
case args.first
when :first then find_initial(options)
when :all then find_every(options)
else find_from_ids(args, options)
end
end
def exists?(*args)
with_deleted_scope { exists_with_deleted?(*args) }
end
def exists_only_deleted?(*args)
with_only_deleted_scope { exists_with_deleted?(*args) }
end
def count_with_deleted(*args)
calculate_with_deleted(:count, *construct_count_options_from_args(*args))
end
def count_only_deleted(*args)
with_only_deleted_scope { count_with_deleted(*args) }
end
def count(*args)
with_deleted_scope { count_with_deleted(*args) }
with, only = extract_deleted_options(args.last) if args.last.is_a?(Hash)
with ? count_with_deleted(*args) :
only ? count_only_deleted(*args) :
with_deleted_scope { count_with_deleted(*args) }
end
def calculate(*args)
with_deleted_scope { calculate_with_deleted(*args) }
with, only = extract_deleted_options(args.last) if args.last.is_a?(Hash)
with ? calculate_with_deleted(*args) :
only ? calculate_only_deleted(*args) :
with_deleted_scope { calculate_with_deleted(*args) }
end
def delete_all(conditions = nil)
@@ -112,18 +148,28 @@ module Caboose #:nodoc:
with_scope({:find => { :conditions => ["#{table_name}.#{deleted_attribute} IS NULL OR #{table_name}.#{deleted_attribute} > ?", current_time] } }, :merge, &block)
end
def with_only_deleted_scope(&block)
with_scope({:find => { :conditions => ["#{table_name}.#{deleted_attribute} IS NOT NULL AND #{table_name}.#{deleted_attribute} <= ?", current_time] } }, :merge, &block)
end
private
# all find calls lead here
def find_every(options)
options.delete(:with_deleted) ?
find_every_with_deleted(options) :
with_deleted_scope { find_every_with_deleted(options) }
with, only = extract_deleted_options(options)
with ? find_every_with_deleted(options) :
only ? with_only_deleted_scope { find_every_with_deleted(options) } :
with_deleted_scope { find_every_with_deleted(options) }
end
def extract_deleted_options(options)
return options.delete(:with_deleted), options.delete(:only_deleted)
end
end
def destroy_without_callbacks
unless new_record?
self.class.update_all self.class.send(:sanitize_sql, ["#{self.class.deleted_attribute} = ?", self.class.send(:current_time)]), ["#{self.class.primary_key} = ?", id]
self.class.update_all self.class.send(:sanitize_sql, ["#{self.class.deleted_attribute} = ?", (self.deleted_at = self.class.send(:current_time))]), ["#{self.class.primary_key} = ?", id]
end
freeze
end
@@ -143,11 +189,19 @@ module Caboose #:nodoc:
!!read_attribute(:deleted_at)
end
def restore!
def recover!
self.deleted_at = nil
self.save!
save!
end
def recover_with_associations!(*associations)
self.recover!
associations.to_a.each do |assoc|
self.send(assoc).find_with_deleted(:all).each do |a|
a.recover! if a.class.paranoid?
end
end
end
end
end
end

View File

@@ -0,0 +1,94 @@
module Caboose #:nodoc:
module Acts #:nodoc:
# Adds a wrapper find method which can identify :with_deleted or :only_deleted options
# and would call the corresponding acts_as_paranoid finders find_with_deleted or
# find_only_deleted methods.
#
# With this wrapper you can easily change from using this pattern:
#
# if some_condition_enabling_access_to_deleted_records?
# @post = Post.find_with_deleted(params[:id])
# else
# @post = Post.find(params[:id])
# end
#
# to this:
#
# @post = Post.find(params[:id], :with_deleted => some_condition_enabling_access_to_deleted_records?)
#
# Examples
#
# class Widget < ActiveRecord::Base
# acts_as_paranoid
# end
#
# Widget.find(:all)
# # SELECT * FROM widgets WHERE widgets.deleted_at IS NULL
#
# Widget.find(:all, :with_deleted => false)
# # SELECT * FROM widgets WHERE widgets.deleted_at IS NULL
#
# Widget.find_with_deleted(:all)
# # SELECT * FROM widgets
#
# Widget.find(:all, :with_deleted => true)
# # SELECT * FROM widgets
#
# Widget.find_only_deleted(:all)
# # SELECT * FROM widgets WHERE widgets.deleted_at IS NOT NULL
#
# Widget.find(:all, :only_deleted => true)
# # SELECT * FROM widgets WHERE widgets.deleted_at IS NOT NULL
#
# Widget.find(:all, :only_deleted => false)
# # SELECT * FROM widgets WHERE widgets.deleted_at IS NULL
#
module ParanoidFindWrapper
def self.included(base) # :nodoc:
base.extend ClassMethods
end
module ClassMethods
def acts_as_paranoid_with_find_wrapper(options = {})
unless paranoid? # don't let AR call this twice
acts_as_paranoid_without_find_wrapper(options)
class << self
alias_method :find_without_find_wrapper, :find
alias_method :validate_find_options_without_find_wrapper, :validate_find_options
end
end
include InstanceMethods
end
end
module InstanceMethods #:nodoc:
def self.included(base) # :nodoc:
base.extend ClassMethods
end
module ClassMethods
# This is a wrapper for the regular "find" so you can pass acts_as_paranoid related
# options and determine which finder to call.
def find(*args)
options = args.extract_options!
# Determine who to call.
finder_option = VALID_PARANOID_FIND_OPTIONS.detect { |key| options.delete(key) } || :without_find_wrapper
finder_method = "find_#{finder_option}".to_sym
# Put back the options in the args now that they don't include the extended keys.
args << options
send(finder_method, *args)
end
protected
VALID_PARANOID_FIND_OPTIONS = [:with_deleted, :only_deleted]
def validate_find_options(options) #:nodoc:
cleaned_options = options.reject { |k, v| VALID_PARANOID_FIND_OPTIONS.include?(k) }
validate_find_options_without_find_wrapper(cleaned_options)
end
end
end
end
end
end

View File

@@ -0,0 +1,9 @@
tagging_1:
id: 1
tag_id: 1
widget_id: 1
deleted_at: '2005-01-01 00:00:00'
tagging_2:
id: 2
tag_id: 2
widget_id: 1

View File

@@ -0,0 +1,6 @@
tag_1:
id: 1
name: 'tag 1'
tag_2:
id: 2
name: 'tag 1'

View File

@@ -6,6 +6,9 @@ class Widget < ActiveRecord::Base
has_and_belongs_to_many :habtm_categories, :class_name => 'Category'
has_one :category
belongs_to :parent_category, :class_name => 'Category'
has_many :taggings
has_many :tags, :through => :taggings
has_many :any_tags, :through => :taggings, :class_name => 'Tag', :source => :tag, :with_deleted => true
end
class Category < ActiveRecord::Base
@@ -22,15 +25,47 @@ class Category < ActiveRecord::Base
end
end
class Tag < ActiveRecord::Base
has_many :taggings
has_many :widgets, :through => :taggings
end
class Tagging < ActiveRecord::Base
belongs_to :tag
belongs_to :widget
acts_as_paranoid
end
class NonParanoidAndroid < ActiveRecord::Base
end
class ParanoidTest < Test::Unit::TestCase
fixtures :widgets, :categories, :categories_widgets
fixtures :widgets, :categories, :categories_widgets, :tags, :taggings
def test_should_recognize_with_deleted_option
assert_equal [1, 2], Widget.find(:all, :with_deleted => true).collect { |w| w.id }
assert_equal [1], Widget.find(:all, :with_deleted => false).collect { |w| w.id }
end
def test_should_recognize_only_deleted_option
assert_equal [2], Widget.find(:all, :only_deleted => true).collect { |w| w.id }
assert_equal [1], Widget.find(:all, :only_deleted => false).collect { |w| w.id }
end
def test_should_exists_with_deleted
assert Widget.exists_with_deleted?(2)
assert !Widget.exists?(2)
end
def test_should_exists_only_deleted
assert Widget.exists_only_deleted?(2)
assert !Widget.exists_only_deleted?(1)
end
def test_should_count_with_deleted
assert_equal 1, Widget.count
assert_equal 2, Widget.count_with_deleted
assert_equal 1, Widget.count_only_deleted
assert_equal 2, Widget.calculate_with_deleted(:count, :all)
end
@@ -50,6 +85,7 @@ class ParanoidTest < Test::Unit::TestCase
widgets(:widget_1).destroy!
assert_equal 0, Widget.count
assert_equal 0, Category.count
assert_equal 1, Widget.count_only_deleted
assert_equal 1, Widget.calculate_with_deleted(:count, :all)
# Category doesn't get destroyed because the dependent before_destroy callback uses #destroy
assert_equal 4, Category.calculate_with_deleted(:count, :all)
@@ -94,6 +130,12 @@ class ParanoidTest < Test::Unit::TestCase
assert_equal 1, Widget.count
assert_equal 1, Widget.count(:all, :conditions => ['title=?', 'widget 1'])
assert_equal 2, Widget.calculate_with_deleted(:count, :all)
assert_equal 1, Widget.count_only_deleted
end
def test_should_find_only_deleted
assert_equal [2], Widget.find_only_deleted(:all).collect { |w| w.id }
assert_equal [1, 2], Widget.find_with_deleted(:all, :order => 'id').collect { |w| w.id }
end
def test_should_not_find_deleted
@@ -111,6 +153,16 @@ class ParanoidTest < Test::Unit::TestCase
assert_equal [categories(:category_1)], widgets(:widget_1).habtm_categories
end
def test_should_not_find_deleted_has_many_through_associations
assert_equal 1, widgets(:widget_1).tags.size
assert_equal [tags(:tag_2)], widgets(:widget_1).tags
end
def test_should_find_has_many_through_associations_with_deleted
assert_equal 2, widgets(:widget_1).any_tags.size
assert_equal Tag.find(:all), widgets(:widget_1).any_tags
end
def test_should_not_find_deleted_belongs_to_associations
assert_nil Category.find_with_deleted(3).widget
end
@@ -208,6 +260,24 @@ class ParanoidTest < Test::Unit::TestCase
assert_equal [], w[2].categories.search('c').ids
assert_equal [3,4], w[2].categories.search_with_deleted('c').ids
end
def test_should_recover_record
Widget.find(1).destroy
assert_equal true, Widget.find_with_deleted(1).deleted?
Widget.find_with_deleted(1).recover!
assert_equal false, Widget.find(1).deleted?
end
def test_should_recover_record_and_has_many_associations
Widget.find(1).destroy
assert_equal true, Widget.find_with_deleted(1).deleted?
assert_equal true, Category.find_with_deleted(1).deleted?
Widget.find_with_deleted(1).recover_with_associations!(:categories)
assert_equal false, Widget.find(1).deleted?
assert_equal false, Category.find(1).deleted?
end
end
class Array

View File

@@ -16,5 +16,15 @@ ActiveRecord::Schema.define(:version => 1) do
t.column :category_id, :integer
t.column :widget_id, :integer
end
create_table :tags, :force => true do |t|
t.column :name, :string, :limit => 50
end
create_table :taggings, :force => true do |t|
t.column :tag_id, :integer
t.column :widget_id, :integer
t.column :deleted_at, :timestamp
end
end

View File

@@ -1,13 +1,27 @@
$:.unshift(File.dirname(__FILE__) + '/../lib')
require 'test/unit'
require File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb'))
require 'rubygems'
if ENV['RAILS'].nil?
require File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb'))
else
# specific rails version targeted
# load activerecord and plugin manually
gem 'activerecord', "=#{ENV['RAILS']}"
require 'active_record'
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
Dir["#{$LOAD_PATH.last}/**/*.rb"].each do |path|
require path[$LOAD_PATH.last.size + 1..-1]
end
require File.join(File.dirname(__FILE__), '..', 'init.rb')
end
require 'active_record/fixtures'
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
# do this so fixtures will load
ActiveRecord::Base.configurations.update config
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite'])
ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite3'])
load(File.dirname(__FILE__) + "/schema.rb")