Changed behaviour of acts_as_paraniod. Finder wrapper removed an replaced by simple named_scope 'without_deleted'.

This commit is contained in:
Benjamin Meichsner 2009-02-11 15:23:59 +01:00
parent cd9636a650
commit 325d47b22f
18 changed files with 218 additions and 136 deletions

View File

@ -11,8 +11,8 @@ class Admin::OrdergroupsController < ApplicationController
# if the search field is used # if the search field is used
conditions = "name LIKE '%#{params[:query]}%'" unless params[:query].nil? conditions = "name LIKE '%#{params[:query]}%'" unless params[:query].nil?
@total = Ordergroup.count(:conditions => conditions ) @total = Ordergroup.without_deleted.count(:conditions => conditions )
@ordergroups = Ordergroup.paginate(:conditions => conditions, :page => params[:page], @ordergroups = Ordergroup.without_deleted.paginate(:conditions => conditions, :page => params[:page],
:per_page => @per_page, :order => 'name') :per_page => @per_page, :order => 'name')
respond_to do |format| respond_to do |format|
@ -60,7 +60,7 @@ class Admin::OrdergroupsController < ApplicationController
@ordergroup = Ordergroup.find(params[:id]) @ordergroup = Ordergroup.find(params[:id])
@ordergroup.destroy @ordergroup.destroy
redirect_to(admin_Ordergroups_url) redirect_to(admin_ordergroups_url)
end end
def memberships def memberships

View File

@ -28,12 +28,14 @@ class ArticlesController < ApplicationController
# if somebody uses the search field: # if somebody uses the search field:
conditions = ["articles.name LIKE ?", "%#{params[:query]}%"] unless params[:query].nil? conditions = ["articles.name LIKE ?", "%#{params[:query]}%"] unless params[:query].nil?
@total = @supplier.articles.count(:conditions => conditions) @total = @supplier.articles.without_deleted.count(:conditions => conditions)
@articles = @supplier.articles.paginate(:order => sort, @articles = @supplier.articles.without_deleted.paginate(
:order => sort,
:conditions => conditions, :conditions => conditions,
:page => params[:page], :page => params[:page],
:per_page => @per_page, :per_page => @per_page,
:include => :article_category) :include => :article_category
)
respond_to do |format| respond_to do |format|
format.html # list.haml format.html # list.haml
@ -136,7 +138,7 @@ class ArticlesController < ApplicationController
# Renders a form for editing all articles from a supplier # Renders a form for editing all articles from a supplier
def edit_all def edit_all
@articles = @supplier.articles.all @articles = @supplier.articles.without_deleted
end end
# Updates all article of specific supplier # Updates all article of specific supplier

View File

@ -101,7 +101,7 @@ class DeliveriesController < ApplicationController
end end
def auto_complete_for_article_name def auto_complete_for_article_name
@articles = @supplier.articles.find(:all, @articles = @supplier.articles.without_deleted.find(:all,
:conditions => [ "LOWER(articles.name) LIKE ?", '%' + params[:article][:name].downcase + '%' ], :conditions => [ "LOWER(articles.name) LIKE ?", '%' + params[:article][:name].downcase + '%' ],
:limit => 8) :limit => 8)
render :partial => 'shared/auto_complete_articles' render :partial => 'shared/auto_complete_articles'

View File

@ -20,9 +20,9 @@ class Finance::TransactionsController < ApplicationController
conditions = "name LIKE '%#{params[:query]}%'" unless params[:query].nil? conditions = "name LIKE '%#{params[:query]}%'" unless params[:query].nil?
@total = Ordergroup.count(:conditions => conditions) @total = Ordergroup.without_deleted.count(:conditions => conditions)
@groups = Ordergroup.paginate :conditions => conditions, :page => params[:page], @groups = Ordergroup.without_deleted.paginate :conditions => conditions,
:per_page => @per_page, :order => sort :page => params[:page], :per_page => @per_page, :order => sort
respond_to do |format| respond_to do |format|
format.html format.html

View File

@ -3,7 +3,7 @@ class SuppliersController < ApplicationController
helper :deliveries helper :deliveries
def index def index
@suppliers = Supplier.all :order => 'name' @suppliers = Supplier.without_deleted :order => 'name'
@deliveries = Delivery.recent @deliveries = Delivery.recent
end end
@ -53,7 +53,7 @@ class SuppliersController < ApplicationController
flash[:notice] = "Lieferant wurde gelöscht" flash[:notice] = "Lieferant wurde gelöscht"
redirect_to suppliers_path redirect_to suppliers_path
rescue => e rescue => e
flash[:error] = _("An error has occurred: ") + e.message flash[:error] = "Ein Fehler ist aufgetreten: " + e.message
redirect_to @supplier redirect_to @supplier
end end

View File

