Allow multiple pinned statuses to be shown and make them be ordered b… (#4690)

* Allow multiple pinned statuses to be shown and make them be ordered by pinned date

* Set timestamps NOT NULL

* Make single-line pinned_statuses

* Spec for pinned_statuses

* Remove redundant empty line
This commit is contained in:
nullkal 2017-08-26 01:50:52 +09:00 committed by Eugen Rochko
parent fb8aa2b3ba
commit c2af138113
6 changed files with 27 additions and 3 deletions

View file

@ -14,7 +14,7 @@ class AccountsController < ApplicationController
return return
end end
@pinned_statuses = cache_collection(@account.pinned_statuses.limit(1), Status) unless media_requested? @pinned_statuses = cache_collection(@account.pinned_statuses, Status) unless media_requested?
@statuses = filtered_statuses.paginate_by_max_id(20, params[:max_id], params[:since_id]) @statuses = filtered_statuses.paginate_by_max_id(20, params[:max_id], params[:since_id])
@statuses = cache_collection(@statuses, Status) @statuses = cache_collection(@statuses, Status)
@next_url = next_url unless @statuses.empty? @next_url = next_url unless @statuses.empty?

View file

@ -79,7 +79,7 @@ class Account < ApplicationRecord
# Pinned statuses # Pinned statuses
has_many :status_pins, inverse_of: :account, dependent: :destroy has_many :status_pins, inverse_of: :account, dependent: :destroy
has_many :pinned_statuses, through: :status_pins, class_name: 'Status', source: :status has_many :pinned_statuses, -> { reorder('status_pins.created_at DESC') }, through: :status_pins, class_name: 'Status', source: :status
# Media # Media
has_many :media_attachments, dependent: :destroy has_many :media_attachments, dependent: :destroy

View file

@ -6,6 +6,8 @@
# id :integer not null, primary key # id :integer not null, primary key
# account_id :integer not null # account_id :integer not null
# status_id :integer not null # status_id :integer not null
# created_at :datetime not null
# updated_at :datetime not null
# #
class StatusPin < ApplicationRecord class StatusPin < ApplicationRecord

View file

@ -0,0 +1,5 @@
class AddTimestampsToStatusPins < ActiveRecord::Migration[5.1]
def change
add_timestamps :status_pins, null: false, default: -> { 'CURRENT_TIMESTAMP' }
end
end

View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170823162448) do ActiveRecord::Schema.define(version: 20170824103029) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -285,6 +285,8 @@ ActiveRecord::Schema.define(version: 20170823162448) do
create_table "status_pins", force: :cascade do |t| create_table "status_pins", force: :cascade do |t|
t.bigint "account_id", null: false t.bigint "account_id", null: false
t.bigint "status_id", null: false t.bigint "status_id", null: false
t.datetime "created_at", default: -> { "now()" }, null: false
t.datetime "updated_at", default: -> { "now()" }, null: false
t.index ["account_id", "status_id"], name: "index_status_pins_on_account_id_and_status_id", unique: true t.index ["account_id", "status_id"], name: "index_status_pins_on_account_id_and_status_id", unique: true
t.index ["account_id"], name: "index_status_pins_on_account_id" t.index ["account_id"], name: "index_status_pins_on_account_id"
t.index ["status_id"], name: "index_status_pins_on_status_id" t.index ["status_id"], name: "index_status_pins_on_status_id"

View file

@ -10,6 +10,13 @@ RSpec.describe AccountsController, type: :controller do
let!(:status2) { Status.create!(account: alice, text: 'Boop', thread: status1) } let!(:status2) { Status.create!(account: alice, text: 'Boop', thread: status1) }
let!(:status3) { Status.create!(account: alice, text: 'Picture!') } let!(:status3) { Status.create!(account: alice, text: 'Picture!') }
let!(:status4) { Status.create!(account: alice, text: 'Mentioning @alice') } let!(:status4) { Status.create!(account: alice, text: 'Mentioning @alice') }
let!(:status5) { Status.create!(account: alice, text: 'Kitsune') }
let!(:status6) { Status.create!(account: alice, text: 'Neko') }
let!(:status7) { Status.create!(account: alice, text: 'Tanuki') }
let!(:status_pin1) { StatusPin.create!(account: alice, status: status5, created_at: 5.days.ago) }
let!(:status_pin2) { StatusPin.create!(account: alice, status: status6, created_at: 2.years.ago) }
let!(:status_pin3) { StatusPin.create!(account: alice, status: status7, created_at: 10.minutes.ago) }
before do before do
status3.media_attachments.create!(account: alice, file: fixture_file_upload('files/attachment.jpg', 'image/jpeg')) status3.media_attachments.create!(account: alice, file: fixture_file_upload('files/attachment.jpg', 'image/jpeg'))
@ -70,6 +77,14 @@ RSpec.describe AccountsController, type: :controller do
expect(statuses[1]).to eq status2 expect(statuses[1]).to eq status2
end end
it 'assigns @pinned_statuses' do
pinned_statuses = assigns(:pinned_statuses).to_a
expect(pinned_statuses.size).to eq 3
expect(pinned_statuses[0]).to eq status7
expect(pinned_statuses[1]).to eq status5
expect(pinned_statuses[2]).to eq status6
end
it 'returns http success' do it 'returns http success' do
expect(response).to have_http_status(:success) expect(response).to have_http_status(:success)
end end