SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Your daddy. WALoeIII's Avatar
    Join Date
    Apr 2001
    Location
    USA
    Posts
    526
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [rails] Trouble writing a loop that will stop execution

    I'm trying to write a function that will accept a rails URL, and see if it matches one of my Rights. Rights are just controller URLs, so I can manage access at the controller level (don't need action level). What I'm trying to do is take the url passed, and see if its access controlled. If it is, the method should stop and return true, if not it will return false. This method is used to check when someone logs out if they can be redirected to where they are, or if they need to be redirected to the home URL, it protects from an infinite login loop.

    Code:
    # true if this is a protected url that would cause a login loop
    def looper_url?(url)
      rights = Right.find(:all)
      for right in rights
      if url_for(right) == url
        return true
        exit
      end
    end
    This is what I came up with, it doesn't work but it should be pretty enough to give you an idea of what I'm trying to do. I think the problem is just in my inner most if block, not sure how to stop the loop and return true.

  2. #2
    SitePoint Addict myrdhrin's Avatar
    Join Date
    Jul 2004
    Location
    Montreal
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello Andrew,

    How about this:

    Code:
    def looper_url?(url)
      rights = Right.find(:all)
      return ! rights.detect {|right|
    	url_for(right) == url
      }.nil?
    end
    The detect method of the array will stop at the first occurance which evaluates as not nil.

    For an isolated code to test the logic:

    Code:
    def url_for(right)
       return right
    end
    
    class Right
    	def self.find(param)
    		['aaa','bbb','ccc']
    	end
    end
    
    def looper_url?(url)
      rights = Right.find(:all)
      return ! rights.detect {|right|
    	url_for(right) == url
      }.nil?
    end
    
    # will return false
    puts looper_url?('afaa')
    
    # will return true
    puts looper_url?('aaa')
    Jean-Marc (aka Myrdhrin)
    M2i3 - blog - Protect your privacy with Zliki

  3. #3
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    def looper_url?(url)
      Right.find(:all).any?{|right| url_for(right) == url}
    end


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
  •