adds counter cache for user's notifications

This commit is contained in:
Juanjo Bazán
2016-01-08 14:39:34 +01:00
parent 704a038795
commit 432e9e0d5b
4 changed files with 10 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
class Notification < ActiveRecord::Base
belongs_to :user
belongs_to :user, counter_cache: true
belongs_to :notifiable, polymorphic: true
scope :unread, -> { all }

View File

@@ -2,9 +2,9 @@
<% if user_signed_in? %>
<li>
<%= link_to notifications_path, class: "notifications" do %>
<% if current_user.notifications.count > 0 %>
<% if current_user.notifications_count > 0 %>
<i class="icon-circle"></i>
<i class="icon-notification" title="<%= t('layouts.header.new_notifications', count: current_user.notifications.count).html_safe %>">
<i class="icon-notification" title="<%= t('layouts.header.new_notifications', count: current_user.notifications_count).html_safe %>">
</i>
<% else %>
<i class="icon-no-notification" title="<%= t('layouts.header.no_notifications') %>"></i>

View File

@@ -0,0 +1,5 @@
class AddNotificationsCounterCacheToUser < ActiveRecord::Migration
def change
add_column :users, :notifications_count, :integer, default: 0
end
end

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160108114750) do
ActiveRecord::Schema.define(version: 20160108133501) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -339,6 +339,7 @@ ActiveRecord::Schema.define(version: 20160108114750) do
t.datetime "erased_at"
t.boolean "public_activity", default: true
t.boolean "newsletter", default: false
t.integer "notifications_count", default: 0
end
add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true, using: :btree