Skip to content

Instantly share code, notes, and snippets.

@jefflunt
Created March 15, 2011 15:51
Show Gist options
  • Save jefflunt/870916 to your computer and use it in GitHub Desktop.
Save jefflunt/870916 to your computer and use it in GitHub Desktop.
models/controllers for FeelGoodTrader.com notification system
class OpenNotification < ActiveRecord::Base
belongs_to :user
belongs_to :listing
validates_presence_of :user_id
validates_presence_of :listing_id
validates_uniqueness_of :listing_id, :scope => :user_id
validate :a_user_cannot_get_a_notification_for_their_own_listing
def self.new_open_notifications(user_id)
@open_notifications = OpenNotification.find(
:all,
:conditions => ["user_id = ?", user_id]
)
end
private
def a_user_cannot_get_a_notification_for_their_own_listing
unless user_id != listing.user_id
errors.add("A user cannot receive a notification for their own listing.")
end
end
end
class OpenNotificationsController < ApplicationController
before_filter :require_user
before_filter :require_notification_author, :only => [:destroy]
def ssl_allowed?
true
end
def destroy
open_notification = OpenNotification.find(params[:id])
if open_notification
open_notification.destroy
end
redirect_back_or :root
end
private
def require_notification_author
unless current_user.id == OpenNotification.find(params[:id]).user_id
flash[:error] = "You cannot delete another person's notification."
redirect_back_or :root
end
end
end
class SearchNotification < ActiveRecord::Base
belongs_to :user
validates_presence_of :user_id
validates_presence_of :query
validates_uniqueness_of :query, :scope => :user_id
end
class SearchNotificationsController < ApplicationController
before_filter :require_user
before_filter :require_notification_owner_or_admin, :only => [:edit, :destroy]
def ssl_allowed?
true
end
def create
unless params[:search_notification][:query].strip.blank?
@search_notification = SearchNotification.new(params[:search_notification])
@search_notification.user = current_user
@search_notification.query.strip!
@search_notification.query.downcase!
if @search_notification.save
flash[:notice] = "You will now get notifications for '#{params[:search_notification][:query]}'"
redirect_to edit_my_profile_users_path
else
if SearchNotification.find_by_query(params[:search_notification][:query])
flash[:warning] = "You've already got a notification setup for that. You're good to go!"
else
flash[:error] = "An error occured"
end
redirect_back_or :root
end
else
redirect_back_or :root
end
end
def update
@search_notification = SearchNotification.find(params[:id])
if @search_notification.update_attributes(params[:search_notification])
flash[:notice] = "Notification updated"
else
flash[:error] = "Error"
end
redirect_back_or :root
end
def destroy
@search_notification = SearchNotification.find(params[:id])
flash[:notice] = "You will no longer get new notifications for '#{@search_notification.query}'"
@search_notification.delete
redirect_back_or :root
end
private
def require_notification_owner_or_admin
unless current_user.id == SearchNotification.find(params[:id]).user_id || current_user.has_admin_rights
flash[:error] = "You don't have access to that."
redirect_back_or :root
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment