Skip to content

Instantly share code, notes, and snippets.

@sintaxi
Created April 21, 2009 04:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sintaxi/98930 to your computer and use it in GitHub Desktop.
Save sintaxi/98930 to your computer and use it in GitHub Desktop.
class Hit < ActiveRecord::Base
belongs_to :user
belongs_to :shot, :counter_cash => true
validate_on_create :check_for_gaming
def request=(request)
self.user_ip = request.remote_ip
self.user_agent = request.env['HTTP_USER_AGENT']
end
private
def check_for_gaming
# smart method that checks for shit
end
end
class Hits < Application
# this is a nested resource with only a create method
# we use render_then_call (merb only) just to pass back
# a 200 status before doing the calculation
# this action is hit with a XHR POST that looks like...
# http://example.com/shot/4587/hit
#
def create
only_provides :js
# returns a 200 and moves on. (if rails omit the render)
Merb.render_then_call(:status => 200) do
@shot = @shot.find(params[:shot_id])
@hit = Hit.new(:shot => @shot, :user_id => current_user) #will pass in nil if user is not logged in
@hit.request = request
@hit.save
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment