more idiomatic models for diary entry subscriptions
This commit is contained in:
parent
44b08cc35d
commit
3c01d2e80d
7 changed files with 18 additions and 20 deletions
|
@ -26,7 +26,7 @@ class DiaryEntryController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
# Subscribe user to diary comments
|
# 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
|
redirect_to :controller => "diary_entry", :action => "list", :display_name => @user.display_name
|
||||||
else
|
else
|
||||||
|
@ -70,7 +70,7 @@ class DiaryEntryController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
# Add the commenter to the subscribers if necessary
|
# 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
|
redirect_to :controller => "diary_entry", :action => "view", :display_name => @entry.user.display_name, :id => @entry.id
|
||||||
else
|
else
|
||||||
|
@ -83,9 +83,7 @@ class DiaryEntryController < ApplicationController
|
||||||
def subscribe
|
def subscribe
|
||||||
diary_entry = DiaryEntry.find(params[:id])
|
diary_entry = DiaryEntry.find(params[:id])
|
||||||
|
|
||||||
if ! diary_entry.subscribers.exists?(@user.id)
|
diary_entry.subscriptions.create(user: @user) unless diary_entry.subscribers.exists?(@user)
|
||||||
diary_entry.subscribers << @user
|
|
||||||
end
|
|
||||||
|
|
||||||
redirect_to :controller => "diary_entry", :action => "view", :display_name => diary_entry.user.display_name, :id => diary_entry.id
|
redirect_to :controller => "diary_entry", :action => "view", :display_name => diary_entry.user.display_name, :id => diary_entry.id
|
||||||
rescue ActiveRecord::RecordNotFound
|
rescue ActiveRecord::RecordNotFound
|
||||||
|
@ -95,9 +93,7 @@ class DiaryEntryController < ApplicationController
|
||||||
def unsubscribe
|
def unsubscribe
|
||||||
diary_entry = DiaryEntry.find(params[:id])
|
diary_entry = DiaryEntry.find(params[:id])
|
||||||
|
|
||||||
if diary_entry.subscribers.exists?(@user.id)
|
diary_entry.subscriptions.where(user: @user).delete_all if diary_entry.subscribers.exists?(@user)
|
||||||
diary_entry.subscribers.delete(@user)
|
|
||||||
end
|
|
||||||
|
|
||||||
redirect_to :controller => "diary_entry", :action => "view", :display_name => diary_entry.user.display_name, :id => diary_entry.id
|
redirect_to :controller => "diary_entry", :action => "view", :display_name => diary_entry.user.display_name, :id => diary_entry.id
|
||||||
rescue ActiveRecord::RecordNotFound
|
rescue ActiveRecord::RecordNotFound
|
||||||
|
|
|
@ -4,7 +4,8 @@ class DiaryEntry < ActiveRecord::Base
|
||||||
|
|
||||||
has_many :comments, -> { order(:id).preload(:user) }, :class_name => "DiaryComment"
|
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_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) }
|
scope :visible, -> { where(:visible => true) }
|
||||||
|
|
||||||
|
|
4
app/models/diary_entry_subscription.rb
Normal file
4
app/models/diary_entry_subscription.rb
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
class DiaryEntrySubscription < ActiveRecord::Base
|
||||||
|
belongs_to :user
|
||||||
|
belongs_to :diary_entry
|
||||||
|
end
|
|
@ -4,7 +4,8 @@ class User < ActiveRecord::Base
|
||||||
has_many :traces, -> { where(:visible => true) }
|
has_many :traces, -> { where(:visible => true) }
|
||||||
has_many :diary_entries, -> { order(:created_at => :desc) }
|
has_many :diary_entries, -> { order(:created_at => :desc) }
|
||||||
has_many :diary_comments, -> { 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 :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 :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
|
has_many :sent_messages, -> { where(:from_user_visible => true).order(:sent_on => :desc).preload(:sender, :recipient) }, :class_name => "Message", :foreign_key => :from_user_id
|
||||||
|
|
|
@ -34,6 +34,5 @@
|
||||||
<%= if_administrator(:li) do %>
|
<%= 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') } %>
|
<%= 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 %>
|
<% end %>
|
||||||
|
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -21,7 +21,7 @@
|
||||||
<%= richtext_area :diary_comment, :body, :cols => 80, :rows => 15 %>
|
<%= richtext_area :diary_comment, :body, :cols => 80, :rows => 15 %>
|
||||||
<%= submit_tag t('diary_entry.view.save_button') %>
|
<%= submit_tag t('diary_entry.view.save_button') %>
|
||||||
<% end %>
|
<% 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>
|
<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 %>
|
<% 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>
|
<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>
|
||||||
|
|
|
@ -1,20 +1,17 @@
|
||||||
class AddJoinTableBetweenUsersAndDiaryEntries < ActiveRecord::Migration
|
class AddJoinTableBetweenUsersAndDiaryEntries < ActiveRecord::Migration
|
||||||
def change
|
def change
|
||||||
create_table :diary_entries_subscribers, :id => false do |t|
|
create_table :diary_entry_subscriptions, :id => false do |t|
|
||||||
t.column :subscriber_id, :bigint, :null => false
|
t.column :user_id, :bigint, :null => false
|
||||||
t.column :diary_entry_id, :bigint, :null => false
|
t.column :diary_entry_id, :bigint, :null => false
|
||||||
end
|
end
|
||||||
|
|
||||||
add_foreign_key :diary_entries_subscribers, :users, :column => :subscriber_id, :name => "diary_entries_subscribers_subscriber_id_fkey"
|
add_index :diary_entry_subscriptions, [:user_id, :diary_entry_id], :unique => true, :name => "index_diary_subscriptions_on_user_id_and_diary_entry_id"
|
||||||
add_foreign_key :diary_entries_subscribers, :diary_entries, :column => :diary_entry_id, :name => "diary_entries_subscribers_changeset_id_fkey"
|
add_index :diary_entry_subscriptions, [:diary_entry_id]
|
||||||
|
|
||||||
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]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def up
|
def up
|
||||||
DiaryEntry.find_each do |diary_entry|
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue