Hi there,
I love the acts_as_* family that rails uses. I’ve also found I can
create something similar by dropping the following code into my
activerecord classes:
class MyClass < ActiveRecord::Base
class << self
alias_method :count_with_unapproved, :count
end
def self.find(*args)
options = extract_options_from_args!(args)
if options[:conditions].nil?
options[:conditions] = [“status = ‘approved’”]
end
args << options
super(*args)
end
def self.count(conditions = nil, joins = nil)
logger.info conditions.inspect
if conditions.nil?
conditions = “status = ‘approved’”
else
case conditions
when String then conditions += " AND status = ‘approved’"
when Array then conditions[0] +=" AND status = ‘approved’"
end
end
super(conditions, joins)
end
end
which lets me get the equivalent functionality of something like
acts_as_approved, where I can easily subdivide my records into approved
and not approved.
I’d love this to be a mixin, but when I look at the code for, say,
acts_as_paranoid I feel like the ruby novice I am. I’d like to create
other versions of this, like acts_as_private that would allow me to
divide off other things based on the setting in a private field, but
again, I don’t feel like I’m ready to create my own mixin from scratch.
I was thinking it would be cool to have mixin called “acts_as_whatever”
that you could use like this:
class MyClass < ActiveRecord::Base
acts_as_whatever :status => ‘approved’
end
which would do the same as my code above - anybody know if that’s
practical?
Then I could do things like:
class MyClass < ActiveRecord::Base
acts_as_whatever :public=> ‘true’
end
I guess alternatively I could just extend ActiveRecord::Base and make my
private or status-oriented classes extend that, or I could try again to
get my head round mixin creation.
Any thoughts?
CHEERS> SAM