Search code examples
rubyruby-on-rails-3qr-codebefore-filter

Rails 3 before_filter setting and checking boolean values | paperchase / treasure hunt game


I am working on a paperchase / treasure hunt mobile web app. I have basic authentication, if the user scans any of the following codes he will be directed to the sign_up page. So far everything works fine but here comes the tricky part:

I have 10 QR-Codes, every single one represents one URL.

  1. QR-Code ID:1 URL:http://paperchase.heroku.com/qrs/4975
  2. QR-Code ID:2 URL:http://paperchase.heroku.com/qrs/2368
  3. QR-Code ID:3 URL:http://paperchase.heroku.com/qrs/2317
  4. QR-Code ID:4 URL:http://paperchase.heroku.com/qrs/2369
  5. QR-Code ID:5 URL:http://paperchase.heroku.com/qrs/6247
  6. QR-Code ID:6 URL:http://paperchase.heroku.com/qrs/1493
  7. QR-Code ID:7 URL:http://paperchase.heroku.com/qrs/1759
  8. QR-Code ID:8 URL:http://paperchase.heroku.com/qrs/4278
  9. QR-Code ID:9 URL:http://paperchase.heroku.com/qrs/8912
  10. QR-Code ID:10 URL:http://paperchase.heroku.com/qrs/5346

Now I want the user to scan every single code in the shown order. If he scans Code 1, he will find the directions to Code 2, if he scans Code 2, he will find the directions to Code 3 and so on. But right now it is possible to skip codes, e.g. you could scan code 10 after code 1 and win.

The solution I came up with:

All QR-Codes are set to false. If you scan QR-Code 1, it will be set to true and you can scan QR-Code 2. If you now want to scan QR-Code 5, it redirects you to the root_path because QR-Code 4 is set to false.

This is part of my User model:

  ...
  t.boolean :qr_01, :default => false
  t.boolean :qr_02, :default => false
  t.boolean :qr_03, :default => false
  t.boolean :qr_04, :default => false
  ...

Now I think that I have to write some kind of before_filter with the logic (setting QR-Codes to true, checking if all the prior QR-Codes are set to true) But I have no clue how it should look like.

Thanks in advance


Solution

  • Why not just store one int field in your model?

    For example if my User model had

     t.integer :qr_code
    

    You could simply check inside your controller action:

    def add_qr
      user = User.find_by_id(params[:id])
      if user.qr_code != params[:qr_code]-1
        redirect_to(some_url)
      else
        user.qr_code+=1
        user.save
        #do whatever else you need to do and display view
      end
    end