commit: c2af1381130caae0acc02db853580a2bdab61078
parent: fb8aa2b3bac122f819b07c7f9461b26cc369c621
Author: nullkal <nullkal@nil.nu>
Date: Sat, 26 Aug 2017 01:50:52 +0900
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
Diffstat:
6 files changed, 27 insertions(+), 3 deletions(-)
diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb
@@ -14,7 +14,7 @@ class AccountsController < ApplicationController
return
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 = cache_collection(@statuses, Status)
@next_url = next_url unless @statuses.empty?
diff --git a/app/models/account.rb b/app/models/account.rb
@@ -79,7 +79,7 @@ class Account < ApplicationRecord
# Pinned statuses
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
has_many :media_attachments, dependent: :destroy
diff --git a/app/models/status_pin.rb b/app/models/status_pin.rb
@@ -6,6 +6,8 @@
# id :integer not null, primary key
# account_id :integer not null
# status_id :integer not null
+# created_at :datetime not null
+# updated_at :datetime not null
#
class StatusPin < ApplicationRecord
diff --git a/db/migrate/20170824103029_add_timestamps_to_status_pins.rb b/db/migrate/20170824103029_add_timestamps_to_status_pins.rb
@@ -0,0 +1,5 @@
+class AddTimestampsToStatusPins < ActiveRecord::Migration[5.1]
+ def change
+ add_timestamps :status_pins, null: false, default: -> { 'CURRENT_TIMESTAMP' }
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
@@ -10,7 +10,7 @@
#
# 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
enable_extension "plpgsql"
@@ -285,6 +285,8 @@ ActiveRecord::Schema.define(version: 20170823162448) do
create_table "status_pins", force: :cascade do |t|
t.bigint "account_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"], name: "index_status_pins_on_account_id"
t.index ["status_id"], name: "index_status_pins_on_status_id"
diff --git a/spec/controllers/accounts_controller_spec.rb b/spec/controllers/accounts_controller_spec.rb
@@ -10,6 +10,13 @@ RSpec.describe AccountsController, type: :controller do
let!(:status2) { Status.create!(account: alice, text: 'Boop', thread: status1) }
let!(:status3) { Status.create!(account: alice, text: 'Picture!') }
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
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
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
expect(response).to have_http_status(:success)
end