SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Miami, FL
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Solution to deleting directories

    Hi everyone,

    I've got an app that's going well, but I've encountered a little setback. For the most part, it's a simple maneuver. I want to be able to delete a directory when an entry is deleted. When someone manually deletes it, I've got this code in the controller:

    Code Ruby:
    def delete
      @user = User.find(session[:user_id])
      @todelete = Cpost.find(params[:id])
      @todelete.destroy
      @cpostdir = "#{RAILS_ROOT}/public/images/users/#{@user.screen_name}/#{@todelete.id}"
      FileUtils.remove_dir(@cpostdir) if File.exists?(@cpostdir)
      redirect_to :controller => 'cposts', :action => 'list'
      flash[:notice] = "Your entry has been deleted."
    end

    That's all well and good, it works exactly how you suspect it would. Now, what I want to do is automatically delete entries after a certain amount of time, let's say, when they're 15 days old.

    This is what I've got in my app controller so far (a before filter calls the method)

    Code Ruby:
    def remove_old_cposts
      Cpost.destroy_all(["created_at < ?", 15.days.ago])
    end

    It removes all entries older than 15 days. The thing I need added is to remove the directory that corresponds to each one of those entries being deleted. If you noticed in the method I posted before, the specific directory contains the screen name of the person who posted it, and finally the ID of the post (both directories in an arbitrarity root/images/users/ dir.) So, in an entry made by the user "Tester1", the images would be placed in root/images/users/Tester1/:id/-images-go-here. And Tester2's would be in root/images/users/Tester2/:id/-images-go-here. Finally, Tester3's are in root/images/users/Tester3/:id/-images-go-here. Let's say Tester1 and Tester3's are older than 15 days, how would I go about finding the directories to those? It's easy when you're deleting one single file, since you have the Cpost ID and the session user, but not when you're destroying a bunch of stuff at once.

    What's the best way to also remove those directories automatically?

    Thanks in advance!

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Create an after_destroy method in the Cpost model that deletes the corresponding directory.

  3. #3
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, this folder deletion code does not belong in the controller, move it into your model. I'd also define a Cpost.destroy_old method on your model and call that instead of having the business rule of what constitutes an "old" post in the controller.

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Miami, FL
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the response, can you show me a quick example of using after_destroy? I've never used it (or even heard about it) before.

  5. #5
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just define a method after_destroy in your model:

    Code:
    class Cpost < ...
      def after_destroy
         remove_dir(...)
      end
    end
    
    Cpost.find(4).destroy => the after_destroy method will be called here.

  6. #6
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Miami, FL
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks, gents. This will work out nicely.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •