Skip to content
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

Admin user can merge two articles from edit page #25

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
2545f33
initial setup
emgord Mar 28, 2016
4066a16
added cucumber feature for adding categories
emgord Mar 28, 2016
5ce6473
admin can add and edit categories, need to figure out tests
emgord Mar 28, 2016
c9a0a31
fixed broken cucumber tests
emgord Mar 28, 2016
7a0ffc9
added more cucumber tests to ensure new category is being created and…
emgord Mar 28, 2016
a70de61
cucumber test to check category count
emgord Mar 28, 2016
f7c4e53
added cucumber tests for editing categories
emgord Mar 28, 2016
2be66f9
trying to get rspec tests, small change to cucumber edit route
emgord Mar 28, 2016
1237194
rspec test for new
emgord Mar 28, 2016
17ea51e
rspec test for saving new categories
emgord Mar 29, 2016
f06cb7f
handle if category does not save
emgord Mar 29, 2016
28c6afd
admin users can add and edit categories
emgord Mar 29, 2016
cefa0da
working on cucumber tests for merging articles
emgord Mar 29, 2016
cd60cf9
removed db_test and db_development from git, added to git ignore
emgord Mar 29, 2016
6fe20e9
replaced case statement with if/else in categories controller new or …
emgord Mar 29, 2016
69a6ad0
changed add categories to manage categories
emgord Mar 29, 2016
f258055
merge with master
emgord Mar 29, 2016
ea6edec
cucumber tests for merging articles as admin
emgord Mar 29, 2016
0c67b88
added cucumber method to log in as contributor
emgord Mar 29, 2016
ac18cfd
cucumber test for non-admin cannot merge articles
emgord Mar 29, 2016
9d6e3d0
rspec tests for merging articles controller function
emgord Mar 29, 2016
e3dc9da
added merge controller action
emgord Mar 29, 2016
62d6644
controller method for merging articles complete, need merge_with mode…
emgord Mar 29, 2016
3e1f859
rspec for merge_with model method
emgord Mar 29, 2016
b1b373b
merge_with method working and specs passing
emgord Mar 29, 2016
faf1d35
merge article form added, need to restrict from contributor and new view
emgord Mar 30, 2016
c40e808
fixed cucumber test for admin merging articles
emgord Mar 30, 2016
21fb60c
fixed spec to reflect how params come in from form
emgord Mar 30, 2016
d297418
restrict so merge only visible for edit and admin
emgord Mar 30, 2016
57d6c07
more rspec tests for error cases
emgord Mar 30, 2016
9a3a6a8
removed focus so all specs run
emgord Mar 30, 2016
9888ccd
Merge pull request #1 from emgord/mergearticles
emgord Mar 30, 2016
68be69d
moved locals inside condition since not needed unless user is admin
emgord Mar 30, 2016
3832e0d
changes to cucumber and rspec tests based on feedback
emgord Mar 30, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ config/mail.yml
*~
db/*.sqlite*
db/schema.rb
db/db_test
db/db_development
.*.swp
.*.swo
.DS_Store
Expand Down
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,7 @@ group :development, :test do
gem 'cucumber-rails-training-wheels'
gem 'database_cleaner'
gem 'capybara'
gem 'better_errors'
gem 'binding_of_caller'
gem 'pry'
end
25 changes: 24 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
GEM
remote: http://rubygems.org/
remote: https://rubygems.org/
specs:
RedCloth (4.2.9)
abstract (1.0.0)
Expand Down Expand Up @@ -34,6 +34,11 @@ GEM
addressable (2.3.2)
archive-tar-minitar (0.5.2)
arel (2.0.10)
better_errors (0.0.8)
coderay
erubis
binding_of_caller (0.7.2)
debug_inspector (>= 0.0.1)
bluecloth (2.2.0)
builder (2.1.2)
capybara (1.1.2)
Expand All @@ -60,6 +65,7 @@ GEM
cucumber-rails (>= 1.1.1)
daemons (1.1.9)
database_cleaner (0.8.0)
debug_inspector (0.0.2)
diff-lcs (1.1.3)
erubis (2.6.6)
abstract (>= 1.0.0)
Expand Down Expand Up @@ -87,13 +93,20 @@ GEM
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
method_source (0.6.7)
ruby_parser (>= 2.3.1)
mime-types (1.19)
mini_magick (1.3.3)
subexec (~> 0.0.4)
multi_json (1.3.6)
nokogiri (1.5.5)
pg (0.14.1)
polyglot (0.3.3)
pry (0.9.7.4)
coderay (~> 0.9.8)
method_source (~> 0.6.7)
ruby_parser (>= 2.3.1)
slop (~> 2.1.0)
rack (1.2.5)
rack-mount (0.6.14)
rack (>= 1.0.0)
Expand Down Expand Up @@ -141,17 +154,21 @@ GEM
ruby-debug-base19 (>= 0.11.19)
ruby_core_source (0.1.5)
archive-tar-minitar (>= 0.5.2)
ruby_parser (3.8.1)
sexp_processor (~> 4.1)
rubypants (0.2.0)
rubyzip (0.9.9)
selenium-webdriver (2.25.0)
childprocess (>= 0.2.5)
libwebsocket (~> 0.1.3)
multi_json (~> 1.0)
rubyzip
sexp_processor (4.7.0)
simplecov (0.6.4)
multi_json (~> 1.0)
simplecov-html (~> 0.5.3)
simplecov-html (0.5.3)
slop (2.1.0)
sqlite3 (1.3.6)
subexec (0.0.4)
thin (1.5.0)
Expand Down Expand Up @@ -179,6 +196,8 @@ DEPENDENCIES
acts_as_list
acts_as_tree_rails3
addressable (~> 2.1)
better_errors
binding_of_caller
bluecloth (~> 2.1)
capybara
coderay (~> 0.9)
Expand All @@ -193,6 +212,7 @@ DEPENDENCIES
kaminari
mini_magick (~> 1.3.3)
pg
pry
rails (~> 3.0.10)
rake (~> 0.9.2)
recaptcha
Expand All @@ -205,3 +225,6 @@ DEPENDENCIES
thin
uuidtools (~> 2.1.1)
webrat

BUNDLED WITH
1.11.2
14 changes: 9 additions & 5 deletions app/controllers/admin/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ class Admin::CategoriesController < Admin::BaseController
def index; redirect_to :action => 'new' ; end
def edit; new_or_edit; end

def new
def new
respond_to do |format|
format.html { new_or_edit }
format.js {
format.js {
@category = Category.new
}
end
Expand All @@ -25,12 +25,16 @@ def destroy

def new_or_edit
@categories = Category.find(:all)
@category = Category.find(params[:id])
if params[:id].nil?
@category = Category.new
else
@category = Category.find(params[:id])
end
@category.attributes = params[:category]
if request.post?
respond_to do |format|
format.html { save_category }
format.js do
format.js do
@category.save
@article = Article.new
@article.categories << @category
Expand All @@ -43,7 +47,7 @@ def new_or_edit
end

def save_category
if @category.save!
if @category.save
flash[:notice] = _('Category was successfully saved.')
else
flash[:error] = _('Category could not be saved.')
Expand Down
32 changes: 25 additions & 7 deletions app/controllers/admin/content_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -44,14 +44,33 @@ def destroy
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
flash[:notice] = _("This article was deleted successfully")
redirect_to :action => 'index'
end

def merge
if current_user.admin?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating these local variables first is nice, but if the user isn't an admin, they aren't needed to i would suggest moving them into the first conditional

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great point. I made the change.

id = params[:id]
merge_id = params[:merge_with][:merge_id]
if id == merge_id
flash[:error] = _("You cannot merge an article with itself")
elsif Article.exists?(merge_id) && Article.exists?(id)
record = Article.find(id)
record.merge_with(merge_id)
flash[:notice] = _("Articles merged successfully")
else
flash[:error] = _("Article not found")
end
else
flash[:error] = _("Error, you are not allowed to perform this action")
end
redirect_to :action => 'index'
end

def insert_editor
editor = 'visual'
editor = 'simple' if params[:editor].to_s == 'simple'
Expand All @@ -77,7 +96,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)
Expand All @@ -92,7 +111,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
Expand Down Expand Up @@ -144,7 +163,6 @@ def new_or_edit
id = params[:article][:id] if params[:article] && params[:article][:id]
@article = Article.get_or_build_article(id)
@article.text_filter = current_user.text_filter if current_user.simple_editor?

@post_types = PostType.find(:all)
if request.post?
if params[:article][:draft]
Expand All @@ -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
Expand Down
17 changes: 14 additions & 3 deletions app/models/article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -416,6 +416,17 @@ def access_by?(user)
user.admin? || user_id == user.id
end

def merge_with(other_article_id)
Article.find(other_article_id).comments.each do |comment|

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a lot of method chaining going on here. Is there a better way to get the comments associated without iterating through each one?

comment.article_id = self.id
comment.save
end
merge_article = Article.find(other_article_id)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this and line 420 are both doing the same find operation which means it is calling the database an extra time. Consider how to change this to find the article only once.

self.body += " " + merge_article.body
self.save

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Saving the article and destroying the old one could both have different types of errors. Consider using a transaction to ensure that their both or none happen.

merge_article.destroy
end

protected

def set_published_at
Expand Down
12 changes: 12 additions & 0 deletions app/views/admin/content/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
<% @page_heading = _('New article') %>

<%= render "admin/shared/edit", { :form_type => "article", :form_action => { :action => "new", :id => @article.id , :class => ('autosave')} } %>

<% if current_user.admin? && [email protected]? %>
<h3>Merge Articles</h3>
<%= form_tag :action=>"merge", :id => @article.id do %>
<%= error_messages_for 'article' %>
<label for="merge_with"><%= _("Article ID")%></label>
<div class='input'>
<%= text_field :merge_with, :merge_id %>
</div>
<%= submit_tag("Merge") %>
<% end %>
<% end %>
39 changes: 39 additions & 0 deletions features/manage_categories.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Feature: Manage Categories
As a blog administrator
In order to share my thoughts with the world
I want to be able to add categories to my blog

Background:
Given the blog is set up
And I am logged into the admin panel

Scenario: Successfully add categories
Given I am on the new category page
Then I should not see "fun"
When I fill in "category_name" with "fun"
And I fill in "category_keywords" with "friend, happy, song"
And I fill in "category_permalink" with "something"
And I fill in "category_description" with "cool beans"
And I press "Save"
Then I should be on the new category page
And I should see "fun"
And I should see "friend, happy, song"
And I should see "something"
And I should see "cool beans"
And I should have 2 categories

Scenario: Successfully edit categories
Given I am on the edit category page
Then I should see "general"
When I fill in "category_name" with "fun"
And I fill in "category_keywords" with "friend, happy, song"
And I fill in "category_permalink" with "something"
And I fill in "category_description" with "cool beans"
And I press "Save"
Then I should be on the new category page
Then I should not see "general"
And I should see "fun"
And I should see "friend, happy, song"
And I should see "something"
And I should see "cool beans"
And I should have 1 categories
29 changes: 29 additions & 0 deletions features/merge_articles.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Feature: Merge Articles
As a blog administrator
In order to share my thoughts with the world
I want to be able to merge similar articles on my blog

Background:
Given the blog is set up

Scenario: Non-admin cannot merge articles
Given I am logged in as a contributor
And I am on the edit articles page
Then I should not see "Merge Articles"

Scenario: Admin can successfully merge articles
Given I am logged into the admin panel
And I have two similar articles titled Cats, Dogs
And I am on the manage articles page
Then I should see "Cats"
And I should see "Dogs"
When I follow "Edit"
Then I should be on the edit articles page
And I should see "Merge Articles"
When I fill in "merge_with_merge_id" with "4"
And I press "Merge"
Then I should be on the manage articles page
And I should not see "Dogs"
And I should see "Cats"
When I follow "Show"
Then I should see "I love cats I love dogs"
Loading