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

Added new feature - merging articles #31

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
73f14ca
deployed to heroku
kdefliese Mar 28, 2016
8affeba
started writing feature for creating categories
kdefliese Mar 28, 2016
e197c54
cucumber test seems to be failing appropriately
kdefliese Mar 28, 2016
11c3441
updated feature based on better understanding of the bug
kdefliese Mar 28, 2016
bafbcb1
added rspec tests for rendering index and new page
kdefliese Mar 28, 2016
c495c6d
updated category new_or_edit function to handle nil ID, rspecs passing
kdefliese Mar 28, 2016
cea1ca8
cucumber test passes for going to the categories page
kdefliese Mar 28, 2016
fa42bbe
added to cucumber test so it covers category creation too
kdefliese Mar 28, 2016
e2fd415
added cucumber feature for editing categories
kdefliese Mar 28, 2016
e67a30a
Added db files to gitignore
kdefliese Mar 28, 2016
8388959
deleted db files
kdefliese Mar 28, 2016
18234f2
added additional field validation for category add/edit
kdefliese Mar 28, 2016
a094dc5
added cucumber scenarios for saving with blank category name
kdefliese Mar 29, 2016
4542d5b
fixed error that was happening when saving category with empty name
kdefliese Mar 29, 2016
e42d3ff
added last step for cucumber tests for saving nil category names
kdefliese Mar 29, 2016
f6c1b6a
removed db files again..
kdefliese Mar 29, 2016
68c604a
began creating feature for merging articles
kdefliese Mar 29, 2016
7d15d8d
wrote first spec for merging articles
kdefliese Mar 29, 2016
c2ff602
updated cucumber feature for article merging
kdefliese Mar 29, 2016
d3574d7
added route and method stub for merging articles
kdefliese Mar 29, 2016
41eb299
added article self method for merging, removed unnecessary route
kdefliese Mar 29, 2016
4ca8dce
fixed gitignore to properly exclude db files
kdefliese Mar 29, 2016
be933d0
added some web steps for article merging cucumber scenario
kdefliese Mar 30, 2016
9f1725e
added partial for merging article
kdefliese Mar 30, 2016
3e897e3
merge partial should only show up for admins when editing
kdefliese Mar 30, 2016
1fb480c
updated web steps and split merge into two feature files
kdefliese Mar 30, 2016
926e998
fixed typo in non-admin merging scenario
kdefliese Mar 30, 2016
36f85c3
basic merging works
kdefliese Mar 30, 2016
77772e2
cucumber tests passing for merging articles
kdefliese Mar 30, 2016
5a75c27
added logic to prevent merging article with itself
kdefliese Mar 30, 2016
589907e
added author to cucumber tests for article merging
kdefliese Mar 30, 2016
8efd204
updated article merge spec
kdefliese Mar 31, 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
@@ -1,6 +1,8 @@
config/mail.yml
*.log
*~
db/db_development
db/db_test
db/*.sqlite*
db/schema.rb
.*.swp
Expand Down
5 changes: 4 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 @@ -205,3 +205,6 @@ DEPENDENCIES
thin
uuidtools (~> 2.1.1)
webrat

BUNDLED WITH
1.11.2
15 changes: 10 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,17 @@ def destroy

def new_or_edit
@categories = Category.find(:all)
@category = Category.find(params[:id])
@category = case params[:id]
when nil
Category.new
else
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 +48,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
28 changes: 22 additions & 6 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,7 +44,7 @@ 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
Expand Down Expand Up @@ -77,7 +77,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 +92,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 All @@ -113,6 +113,22 @@ def autosave
render :text => nil
end

def merge
@article_1 = Article.find(params[:id])
# check to see that article with entered ID exists and isn't the same as the current article
if !Article.find_by_id(params[:merge_with]).nil? && (params[:merge_with] != params[:id])
@article_1.merge_with(params[:merge_with])
flash[:notice] = _('Article was successfully merged')
redirect_to :action => 'index'
elsif Article.find_by_id(params[:merge_with]).nil?

Choose a reason for hiding this comment

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

So you're doing two find_by_ids here, which is going to make the same database query twice. I'd recommend storing the result of this above the conditional so it will only do this database query once.

flash[:error] = _("An article doesn't exist with that ID! Please try again")
new_or_edit
else
flash[:error] = _("You can't merge this article with itself! Please try again")
new_or_edit
end
end

protected

def get_fresh_or_existing_draft_for_article
Expand Down Expand Up @@ -159,13 +175,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)
# keep title and author from article 1 (self)
article_2 = Article.find(other_article_id.to_i)

Choose a reason for hiding this comment

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

Even though you are merging with another article, it doesn't really make sense to call this variable article_2 since you use the self variable for the "article_1".

# combine body and comments from both articles
self.body += " #{article_2.body}"
self.comments += article_2.comments
self.save
article_2.delete
return self
end

protected

def set_published_at
Expand Down
13 changes: 13 additions & 0 deletions app/views/admin/content/_merge.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div>
<h3>Merge Articles</h3>
<form action="/admin/content/merge/<%= @article.id %>" method="get">

Choose a reason for hiding this comment

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

Is there a named path you can use here instead of a string?

<div class='clearfix'>
<label for="merge_with" style="width: 55px; margin-right: 5px;"><%= _("Article ID")%></label>

Choose a reason for hiding this comment

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

Eek! Is there a class you can use to do this styling instead of styling directly in the label?

Copy link
Author

Choose a reason for hiding this comment

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

I wanted to but I couldn't figure out where I was supposed to put CSS definitions! This would have been something I would have asked my team about best practice I think...

Choose a reason for hiding this comment

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

Cool - makes sense to me!

<div class='input'>
<input class="large" id="merge_id" name="merge_with" size="30" type="text">
</div>
</div>
<br />
<button class="btn" type="submit" value="submit" id="submit_merge">Merge</button>
</form>
</div>
4 changes: 4 additions & 0 deletions app/views/admin/shared/_edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@
<%= render :partial => "form" %>

<% end %>

<% if params[:id] && @current_user.admin? %>
<%= render :partial => "merge" %>
<% end %>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
end

# Admin/XController

%w{advanced cache categories comments content profiles feedback general pages
resources sidebar textfilters themes trackbacks users settings tags redirects seo post_types }.each do |i|
match "/admin/#{i}", :to => "admin/#{i}#index", :format => false
Expand Down
25 changes: 25 additions & 0 deletions features/create_category.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Feature: Create Categories
As a blog administrator
I want to be able to create Categories

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

Scenario: Successfully create new Categories
When I click on Categories
Then I should be on the new category page
When I fill in "category_name" with "Meow Meow"
And I fill in "category_keywords" with "Cats"
And I fill in "category_description" with "I like cats"
And I press "Save"
Then I should see "Meow Meow"
And I should see "Cats"
And I should see "I like cats"

Scenario: Try to create a Category without a name
When I click on Categories
Then I should be on the new category page
When I fill in "category_name" with ""
And I press "Save"
Then page should have error message "Category could not be saved."
30 changes: 30 additions & 0 deletions features/edit_category.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Feature: Edit Categories
As a blog administrator
I want to be able to edit Categories

Background:
Given the blog is set up
And I am logged into the admin panel
And I have an existing Category named "Meow"

Scenario: Successfully edit existing Categories
When I click on Categories
Then I should be on the new category page
When I follow "Meow"
Then I should be on the edit category page
When I fill in "category_name" with "Meow Meow"
And I fill in "category_keywords" with "Cats Meow"
And I fill in "category_description" with "I really really like cats"
And I press "Save"
Then I should see "Meow Meow"
And I should see "Cats Meow"
And I should see "I really really like cats"

Scenario: Try to edit a Category and remove the name
When I click on Categories
Then I should be on the new category page
When I follow "Meow"
Then I should be on the edit category page
When I fill in "category_name" with ""
And I press "Save"
Then page should have error message "Category could not be saved."
23 changes: 23 additions & 0 deletions features/merge_article.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Feature: Merge Articles
As a blog administrator
In order to keep the world spam-free
I want to be able to merge similar articles on my blog

Background:
Given the blog is set up
And I am logged into the admin panel
And an article exists with title "Entry 1" and text "This is entry 1" and author "I Like Cats"

Scenario: Admin can successfully merge articles
Given I am on the edit article page
Then I should see "Merge Articles"
When I fill in "merge_with" with "3"
And I press "Merge"
Then I should be on the admin content page
And I should see "Article was successfully merged"
And I should see "Hello World!"
And I should see "admin"
And I should not see "Entry 1"
And I should not see "I Like Cats"
When I follow "Hello World!"
Then I should see "Welcome to Typo. This is your first article. Edit or delete it, then start blogging! This is entry 1"
12 changes: 12 additions & 0 deletions features/non_admins_cant_merge_articles.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Feature: Merge Articles
As a non-admin blog user
I can't have special things
So I can't merge articles

Background:
Given the blog is set up
And an article exists with title "Entry 1" and text "This is entry 1" and author "I Like Cats"

Scenario: Non-admin cannot merge articles

Choose a reason for hiding this comment

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

I think it could make sense to put this scenario in the feature in the merge_articles rather than in this separate file.

Given I am on the edit article page
Then I should not see "Merge Articles"
24 changes: 21 additions & 3 deletions features/step_definitions/web_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def with_scope(locator)
end
end
end

Then /^(?:|I )should be on (.+)$/ do |page_name|
current_path = URI.parse(current_url).path
if current_path.respond_to? :should
Expand All @@ -264,8 +264,8 @@ def with_scope(locator)
query = URI.parse(current_url).query
actual_params = query ? CGI.parse(query) : {}
expected_params = {}
expected_pairs.rows_hash.each_pair{|k,v| expected_params[k] = v.split(',')}
expected_pairs.rows_hash.each_pair{|k,v| expected_params[k] = v.split(',')}

if actual_params.respond_to? :should
actual_params.should == expected_params
else
Expand All @@ -276,3 +276,21 @@ def with_scope(locator)
Then /^show me the page$/ do
save_and_open_page
end

When /^I click on Categories$/ do
click_link('Categories')
end

Given /^I have an existing Category named "(.*?)"$/ do |arg1|
Category.create!({:name => 'Meow'})
end

Then /^page should have error message "(.*?)"$/ do |arg1|
page.should have_content('Category could not be saved')
end

Given /^an article exists with title "(.*?)" and text "(.*?)" and author "(.*?)"$/ do |arg1, arg2, arg3|
Article.create!({:title => arg1,
:body => arg2,
:author => arg3})
end
10 changes: 9 additions & 1 deletion features/support/paths.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ def path_to(page_name)
'/'
when /^the new article page$/
'/admin/content/new'

when /^the new category page$/
'/admin/categories/new'
when /^the edit category page$/
# there has to be a better way to do this...
'/admin/categories/edit/2'
when /^the edit article page$/
'admin/content/edit/1'
when /^the article merge path$/
'/admin/content/merge/1'
# Add more mappings here.
# Here is an example that pulls values out of the Regexp:
#
Expand Down
2 changes: 1 addition & 1 deletion public/javascripts/ckeditor/config.bak
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ CKEDITOR.editorConfig = function( config )
config.PreserveSessionOnFileBrowser = true;
// Define changes to default configuration here. For example:
//config.language = '';
config.uiColor = '#E0ECFF';
config.uiColor = '#eee';
config.toolbar = 'Basic';
config.entities_greek = false;
config.entities_latin = false;
Expand Down
2 changes: 1 addition & 1 deletion public/javascripts/ckeditor/config.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ CKEDITOR.editorConfig = function( config )
config.PreserveSessionOnFileBrowser = true;
// Define changes to default configuration here. For example:
//config.language = '';
config.uiColor = '#E0ECFF';
config.uiColor = '#eee';
config.toolbar = 'Basic';
config.entities_greek = false;
config.entities_latin = false;
Expand Down
Loading