27 lines
677 B
Ruby
27 lines
677 B
Ruby
module JamRuby
|
|
class BroadcastNotification < ActiveRecord::Base
|
|
|
|
attr_accessible :title, :message, :button_label, :frequency
|
|
has_many :user_views, class_name: 'JamRuby::BroadcastNotificationView', dependent: :destroy
|
|
|
|
def frequency_distribution
|
|
BroadcastNotificationView
|
|
.where(broadcast_notification_id: self.id)
|
|
.group(:view_count)
|
|
.count
|
|
end
|
|
|
|
def did_view(user)
|
|
bnv = BroadcastNotificationView
|
|
.where(broadcast_notification_id: self.id, user_id: user.id)
|
|
.limit(1)
|
|
.first
|
|
bnv = user_views.new(user: user) unless bnv
|
|
bnv.view_count += 1
|
|
bnv.save
|
|
bnv
|
|
end
|
|
|
|
end
|
|
end
|