end
# Subscribe user to diary comments
- @diary_entry.subscribers << @user
+ @diary_entry.subscriptions.create(user: @user)
redirect_to :controller => "diary_entry", :action => "list", :display_name => @user.display_name
else
end
# Add the commenter to the subscribers if necessary
- @entry.subscribers << @user unless @entry.subscribers.exists?(@user.id)
+ @entry.subscriptions.create(user: @user) unless @entry.subscribers.exists?(@user)
redirect_to :controller => "diary_entry", :action => "view", :display_name => @entry.user.display_name, :id => @entry.id
else
def subscribe
diary_entry = DiaryEntry.find(params[:id])
- if ! diary_entry.subscribers.exists?(@user.id)
- diary_entry.subscribers << @user
- end
+ diary_entry.subscriptions.create(user: @user) unless diary_entry.subscribers.exists?(@user)
redirect_to :controller => "diary_entry", :action => "view", :display_name => diary_entry.user.display_name, :id => diary_entry.id
rescue ActiveRecord::RecordNotFound
def unsubscribe
diary_entry = DiaryEntry.find(params[:id])
- if diary_entry.subscribers.exists?(@user.id)
- diary_entry.subscribers.delete(@user)
- end
+ diary_entry.subscriptions.where(user: @user).delete_all if diary_entry.subscribers.exists?(@user)
redirect_to :controller => "diary_entry", :action => "view", :display_name => diary_entry.user.display_name, :id => diary_entry.id
rescue ActiveRecord::RecordNotFound
has_many :comments, -> { order(:id).preload(:user) }, :class_name => "DiaryComment"
has_many :visible_comments, -> { joins(:user).where(:visible => true, :users => { :status => %w(active confirmed) }).order(:id) }, :class_name => "DiaryComment"
- has_and_belongs_to_many :subscribers, :class_name => "User", :join_table => "diary_entries_subscribers", :association_foreign_key => "subscriber_id"
+ has_many :subscriptions, :class_name => 'DiaryEntrySubscription'
+ has_many :subscribers, :through => :subscriptions, :source => :user
scope :visible, -> { where(:visible => true) }
--- /dev/null
+class DiaryEntrySubscription < ActiveRecord::Base
+ belongs_to :user
+ belongs_to :diary_entry
+end
has_many :traces, -> { where(:visible => true) }
has_many :diary_entries, -> { order(:created_at => :desc) }
has_many :diary_comments, -> { order(:created_at => :desc) }
- has_and_belongs_to_many :diary_entries_subscriptions, :class_name => "DiaryEntry", :join_table => "diary_entries_subscribers", :foreign_key => "subscriber_id"
+ has_many :diary_entry_subscriptions, :class_name => 'DiaryEntrySubscription'
+ has_many :diary_subscriptions, :through => :diary_entry_subscriptions, :source => :diary_entry
has_many :messages, -> { where(:to_user_visible => true).order(:sent_on => :desc).preload(:sender, :recipient) }, :foreign_key => :to_user_id
has_many :new_messages, -> { where(:to_user_visible => true, :message_read => false).order(:sent_on => :desc) }, :class_name => "Message", :foreign_key => :to_user_id
has_many :sent_messages, -> { where(:from_user_visible => true).order(:sent_on => :desc).preload(:sender, :recipient) }, :class_name => "Message", :foreign_key => :from_user_id
<%= if_administrator(:li) do %>
<%= link_to t('diary_entry.diary_entry.hide_link'), hide_diary_entry_path(:display_name => diary_entry.user.display_name, :id => diary_entry.id), :method => :post, :data => { :confirm => t('diary_entry.diary_entry.confirm') } %>
<% end %>
-
</ul>
</div>
<%= richtext_area :diary_comment, :body, :cols => 80, :rows => 15 %>
<%= submit_tag t('diary_entry.view.save_button') %>
<% end %>
- <% if @user and @entry.subscribers.exists?(@user.id) %>
+ <% if @user and @entry.subscribers and @entry.subscribers.exists?(@user) %>
<div style='position:relative; top: -30px; left: 130px'><%= link_to t('javascripts.changesets.show.unsubscribe'), diary_entry_unsubscribe_path(:display_name => @entry.user.display_name, :id => @entry.id), :method => :post, :class => :button %></div>
<% elsif @user %>
<div style='position:relative; top: -30px; left: 130px'><%= link_to t('javascripts.changesets.show.subscribe'), diary_entry_subscribe_path(:display_name => @entry.user.display_name, :id => @entry.id), :method => :post, :class => :button %></div>
class AddJoinTableBetweenUsersAndDiaryEntries < ActiveRecord::Migration
def change
- create_table :diary_entries_subscribers, :id => false do |t|
- t.column :subscriber_id, :bigint, :null => false
+ create_table :diary_entry_subscriptions, :id => false do |t|
+ t.column :user_id, :bigint, :null => false
t.column :diary_entry_id, :bigint, :null => false
end
- add_foreign_key :diary_entries_subscribers, :users, :column => :subscriber_id, :name => "diary_entries_subscribers_subscriber_id_fkey"
- add_foreign_key :diary_entries_subscribers, :diary_entries, :column => :diary_entry_id, :name => "diary_entries_subscribers_changeset_id_fkey"
-
- add_index :diary_entries_subscribers, [:subscriber_id, :diary_entry_id], :unique => true, :name => "index_diary_subscribers_on_subscriber_id_and_diary_id"
- add_index :diary_entries_subscribers, [:diary_entry_id]
+ add_index :diary_entry_subscriptions, [:user_id, :diary_entry_id], :unique => true, :name => "index_diary_subscriptions_on_user_id_and_diary_entry_id"
+ add_index :diary_entry_subscriptions, [:diary_entry_id]
end
def up
DiaryEntry.find_each do |diary_entry|
- diary_entry.subscribers << diary_entry.user unless diary_entry.subscribers.exists?(diary_entry.user.id)
+ diary_entry.subscriptions.create(user: diary_entry.user) unless diary_entry.subscribers.exists?(@user)
end
end