hillam/ctt-server

implement notifications

Closed this issue · 2 comments

  • Site has many notifications.
  • Notification belongs to site.

Notification:

"I would like to be notified if I accrue 'time' seconds over a span of 'span' seconds."

  • time:integer - amount of time in seconds the notification requires
  • sent:integer - time when this notification was last sent; initialized to 'now' upon 'create'
  • span:integer - time span for reaching the target time accrual

Conditions to notify:

One condition must be met before we can check the next one.

  1. The given site exists for current user.
  2. Time.now - span > sent
  3. site.entries since sent sums to > time

Steps:

  • Generate and run a migration for Notifications:

    $ rails g model CreateNotifications time:integer sent:integer span:integer
    $ rake db:migrate
    
  • Establish the relationship to the Site model:

    # in site.rb
    has_many :notifications, dependent: :destroy
    
    # in notification.rb
    belongs_to :site
  • Generate a controller for Notifications:

    $ rails g controller Notifications send create index destroy
    
  • Define notification routes:

    # in routes.rb
    get 'notifications',      to: 'notifications#index'
    post 'notifications',     to: 'notifications#create'
    post 'notifications/:id', to: 'notifications#destroy'
  • Generate a mailer for Notifications:

    $ rails g mailer NotificationsMailer
    
  • Set up the mailer.

    • rake g mailer NotificationsMailer
  • Redirect to notification#send from sites#update if the above conditions to notify are met.

Problem after ec7c63a:

NoMethodError (undefined method `entries' for #<Notification:0x000000054b4c68>):
  app/controllers/sites_controller.rb:24:in `block (2 levels) in update'
  app/controllers/sites_controller.rb:23:in `block in update'
  app/controllers/sites_controller.rb:17:in `update'

In sites_controller.rb, I refer to noti.entries when checking whether to notify. I need to do sites.entires instead.

notifications = site.notifications.where('sent + span < ?', Time.now.to_i)
notifications.each do |noti|
    if noti.entries.where('created_at > ?', Time.at(noti.sent))
        redirect_to notifications_notify(id: noti.id)
    end
end
# mistake..
if noti.entries.where('created_at > ?', Time.at(noti.sent))
# should be 
if noti.entries.where('created_at > ?', Time.at(noti.sent)) > noti.time

Fixed with 7046157