diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 14059fb15..39cfd8b2d 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -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? diff --git a/app/services/articles/updater.rb b/app/services/articles/updater.rb index bdedd6620..969ef3ebc 100644 --- a/app/services/articles/updater.rb +++ b/app/services/articles/updater.rb @@ -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) diff --git a/app/views/dashboards/_dashboard_article.html.erb b/app/views/dashboards/_dashboard_article.html.erb index 06c46057e..22665ba87 100644 --- a/app/views/dashboards/_dashboard_article.html.erb +++ b/app/views/dashboards/_dashboard_article.html.erb @@ -16,7 +16,7 @@
<%= 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 %> @@ -98,6 +98,7 @@
<% if organization && org_admin %> <%= form_for(article) do |f| %> + <%= f.hidden_field :from_dashboard, value: 1 %> <%= 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" %> diff --git a/app/views/dashboards/_dashboard_article_row.html.erb b/app/views/dashboards/_dashboard_article_row.html.erb index a1e4078d1..01704e306 100644 --- a/app/views/dashboards/_dashboard_article_row.html.erb +++ b/app/views/dashboards/_dashboard_article_row.html.erb @@ -107,19 +107,19 @@ <%= t("views.dashboard.article.stats") %> <%= 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 %> <% 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| %>
<%= 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 %>
<% end %> diff --git a/spec/requests/articles/articles_update_spec.rb b/spec/requests/articles/articles_update_spec.rb index 365eb8aee..002f39026 100644 --- a/spec/requests/articles/articles_update_spec.rb +++ b/spec/requests/articles/articles_update_spec.rb @@ -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