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

New feature added to allow admin users to merge articles #29

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
8bb6a30
Added first test to test category#new controller for Cucumber
trowbrsa Mar 28, 2016
d7d0b79
Made some progress on bug fix
trowbrsa Mar 28, 2016
78077f8
Doing a BFG to remove db file
trowbrsa Mar 29, 2016
8a40259
Updated gitignore to exclude db files
trowbrsa Mar 29, 2016
8faf73f
Deleted db_dev and test
trowbrsa Mar 29, 2016
a930230
Added pry for testing in development
trowbrsa Mar 29, 2016
9bd00cd
Removed pry as it does not play nice with this version of Ruby
trowbrsa Mar 29, 2016
81adae7
Wrote a test that will fail based on current bug
trowbrsa Mar 29, 2016
aff0f3a
Successfully fixed bug for Categories by putting creation of new cate…
trowbrsa Mar 29, 2016
11f190e
Additional cucumber tests to verify edit functionality working
trowbrsa Mar 29, 2016
313c535
Added additional path in support/paths file so that you can navigate …
trowbrsa Mar 29, 2016
e1df9bb
All cucumber tests running correctly for the ability to add a new cat…
trowbrsa Mar 29, 2016
6f93e26
Added Cucumber test to verify that non-admin cannot see 'merge articl…
trowbrsa Mar 29, 2016
1873442
Added Kari's suggested edit of test to see if view is rendering corre…
trowbrsa Mar 29, 2016
7a45362
Added gem 'pry' for testing, and added skeleton cucumber tests for te…
trowbrsa Mar 29, 2016
27afa80
Successfully assigned permissions so that users can only see some con…
trowbrsa Mar 29, 2016
df00363
Merge article feature cannot be seen if the article is new
trowbrsa Mar 29, 2016
3fe45ca
Wrote a test to make sure admins can only see merge button in 'edit' …
trowbrsa Mar 29, 2016
3482ac4
Added Rails ERD to see relationship between models
trowbrsa Mar 30, 2016
7dda45c
Drafted version to combined two articles
trowbrsa Mar 30, 2016
340fa06
draft test to ensure that admins can see merge content path
trowbrsa Mar 30, 2016
a1146ff
Moved method to Article model, updated it so less clunky
trowbrsa Mar 30, 2016
8f5a28b
Updated view for submitting merge
trowbrsa Mar 30, 2016
d0e5669
Wrote draft method for merging two articles together in content contr…
trowbrsa Mar 30, 2016
4e08c02
Removed non-working methods and form from content.rb and shared form …
trowbrsa Mar 30, 2016
94077bc
Added flash messages to notify users of action when merging
trowbrsa Mar 30, 2016
6b8b5b6
Refined method for merging articles, added additional cucumber tests
trowbrsa Mar 30, 2016
17484d0
Semi-working final cucumber test
trowbrsa Mar 30, 2016
d1f54b9
Addressed comments in Kari's pull request
trowbrsa 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
Expand Up @@ -3,6 +3,8 @@ config/mail.yml
*~
db/*.sqlite*
db/schema.rb
db/db_development
db/db_test
.*.swp
.*.swo
.DS_Store
Expand Down
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,8 @@ group :development, :test do
gem 'cucumber-rails-training-wheels'
gem 'database_cleaner'
gem 'capybara'
gem 'better_errors'
gem 'binding_of_caller'
gem 'pry'
gem 'rails-erd'
end
35 changes: 33 additions & 2 deletions 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 @@ -45,8 +50,9 @@ GEM
xpath (~> 0.1.4)
childprocess (0.3.5)
ffi (~> 1.0, >= 1.0.6)
choice (0.1.7)
coderay (0.9.8)
columnize (0.3.6)
columnize (0.9.0)
cucumber (1.2.1)
builder (>= 2.1.2)
diff-lcs (>= 1.1.3)
Expand All @@ -60,6 +66,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 +94,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 All @@ -107,6 +121,11 @@ GEM
activesupport (= 3.0.17)
bundler (~> 1.0)
railties (= 3.0.17)
rails-erd (1.1.0)
activerecord (>= 3.0)
activesupport (>= 3.0)
choice (~> 0.1.6)
ruby-graphviz (~> 1.0.4)
railties (3.0.17)
actionpack (= 3.0.17)
activesupport (= 3.0.17)
Expand Down Expand Up @@ -139,19 +158,24 @@ GEM
columnize (>= 0.3.1)
linecache19 (>= 0.5.11)
ruby-debug-base19 (>= 0.11.19)
ruby-graphviz (1.0.9)
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 +203,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,7 +219,9 @@ DEPENDENCIES
kaminari
mini_magick (~> 1.3.3)
pg
pry
rails (~> 3.0.10)
rails-erd
rake (~> 0.9.2)
recaptcha
require_relative
Expand All @@ -205,3 +233,6 @@ DEPENDENCIES
thin
uuidtools (~> 2.1.1)
webrat

BUNDLED WITH
1.11.2
12 changes: 8 additions & 4 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_by_id(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 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 All @@ -37,14 +37,32 @@ def edit
new_or_edit
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.

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.

Copy link
Author

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.

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)

Choose a reason for hiding this comment

The 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 elsif

Copy link
Author

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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 Expand Up @@ -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
1 change: 1 addition & 0 deletions app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class CategoriesController < GroupingController
# index - inherited
# show - inherited

end
20 changes: 17 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 @@ -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|

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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)

Choose a reason for hiding this comment

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

Looks like your'e doing Article.find multiple times. How can you simplify so that it doesn't call the DB multiple times?

Copy link
Author

Choose a reason for hiding this comment

The 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
10 changes: 5 additions & 5 deletions app/models/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ def invalidates_cache?(on_destruction = false)
(changed? && published?) || just_changed_published_status?
end
end

def shorten_url
return unless self.published

r = Redirect.new
r.from_path = r.shorten
r.to_path = self.permalink_url

# This because updating self.redirects.first raises ActiveRecord::ReadOnlyRecord
unless (red = self.redirects.first).nil?
return if red.to_path == self.permalink_url
Expand Down Expand Up @@ -296,6 +296,6 @@ class ContentTextHelpers
include ActionView::Helpers::TagHelper
include ActionView::Helpers::SanitizeHelper
include ActionView::Helpers::TextHelper
extend ActionView::Helpers::SanitizeHelper::ClassMethods
extend ActionView::Helpers::SanitizeHelper::
ClassMethods
end

7 changes: 2 additions & 5 deletions app/views/admin/content/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<div class='clearfix'>
<%= _("Status:") %> <strong><%= @article.state.to_s.downcase %></strong> <a href="#" onclick="Element.toggle('status'); return false">Change</a>
<ul class='inputs-list'>
<li id='status' style='display: none;'>
<li id='status' style='display: none;'>
<label for="article_published">
<%= check_box 'article', 'published' %>
<%= _("Published") %>
Expand Down Expand Up @@ -129,7 +129,7 @@
<div class='class'>
<%= text_field 'article', 'keywords', {:autocomplete => 'off', :style => 'width: 100%'} %>
</div>
<%= auto_complete_field 'article_keywords', { :url => { :action => "auto_complete_for_article_keywords"}, :tokens => ','}%>
<%= auto_complete_field 'article_keywords', { :url => { :action => "auto_complete_for_article_keywords"}, :tokens => ','}%>
</div>

<div class='separator'>
Expand All @@ -146,8 +146,5 @@
<ul id='attachments' class='inputs-list'>
<%= render 'admin/content/attachment', { :attachment_num => 1, :hidden => false } -%>
</ul>
</div>
</div>

</div>

2 changes: 1 addition & 1 deletion app/views/admin/dashboard/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
<div class='dashboard'>
<%= render "inbound" %>
<%= render "posts" %>
<%= render "typo_dev" %>
<%= render "typo_dev" %>
</div>
10 changes: 10 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,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 %>
Binary file added erd.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion features/create_blog.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Feature: Create Blog
And I should see "My Shiny Weblog!"

Scenario: Create blog page not shown when blog created
Given the blog is set up
When I am on the home page
And I am logged into the admin panel
Then I should not see "My Shiny Weblog!"
And I should see "Teh Blag"
Loading