@ -5,7 +5,7 @@ module MessagesHelper
[g.name, g.id] [g.name, g.id]
end end
groups += [[" -- Bestellgruppen -- ", ""]] groups += [[" -- Bestellgruppen -- ", ""]]
groups += Ordergroup.find(:all, :order => 'name', :include => :memberships).reject{ |g| g.memberships.empty? }.collect do |g| groups += Ordergroup.without_deleted(:order => 'name', :include => :memberships).reject{ |g| g.memberships.empty? }.collect do |g|
[g.name, g.id] [g.name, g.id]
end end
groups groups

View File

@ -10,4 +10,10 @@ module OrdersHelper
link_to image_tag("save_pdf.png", :size => "16x16", :border => "0", :alt => "PDF erstellen"), link_to image_tag("save_pdf.png", :size => "16x16", :border => "0", :alt => "PDF erstellen"),
{ :action => action, :id => order, :format => :pdf }, { :title => "PDF erstellen" } { :action => action, :id => order, :format => :pdf }, { :title => "PDF erstellen" }
end end
def options_for_suppliers_to_select
suppliers = Supplier.without_deleted.collect {|s| [ s.name, url_for(:action => "new", :supplier_id => s)] }
stockit = [["Lager", url_for(:action => 'new', :supplier_id => 0)]]
options_for_select(stockit + suppliers)
end
end end

View File

@ -176,7 +176,7 @@ class Article < ActiveRecord::Base
# Checks if the article is in use before it will deleted # Checks if the article is in use before it will deleted
def check_article_in_use def check_article_in_use
raise self.name.to_s + _(" cannot be deleted. The article is used in a current order!") if self.in_open_order raise self.name.to_s + " kann nicht gelöscht werden. Der Artikel befindet sich in einer laufenden Bestellung!" if self.in_open_order
end end
# Create an ArticlePrice, when the price-attr are changed. # Create an ArticlePrice, when the price-attr are changed.

View File

@ -52,12 +52,12 @@ class Order < ActiveRecord::Base
def articles_for_ordering def articles_for_ordering
if stockit? if stockit?
StockArticle.available.all(:include => :article_category, StockArticle.available.without_deleted(:include => :article_category,
:order => 'article_categories.name, articles.name').reject{ |a| :order => 'article_categories.name, articles.name').reject{ |a|
a.quantity_available == 0 a.quantity_available == 0
}.group_by { |a| a.article_category.name } }.group_by { |a| a.article_category.name }
else else
supplier.articles.available.all.group_by { |a| a.article_category.name } supplier.articles.available.without_deleted.group_by { |a| a.article_category.name }
end end
end end

View File

@ -30,6 +30,7 @@ class Supplier < ActiveRecord::Base
has_many :orders has_many :orders
has_many :deliveries has_many :deliveries
has_many :invoices has_many :invoices
belongs_to :shared_supplier # for the sharedLists-App
attr_accessible :name, :address, :phone, :phone2, :fax, :email, :url, :contact_person, :customer_number, :delivery_days, :order_howto, :note, :shared_supplier_id, :min_order_quantity attr_accessible :name, :address, :phone, :phone2, :fax, :email, :url, :contact_person, :customer_number, :delivery_days, :order_howto, :note, :shared_supplier_id, :min_order_quantity
@ -39,16 +40,13 @@ class Supplier < ActiveRecord::Base
validates_length_of :phone, :in => 8..20 validates_length_of :phone, :in => 8..20
validates_length_of :address, :in => 8..50 validates_length_of :address, :in => 8..50
# for the sharedLists-App
belongs_to :shared_supplier
# sync all articles with the external database # sync all articles with the external database
# returns an array with articles(and prices), which should be updated (to use in a form) # returns an array with articles(and prices), which should be updated (to use in a form)
# also returns an array with outlisted_articles, which should be deleted # also returns an array with outlisted_articles, which should be deleted
def sync_all def sync_all
updated_articles = Array.new updated_articles = Array.new
outlisted_articles = Array.new outlisted_articles = Array.new
for article in articles for article in articles.without_deleted
# try to find the associated shared_article # try to find the associated shared_article
shared_article = article.shared_article shared_article = article.shared_article

View File

@ -20,6 +20,8 @@
require 'digest/sha1' require 'digest/sha1'
# specific user rights through memberships (see Group) # specific user rights through memberships (see Group)
class User < ActiveRecord::Base class User < ActiveRecord::Base
#TODO: acts_as_paraniod ??
has_many :memberships, :dependent => :destroy has_many :memberships, :dependent => :destroy
has_many :groups, :through => :memberships has_many :groups, :through => :memberships
has_one :ordergroup, :through => :memberships, :source => :group, :class_name => "Ordergroup" has_one :ordergroup, :through => :memberships, :source => :group, :class_name => "Ordergroup"

View File

