Keep published_at when updating author of a scheduled article from the dashboard (#18653)
This commit is contained in:
parent
3f26694976
commit
4d413af4cc
5 changed files with 55 additions and 6 deletions
|
|
@ -159,6 +159,7 @@ class ArticlesController < ApplicationController
|
|||
def update
|
||||
authorize @article
|
||||
@user = @article.user || current_user
|
||||
|
||||
updated = Articles::Updater.call(@user, @article, article_params_json)
|
||||
|
||||
respond_to do |format|
|
||||
|
|
@ -327,6 +328,12 @@ class ArticlesController < ApplicationController
|
|||
allowed_params << :organization_id
|
||||
end
|
||||
|
||||
manage_published_at_params
|
||||
|
||||
@article_params_json = params.require(:article).permit(allowed_params)
|
||||
end
|
||||
|
||||
def manage_published_at_params
|
||||
time_zone_str = params["article"].delete("timezone")
|
||||
|
||||
time = params["article"].delete("published_at_time")
|
||||
|
|
@ -336,11 +343,9 @@ class ArticlesController < ApplicationController
|
|||
time_zone = Time.find_zone(time_zone_str)
|
||||
time_zone ||= Time.find_zone("UTC")
|
||||
params["article"]["published_at"] = time_zone.parse("#{date} #{time}")
|
||||
elsif params["article"]["version"] != "v1"
|
||||
elsif params["article"]["version"] != "v1" && !params["article"]["from_dashboard"]
|
||||
params["article"]["published_at"] = nil
|
||||
end
|
||||
|
||||
@article_params_json = params.require(:article).permit(allowed_params)
|
||||
end
|
||||
|
||||
def allowed_to_change_org_id?
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ module Articles
|
|||
|
||||
attrs = Articles::Attributes.new(article_params, article.user)
|
||||
.for_update(update_edited_at: update_edited_at)
|
||||
|
||||
success = article.update(attrs)
|
||||
if success
|
||||
user.rate_limiter.track_limit_by_action(:article_update)
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@
|
|||
<div id="ellipsis-menu-dropdown-<%= article.id %>" class="crayons-dropdown top-100 right-0 z-10 align-left js-ellipsis-menu-dropdown">
|
||||
<%= form_for(article, method: :patch, remote: true, authenticity_token: true, html: { "data-type": "json", class: "p-0 js-archive-toggle" }) do |f| %>
|
||||
<%= f.hidden_field :archived, value: !article.archived %>
|
||||
|
||||
<%= f.hidden_field :from_dashboard, value: 1 %>
|
||||
<button type="submit" class="crayons-link crayons-link--block w-100 border-0 bg-transparent">
|
||||
<%= t("views.dashboard.article.#{article.archived ? 'unarchive' : 'archive'}") %>
|
||||
</button>
|
||||
|
|
@ -98,6 +98,7 @@
|
|||
</div>
|
||||
<% if organization && org_admin %>
|
||||
<%= form_for(article) do |f| %>
|
||||
<%= f.hidden_field :from_dashboard, value: 1 %>
|
||||
<input type="hidden" name="destination" value="/dashboard/organization/<%= organization.id %>" />
|
||||
<%= t("views.dashboard.article.author_is") %><%= f.select(:user_id, options_for_select(organization.users.pluck(:name, :id), article.user_id)) %>
|
||||
<%= f.submit t("views.dashboard.article.submit"), class: "crayons-btn crayons-btn--secondary" %>
|
||||
|
|
|
|||
|
|
@ -107,19 +107,19 @@
|
|||
<a href="<%= article.path %>/stats" class="crayons-link crayons-link--block w-100"><%= t("views.dashboard.article.stats") %></a>
|
||||
<%= form_for(article, method: :patch, remote: true, authenticity_token: true, html: { "data-type": "json", class: "js-archive-toggle" }) do |f| %>
|
||||
<%= f.hidden_field :archived, value: !article.archived, id: "article_archived_#{article.id}" %>
|
||||
<%= f.hidden_field :from_dashboard, value: 1 %>
|
||||
|
||||
<button type="submit" class="crayons-link crayons-link--block w-100 border-0 bg-transparent">
|
||||
<%= t("views.dashboard.article.#{article.archived ? 'unarchive' : 'archive'}") %>
|
||||
</button>
|
||||
<% end %>
|
||||
|
||||
<% if organization && org_admin %>
|
||||
<%= form_for(article, html: { class: "mt-4 pt-4 border-0 border-t-1 border-color-base-10 border-solid" }) do |f| %>
|
||||
<input type="hidden" name="destination" value="/dashboard/organization/<%= organization.id %>" />
|
||||
<div class="crayons-field mb-4">
|
||||
<label for="user_id" class="crayons-field__label"><%= t("views.dashboard.article.author") %></label>
|
||||
<%= f.select :user_id, options_for_select(organization.users.pluck(:name, :id), article.user_id), {}, { class: "crayons-select" } %>
|
||||
|
||||
<%= f.hidden_field :from_dashboard, value: 1 %>
|
||||
</div>
|
||||
<button type="submit" class="crayons-btn"><%= t("views.dashboard.article.save") %></button>
|
||||
<% end %>
|
||||
|
|
|
|||
|
|
@ -271,4 +271,46 @@ RSpec.describe "ArticlesUpdate", type: :request do
|
|||
expect(article.published_at).to be_within(1.minute).of(DateTime.parse(date_was))
|
||||
end
|
||||
end
|
||||
|
||||
context "when changing an author inside an organization" do
|
||||
before do
|
||||
user.add_role(:admin)
|
||||
user.organization_memberships.create(organization: organization, type_of_user: "admin")
|
||||
end
|
||||
|
||||
let(:draft) { create(:article, user: user2, published: false, organization: organization) }
|
||||
let(:scheduled_article) do
|
||||
create(:article, user: user2, published_at: 2.days.from_now, published: false, organization: organization)
|
||||
end
|
||||
|
||||
it "changes an author" do
|
||||
put "/articles/#{draft.id}", params: {
|
||||
article: { user_id: user.id, from_dashboard: true }
|
||||
}
|
||||
draft.reload
|
||||
expect(draft.user_id).to eq(user.id)
|
||||
end
|
||||
|
||||
it "doesn't remove the published_at when changing author for a scheduled draft article" do
|
||||
published_at_was = scheduled_article.published_at
|
||||
put "/articles/#{scheduled_article.id}", params: {
|
||||
article: { user_id: user.id, from_dashboard: true }
|
||||
}
|
||||
scheduled_article.reload
|
||||
expect(scheduled_article.user_id).to eq(user.id)
|
||||
expect(scheduled_article.published_at).to be_within(1.second).of(published_at_was)
|
||||
end
|
||||
|
||||
it "doesn't remove published_at when changing author for a scheduled article" do
|
||||
scheduled_article.update_column(:published, true)
|
||||
|
||||
published_at_was = scheduled_article.published_at
|
||||
put "/articles/#{scheduled_article.id}", params: {
|
||||
article: { user_id: user.id, from_dashboard: true }
|
||||
}
|
||||
scheduled_article.reload
|
||||
expect(scheduled_article.user_id).to eq(user.id)
|
||||
expect(scheduled_article.published_at).to be_within(1.second).of(published_at_was)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue