Skip to content

Commit

Permalink
add edit reason when editing a post
Browse files Browse the repository at this point in the history
  • Loading branch information
ZogStriP committed Nov 15, 2013
1 parent 674887d commit 482b752
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,10 @@ Discourse.ComposerController = Discourse.Controller.extend({
archetype: this.get('model.archetype'),
metaData: this.get('model.metaData')
})) : void 0;
}
});
},

canEdit: function() {
return this.get("model.action") === "edit" && Discourse.User.current().get("can_edit");
}.property("model.action")

});
1 change: 1 addition & 0 deletions app/assets/javascripts/discourse/models/composer.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ Discourse.Composer = Discourse.Model.extend({

post.setProperties({
raw: this.get('reply'),
editReason: this.get('editReason'),
imageSizes: opts.imageSizes,
cooked: $('#wmd-preview').html()
});
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/discourse/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ Discourse.Post = Discourse.Model.extend({
return Discourse.ajax("/posts/" + (this.get('id')), {
type: 'PUT',
data: {
post: { raw: this.get('raw') },
post: { raw: this.get('raw'), edit_reason: this.get('editReason') },
image_sizes: this.get('imageSizes')
}
}).then(function(result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
{{/if}}

<div class="title-input">
{{textField value=model.title tabindex="2" id="reply-title" maxlength="255" class="span8" placeholderKey="composer.title_placeholder"}}
{{textField value=model.title tabindex="2" id="reply-title" maxlength="255" placeholderKey="composer.title_placeholder"}}
{{popupInputTip validation=view.titleValidation shownAt=view.showTitleTip}}
</div>

Expand All @@ -39,6 +39,11 @@
{{#if model.showAdminOptions}}
<button {{action toggleAdminOptions target="view"}} class="btn no-text" title='{{i18n composer.admin_options_title}}'><i class="icon icon-wrench"></i></button>
{{/if}}
{{#if canEdit}}
<div class="edit-reason-input">
{{textField value=model.editReason tabindex="5" id="edit-reason" maxlength="255" placeholderKey="composer.edit_reason_placeholder"}}
</div>
{{/if}}
{{/unless}}
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
optionValuePath="content.number"
selectionBinding="versionRight"}}

{{#if postRight.edit_reason}}
<p><strong>{{i18n post.edit_reason}}</strong>{{postRight.edit_reason}}</p>
{{/if}}

<div class='contents'>
{{#if diff}}
{{{diff}}}
Expand All @@ -41,4 +45,4 @@
{{/if}}
{{/if}}

</div>
</div>
11 changes: 5 additions & 6 deletions app/assets/stylesheets/desktop/compose.scss
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,12 @@
height: 400px;
}
.contents {
input#reply-title {
input#reply-title, input#edit-reason {
padding: 7px 10px;
margin: 6px 10px 3px 0;
width: 400px;

}
input#reply-title { width: 400px; }
input#edit-reason { width: 200px; }
.wmd-controls {
@include transition(top 0.3s ease);
top: 100px;
Expand Down Expand Up @@ -283,7 +283,7 @@
}
}
}
#reply-title {
#reply-title, #edit-reason {
margin-right: 10px;
float: left;
&:disabled {
Expand Down Expand Up @@ -324,7 +324,7 @@
bottom: 8px;
}
}
.title-input, .category-input {
.title-input, .category-input, .edit-reason-input {
position: relative;
display: inline;
}
Expand Down Expand Up @@ -357,7 +357,6 @@ div.ac-wrap {
background-color: $white;
border: 1px solid #cccccc;
padding: 5px 10px;
@include border-radius-all(3px);
div.item {
float: left;
margin-right: 10px;
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def update
end

revisor = PostRevisor.new(post)
if revisor.revise!(current_user, params[:post][:raw])
if revisor.revise!(current_user, params[:post][:raw], edit_reason: params[:post][:edit_reason])
TopicLink.extract_from(post)
end

Expand Down
7 changes: 5 additions & 2 deletions app/jobs/regular/pull_hotlinked_images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def execute(args)
# have we already downloaded that file?
if !downloaded_urls.include?(src)
hotlinked = download(src)
if hotlinked.size <= @max_size
if hotlinked.try(:size) <= @max_size
filename = File.basename(URI.parse(src).path)
file = ActionDispatch::Http::UploadedFile.new(tempfile: hotlinked, filename: filename)
upload = Upload.create_for(post.user_id, file, hotlinked.size, src)
Expand Down Expand Up @@ -64,7 +64,10 @@ def execute(args)

# TODO: make sure the post hasn´t changed while we were downloading remote images
if raw != post.raw
options = { force_new_version: true }
options = {
force_new_version: true,
edit_reason: I18n.t("upload.edit_reason")
}
post.revise(Discourse.system_user, raw, options)
end

Expand Down
17 changes: 9 additions & 8 deletions app/serializers/post_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
class PostSerializer < BasicPostSerializer

# To pass in additional information we might need
attr_accessor :topic_slug
attr_accessor :topic_view
attr_accessor :parent_post
attr_accessor :add_raw
attr_accessor :single_post_link_counts
attr_accessor :draft_sequence
attr_accessor :post_actions
attr_accessor :topic_slug,
:topic_view,
:parent_post,
:add_raw,
:single_post_link_counts,
:draft_sequence,
:post_actions

attributes :post_number,
:post_type,
Expand Down Expand Up @@ -43,7 +43,8 @@ class PostSerializer < BasicPostSerializer
:trust_level,
:deleted_at,
:deleted_by,
:user_deleted
:user_deleted,
:edit_reason


def moderator?
Expand Down
2 changes: 2 additions & 0 deletions config/locales/client.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ en:

users_placeholder: "Add a user"
title_placeholder: "Type your title here. What is this discussion about in one brief sentence?"
edit_reason_placeholder: "Short reason of your edit"
reply_placeholder: "Type here. Use Markdown or BBCode to format. Drag or paste an image to upload it."
view_new_post: "View your new post."
saving: "Saving..."
Expand Down Expand Up @@ -798,6 +799,7 @@ en:
reply_topic: "Reply to {{link}}"
quote_reply: "quote reply"
edit: "Editing {{link}} by {{replyAvatar}} {{username}}"
edit_reason: "Reason: "
post_number: "post {{number}}"
in_reply_to: "in reply to"
last_edited_on: "post last edited on"
Expand Down
1 change: 1 addition & 0 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,7 @@ en:
deleted: 'deleted'

upload:
edit_reason: "We have downloaded a copy of the remotes images"
unauthorized: "Sorry, the file you are trying to upload is not authorized (authorized extensions: %{authorized_extensions})."
pasted_image_filename: "Pasted image"
attachments:
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20131115165105_add_edit_reason_to_posts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddEditReasonToPosts < ActiveRecord::Migration
def change
add_column :posts, :edit_reason, :string
end
end
1 change: 1 addition & 0 deletions lib/post_revisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def update_post
@post.raw = @new_raw
@post.updated_by = @user
@post.last_editor_id = @user.id
@post.edit_reason = @opts[:edit_reason] if @opts[:edit_reason]

if @post.hidden && @post.hidden_reason_id == Post.hidden_reasons[:flag_threshold_reached]
@post.hidden = false
Expand Down
15 changes: 11 additions & 4 deletions spec/controllers/posts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,23 @@

let(:post) { Fabricate(:post, user: log_in) }
let(:update_params) do
{id: post.id,
post: {raw: 'edited body'},
image_sizes: {'http://image.com/image.jpg' => {'width' => 123, 'height' => 456}}}
{
id: post.id,
post: { raw: 'edited body', edit_reason: 'typo' },
image_sizes: { 'http://image.com/image.jpg' => {'width' => 123, 'height' => 456} },
}
end

it 'passes the image sizes through' do
Post.any_instance.expects(:image_sizes=)
xhr :put, :update, update_params
end

it 'passes the edit reason through' do
Post.any_instance.expects(:edit_reason=)
xhr :put, :update, update_params
end

it "raises an error when the post parameter is missing" do
update_params.delete(:post)
lambda {
Expand All @@ -241,7 +248,7 @@
end

it "calls revise with valid parameters" do
PostRevisor.any_instance.expects(:revise!).with(post.user, 'edited body')
PostRevisor.any_instance.expects(:revise!).with(post.user, 'edited body', edit_reason: 'typo')
xhr :put, :update, update_params
end

Expand Down

0 comments on commit 482b752

Please sign in to comment.