@ -6,7 +6,7 @@
Neue Bestellung anlegen für Neue Bestellung anlegen für
%select{:onchange => "redirectTo(this)", :style => "font-size: 0.9em;margin-left:1em;"} %select{:onchange => "redirectTo(this)", :style => "font-size: 0.9em;margin-left:1em;"}
%option{:selected => 'selected'}=_ "Choose a supplier..." %option{:selected => 'selected'}=_ "Choose a supplier..."
= options_for_select(Supplier.find(:all).collect {|s| [ s.name, url_for(:action => "new", :supplier_id => s)] }) = options_for_suppliers_to_select
%br/ %br/
.left_column{:style => "width:55em"} .left_column{:style => "width:55em"}
.box_title .box_title

View File

@ -4,8 +4,8 @@
- form_tag do - form_tag do
Neue Lieferung anlegen für: Neue Lieferung anlegen für:
= select_tag :new_delivery, | = select_tag :new_delivery, |
options_for_select([" -- Lieferantin wählen --", ""] + | options_for_select([[" -- Lieferantin wählen --", ""]] + |
Supplier.find(:all).collect {|s| [ s.name, url_for(new_supplier_delivery_path(s))] }), | Supplier.without_deleted.collect {|s| [ s.name, url_for(new_supplier_delivery_path(s))] }), |
:onchange => "redirectTo(this)", :style => "font-size: 0.9em;margin-left:1em;" | :onchange => "redirectTo(this)", :style => "font-size: 0.9em;margin-left:1em;" |
%p %p
= link_to "Lagerbestellung online stellen", new_order_path(:supplier_id => 0) = link_to "Lagerbestellung online stellen", new_order_path(:supplier_id => 0)

View File

