From: Mikel Maron Date: Fri, 7 Oct 2016 00:31:10 +0000 (-0400) Subject: more idiomatic models for diary entry subscriptions X-Git-Tag: live~4336^2~12 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/3c01d2e80dc0e5b6942eb7b965c991f5eba5fef2 more idiomatic models for diary entry subscriptions --- diff --git a/app/controllers/diary_entry_controller.rb b/app/controllers/diary_entry_controller.rb index 07980adc8..90a08615c 100644 --- a/app/controllers/diary_entry_controller.rb +++ b/app/controllers/diary_entry_controller.rb @@ -26,7 +26,7 @@ class DiaryEntryController < ApplicationController 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 @@ -70,7 +70,7 @@ class DiaryEntryController < ApplicationController 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 @@ -83,9 +83,7 @@ class DiaryEntryController < ApplicationController 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 @@ -95,9 +93,7 @@ class DiaryEntryController < ApplicationController 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 diff --git a/app/models/diary_entry.rb b/app/models/diary_entry.rb index 07bb60a10..48f3e69bd 100644 --- a/app/models/diary_entry.rb +++ b/app/models/diary_entry.rb @@ -4,7 +4,8 @@ class DiaryEntry < ActiveRecord::Base 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) } diff --git a/app/models/diary_entry_subscription.rb b/app/models/diary_entry_subscription.rb new file mode 100644 index 000000000..b0a563ea8 --- /dev/null +++ b/app/models/diary_entry_subscription.rb @@ -0,0 +1,4 @@ +class DiaryEntrySubscription < ActiveRecord::Base + belongs_to :user + belongs_to :diary_entry +end diff --git a/app/models/user.rb b/app/models/user.rb index 22cdfabdc..c3e02e0ab 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,7 +4,8 @@ class User < ActiveRecord::Base 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 diff --git a/app/views/diary_entry/_diary_entry.html.erb b/app/views/diary_entry/_diary_entry.html.erb index db15e39cd..410e13047 100644 --- a/app/views/diary_entry/_diary_entry.html.erb +++ b/app/views/diary_entry/_diary_entry.html.erb @@ -34,6 +34,5 @@ <%= 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 %> - diff --git a/app/views/diary_entry/view.html.erb b/app/views/diary_entry/view.html.erb index 657156474..67ca96e72 100644 --- a/app/views/diary_entry/view.html.erb +++ b/app/views/diary_entry/view.html.erb @@ -21,7 +21,7 @@ <%= 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) %>
<%= link_to t('javascripts.changesets.show.unsubscribe'), diary_entry_unsubscribe_path(:display_name => @entry.user.display_name, :id => @entry.id), :method => :post, :class => :button %>
<% elsif @user %>
<%= link_to t('javascripts.changesets.show.subscribe'), diary_entry_subscribe_path(:display_name => @entry.user.display_name, :id => @entry.id), :method => :post, :class => :button %>
diff --git a/db/migrate/20161002153425_add_join_table_between_users_and_diary_entries.rb b/db/migrate/20161002153425_add_join_table_between_users_and_diary_entries.rb index cd8414ee7..b99dbbdb8 100644 --- a/db/migrate/20161002153425_add_join_table_between_users_and_diary_entries.rb +++ b/db/migrate/20161002153425_add_join_table_between_users_and_diary_entries.rb @@ -1,20 +1,17 @@ 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