I think I must of missed something. I also want to include a similar archive
function
The fields included in this function are
archived - tinyint (0 active, 1 archived)
archived_by - user
archived_on - timestamp
To achieve this I used
in the UserMonitor module
alias_method :archive_without_user, :archive
alias_method :archive, :archive_with_user
alias_method :reactivate_without_user, :reactivate
alias_method :reactivate, :reactivate_with_user
def archive_with_user
user = User.current_user
self[:archived_by] = user if respond_to?(:archived_by)
self[:archived_on] = Time.now if respond_to?(:archived_on)
self[:archived] = 1 if respond_to?(:archived)
self.save
end
def reactivate_with_user
user = User.current_user
self[:archived_by] = nil if respond_to?(:archived_by)
self[:archived_on] = nil if respond_to?(:archived_on)
self[:archived] = 0 if respond_to?(:archived)
self.save
end
And in the environment.rb
class ActiveRecord::Base
def archive
end
def archived_by
begin
User.find(self[:archived_by])
rescue ActiveRecord::RecordNotFound
nil
end
end
def archived_by=(user)
self[:archived_by] = user.id <http://user.id>
end
def reactivate
end
end
And in the conroller
def archive
@company = Company.find(params[:id])
if @company.archive
flash[:notice] = ''Company was successfully updated.''
redirect_to :action => ''show'', :id => @company
else
render :action => ''edit''
end
end
def reactivate
@company = Company.find(params[:id])
if @company.activate
flash[:notice] = ''Company was successfully reactivated''
redirect_to :action=>''show'', :id=>@company
else
render :action=>''edit''
end
end
I tried to include the archived_by and archived_on in the UserMonitor but it
didn''t seem to do anything.
This works, but I know it''s ugly. There has to be a nicer way to do
this.
Any suggestions?
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails