-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New feature added to allow admin users to merge articles #29
base: master
Are you sure you want to change the base?
Changes from 28 commits
8bb6a30
d7d0b79
78077f8
8a40259
8faf73f
a930230
9bd00cd
81adae7
aff0f3a
11f190e
313c535
e1df9bb
6f93e26
1873442
7a45362
27afa80
df00363
3fe45ca
3482ac4
7dda45c
340fa06
a1146ff
8f5a28b
d0e5669
4e08c02
94077bc
6b8b5b6
17484d0
d1f54b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ config/mail.yml | |
*~ | ||
db/*.sqlite* | ||
db/schema.rb | ||
db/db_development | ||
db/db_test | ||
.*.swp | ||
.*.swo | ||
.DS_Store | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ def auto_complete_for_article_keywords | |
|
||
def index | ||
@search = params[:search] ? params[:search] : {} | ||
|
||
@articles = Article.search_with_pagination(@search, {:page => params[:page], :per_page => this_blog.admin_display_elements}) | ||
|
||
if request.xhr? | ||
|
@@ -37,14 +37,32 @@ def edit | |
new_or_edit | ||
end | ||
|
||
def merge | ||
if current_user.admin? | ||
id = params[:id] | ||
merge_id = params[:merge_with][:merge_id] | ||
if merge_id == id | ||
flash[:error] = _("You cannot merge an article with itself") | ||
elsif | ||
Article.exists?(merge_id) && Article.exists?(id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Usually we want these conditions to be on the same line as the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, fixed! |
||
new_article = Article.find(id) | ||
new_article.merge_with(merge_id) | ||
flash[:error] = _("Articles successfully merged!") | ||
else | ||
flash[:error] = _("Article does not exist!") | ||
end | ||
redirect_to :action => :index | ||
end | ||
end | ||
|
||
def destroy | ||
@record = Article.find(params[:id]) | ||
|
||
unless @record.access_by?(current_user) | ||
flash[:error] = _("Error, you are not allowed to perform this action") | ||
return(redirect_to :action => 'index') | ||
end | ||
|
||
return(render 'admin/shared/destroy') unless request.post? | ||
|
||
@record.destroy | ||
|
@@ -77,7 +95,7 @@ def attachment_box_add | |
|
||
def attachment_save(attachment) | ||
begin | ||
Resource.create(:filename => attachment.original_filename, :mime => attachment.content_type.chomp, | ||
Resource.create(:filename => attachment.original_filename, :mime => attachment.content_type.chomp, | ||
:created_at => Time.now).write_to_disk(attachment) | ||
rescue => e | ||
logger.info(e.message) | ||
|
@@ -92,7 +110,7 @@ def autosave | |
@article.text_filter = current_user.text_filter if current_user.simple_editor? | ||
|
||
get_fresh_or_existing_draft_for_article | ||
|
||
@article.attributes = params[:article] | ||
@article.published = false | ||
set_article_author | ||
|
@@ -159,13 +177,13 @@ def new_or_edit | |
@article.keywords = Tag.collection_to_string @article.tags | ||
@article.attributes = params[:article] | ||
# TODO: Consider refactoring, because double rescue looks... weird. | ||
|
||
@article.published_at = DateTime.strptime(params[:article][:published_at], "%B %e, %Y %I:%M %p GMT%z").utc rescue Time.parse(params[:article][:published_at]).utc rescue nil | ||
|
||
if request.post? | ||
set_article_author | ||
save_attachments | ||
|
||
@article.state = "draft" if @article.draft | ||
|
||
if @article.save | ||
|
@@ -234,10 +252,10 @@ def def_build_body | |
@article.body = body[0] | ||
@article.extended = body[1] | ||
end | ||
|
||
end | ||
|
||
def setup_resources | ||
@resources = Resource.by_created_at | ||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
class CategoriesController < GroupingController | ||
# index - inherited | ||
# show - inherited | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,10 +104,10 @@ def last_draft(article_id) | |
end | ||
|
||
def search_with_pagination(search_hash, paginate_hash) | ||
|
||
state = (search_hash[:state] and ["no_draft", "drafts", "published", "withdrawn", "pending"].include? search_hash[:state]) ? search_hash[:state] : 'no_draft' | ||
|
||
|
||
list_function = ["Article.#{state}"] + function_search_no_draft(search_hash) | ||
|
||
if search_hash[:category] and search_hash[:category].to_i > 0 | ||
|
@@ -466,4 +466,18 @@ def self.time_delta(year = nil, month = nil, day = nil) | |
to = to - 1 # pull off 1 second so we don't overlap onto the next day | ||
return from..to | ||
end | ||
|
||
def merge_with(other_article_id) | ||
# find each of the comments associated w/ the article | ||
Article.find(other_article_id).comments.each do |comment| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to associate the comments without iterating through each one? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried this: self.comments += other_article.comments and then saving self.comments, but found that the other_article comments were not actually being saved. Is there another way to reassign the comments without iterating through each one? |
||
# assign their article id to the id of the current article | ||
comment.article_id = self.id | ||
comment.save | ||
end | ||
merged_article = Article.find(other_article_id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like your'e doing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made a variable at the top of the method - merged_article - and called this variable rather than calling Article.find multiple times. |
||
self.body += " \n" + merged_article.body | ||
self.save | ||
merged_article.destroy | ||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,13 @@ | |
<%= render :partial => "form" %> | ||
|
||
<% end %> | ||
|
||
<% if current_user.admin? && [email protected]? %> | ||
<h3>Merge Articles</h3> | ||
<% error_messages_for 'article' %> | ||
<%= form_tag :action=>"merge", :id => @article.id do %> | ||
<label for="merge_with"> <%= _("Article ID")%></label> | ||
<%= text_field :merge_with, :merge_id %> | ||
<%= submit_tag("Merge") %> | ||
<% end %> | ||
<% end %> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should something happen if the user is not an admin? I know it might not show up on the view, but someone could still potentially hit the URL anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great suggestion - updated it so that a non-admin gets a flash notice and then is redirected to the index page.