@ -52,6 +52,30 @@ input {
abbr, acronym { abbr, acronym {
cursor: help; } cursor: help; }
input, textarea, select {
border: 1px solid #D7D7D7;
font-family: verdana, arial, helvetica, sans-serif;
font-size: 0.9em;
padding: .2em; }
input:focus, textarea:focus, select:focus {
border-color: #000; }
input[type="button"], input[type="submit"], input[type="reset"] {
background: #EEEEEE none repeat scroll 0%;
border: 1px outset #CCCCCC;
color: #222222;
padding: 0.1em 0.5em;
font-size: 1em;
font-weight: bold; }
select {
max-width: 15em; }
option {
border-top: 1px solid #D7D7D7;
margin: .2em 0; }
#login { #login {
margin: auto; margin: auto;
width: 27em; width: 27em;

View File

@ -52,6 +52,30 @@ input {
abbr, acronym { abbr, acronym {
cursor: help; } cursor: help; }
input, textarea, select {
border: 1px solid #D7D7D7;
font-family: verdana, arial, helvetica, sans-serif;
font-size: 0.9em;
padding: .2em; }
input:focus, textarea:focus, select:focus {
border-color: #000; }
input[type="button"], input[type="submit"], input[type="reset"] {
background: #EEEEEE none repeat scroll 0%;
border: 1px outset #CCCCCC;
color: #222222;
padding: 0.1em 0.5em;
font-size: 1em;
font-weight: bold; }
select {
max-width: 15em; }
option {
border-top: 1px solid #D7D7D7;
margin: .2em 0; }
#login { #login {
margin: auto; margin: auto;
width: 27em; width: 27em;

View File

@ -62,6 +62,31 @@ input
abbr, acronym abbr, acronym
:cursor help :cursor help
input, textarea, select
border: 1px solid #D7D7D7
font-family: verdana, arial, helvetica, sans-serif
font-size: 0.9em
padding: .2em
input:focus, textarea:focus, select:focus
border-color: #000
input[type="button"], input[type="submit"], input[type="reset"]
background: #EEEEEE none repeat scroll 0%
border: 1px outset #CCCCCC
color: #222222
padding: 0.1em 0.5em
font-size: 1em
font-weight: bold
select
max-width: 15em
option
border-top: 1px solid #D7D7D7
margin: .2em 0
// ********************************* loginpage // ********************************* loginpage
#login #login
:margin auto :margin auto

View File

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

View File

@ -58,10 +58,11 @@ module Caboose #:nodoc:
self.deleted_attribute = options[:with] || :deleted_at self.deleted_attribute = options[:with] || :deleted_at
alias_method :destroy_without_callbacks!, :destroy_without_callbacks alias_method :destroy_without_callbacks!, :destroy_without_callbacks
class << self class << self
alias_method :find_every_with_deleted, :find_every # alias_method :find_every_with_deleted, :find_every
alias_method :calculate_with_deleted, :calculate # alias_method :calculate_with_deleted, :calculate
alias_method :delete_all!, :delete_all alias_method :delete_all!, :delete_all
end end
send :named_scope, :without_deleted, :conditions => {:deleted_at => nil}
end end
include InstanceMethods include InstanceMethods
end end
@ -77,55 +78,55 @@ module Caboose #:nodoc:
end end
module ClassMethods module ClassMethods
def find_with_deleted(*args) # def find_with_deleted(*args)
options = args.extract_options! # options = args.extract_options!
validate_find_options(options) # validate_find_options(options)
set_readonly_option!(options) # set_readonly_option!(options)
options[:with_deleted] = true # yuck! # options[:with_deleted] = true # yuck!
#
case args.first # case args.first
when :first then find_initial(options) # when :first then find_initial(options)
when :all then find_every(options) # when :all then find_every(options)
else find_from_ids(args, options) # else find_from_ids(args, options)
end # end
end # end
#
def find_only_deleted(*args) # def find_only_deleted(*args)
options = args.extract_options! # options = args.extract_options!
validate_find_options(options) # validate_find_options(options)
set_readonly_option!(options) # set_readonly_option!(options)
options[:only_deleted] = true # yuck! # options[:only_deleted] = true # yuck!
#
case args.first # case args.first
when :first then find_initial(options) # when :first then find_initial(options)
when :all then find_every(options) # when :all then find_every(options)
else find_from_ids(args, options) # else find_from_ids(args, options)
end # end
end # end
#
def exists?(*args) # def exists?(*args)
with_deleted_scope { exists_with_deleted?(*args) } # with_deleted_scope { exists_with_deleted?(*args) }
end # end
#
def exists_only_deleted?(*args) # def exists_only_deleted?(*args)
with_only_deleted_scope { exists_with_deleted?(*args) } # with_only_deleted_scope { exists_with_deleted?(*args) }
end # end
#
def count_with_deleted(*args) # def count_with_deleted(*args)
calculate_with_deleted(:count, *construct_count_options_from_args(*args)) # calculate_with_deleted(:count, *construct_count_options_from_args(*args))
end # end
#
def count_only_deleted(*args) # def count_only_deleted(*args)
with_only_deleted_scope { count_with_deleted(*args) } # with_only_deleted_scope { count_with_deleted(*args) }
end # end
#
def count(*args) # def count(*args)
with_deleted_scope { count_with_deleted(*args) } # with_deleted_scope { count_with_deleted(*args) }
end # end
#
def calculate(*args) # def calculate(*args)
with_deleted_scope { calculate_with_deleted(*args) } # with_deleted_scope { calculate_with_deleted(*args) }
end # end
def delete_all(conditions = nil) def delete_all(conditions = nil)
self.update_all ["#{self.deleted_attribute} = ?", current_time], conditions self.update_all ["#{self.deleted_attribute} = ?", current_time], conditions
@ -136,23 +137,23 @@ module Caboose #:nodoc:
default_timezone == :utc ? Time.now.utc : Time.now default_timezone == :utc ? Time.now.utc : Time.now
end end
def with_deleted_scope(&block) # def with_deleted_scope(&block)
with_scope({:find => { :conditions => ["#{table_name}.#{deleted_attribute} IS NULL OR #{table_name}.#{deleted_attribute} > ?", current_time] } }, :merge, &block) # with_scope({:find => { :conditions => ["#{table_name}.#{deleted_attribute} IS NULL OR #{table_name}.#{deleted_attribute} > ?", current_time] } }, :merge, &block)
end # end
#
def with_only_deleted_scope(&block) # 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) # with_scope({:find => { :conditions => ["#{table_name}.#{deleted_attribute} IS NOT NULL AND #{table_name}.#{deleted_attribute} <= ?", current_time] } }, :merge, &block)
end # end
private private
# all find calls lead here # all find calls lead here
def find_every(options) # def find_every(options)
options.delete(:with_deleted) ? # options.delete(:with_deleted) ?
find_every_with_deleted(options) : # find_every_with_deleted(options) :
options.delete(:only_deleted) ? # options.delete(:only_deleted) ?
with_only_deleted_scope { find_every_with_deleted(options) } : # with_only_deleted_scope { find_every_with_deleted(options) } :
with_deleted_scope { find_every_with_deleted(options) } # with_deleted_scope { find_every_with_deleted(options) }
end # end
end end
def destroy_without_callbacks def destroy_without_callbacks
@ -182,14 +183,14 @@ module Caboose #:nodoc:
save! save!
end end
def recover_with_associations!(*associations) # def recover_with_associations!(*associations)
self.recover! # self.recover!
associations.to_a.each do |assoc| # associations.to_a.each do |assoc|
self.send(assoc).find_with_deleted(:all).each do |a| # self.send(assoc).find_with_deleted(:all).each do |a|
a.recover! if a.class.paranoid? # a.recover! if a.class.paranoid?
end # end
end # end
end # end
end end
end end
end end