Skip to content

Commit

Permalink
fixes #824
Browse files Browse the repository at this point in the history
  • Loading branch information
r888888888 committed Mar 10, 2013
1 parent 1946fab commit e53d60d
Show file tree
Hide file tree
Showing 22 changed files with 384 additions and 17 deletions.
6 changes: 1 addition & 5 deletions app/controllers/tag_alias_corrections_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
class TagAliasCorrectionsController < ApplicationController
before_filter :moderator_only

def new
@correction = TagAliasCorrection.new(params[:tag_alias_id])
end

def create
@correction = TagAliasCorrection.new(params[:tag_alias_id])

Expand All @@ -13,7 +9,7 @@ def create
flash[:notice] = "The fix has been queued and will be processed"
end

redirect_to tag_alias_correction_path(:id => params[:tag_alias_id])
redirect_to tag_alias_correction_path(:tag_alias_id => params[:tag_alias_id])
end

def show
Expand Down
17 changes: 17 additions & 0 deletions app/controllers/tag_alias_requests_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class TagAliasRequestsController < ApplicationController
before_filter :member_only
rescue_from TagAliasRequest::ValidationError, :with => :rescue_exception

def new
end

def create
@tag_alias_request = TagAliasRequest.new(
params[:tag_alias_request][:antecedent_name],
params[:tag_alias_request][:consequent_name],
params[:tag_alias_request][:reason]
)
@tag_alias_request.create
redirect_to(forum_topic_path(@tag_alias_request.forum_topic))
end
end
2 changes: 1 addition & 1 deletion app/controllers/tag_aliases_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class TagAliasesController < ApplicationController
before_filter :admin_only, :only => [:approve, :destroy, :create]
before_filter :admin_only, :only => [:approve, :destroy, :new, :create]
respond_to :html, :xml, :json, :js

def new
Expand Down
17 changes: 17 additions & 0 deletions app/controllers/tag_implication_requests_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class TagImplicationRequestsController < ApplicationController
before_filter :member_only
rescue_from TagImplicationRequest::ValidationError, :with => :rescue_exception

def new
end

def create
@tag_implication_request = TagImplicationRequest.new(
params[:tag_implication_request][:antecedent_name],
params[:tag_implication_request][:consequent_name],
params[:tag_implication_request][:reason]
)
@tag_implication_request.create
redirect_to(forum_topic_path(@tag_implication_request.forum_topic))
end
end
4 changes: 2 additions & 2 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ def nav_link_match(controller, url)
when "moderator/dashboards"
/^\/moderator/

when "tag_aliases", "tag_alias_corrections"
when "tag_aliases", "tag_alias_corrections", "tag_alias_requests"
/^\/tag_aliases/

when "tag_implications"
when "tag_implications", "tag_implication_requests"
/^\/tag_implications/

when "wiki_pages", "wiki_page_versions"
Expand Down
39 changes: 39 additions & 0 deletions app/logical/tag_alias_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class TagAliasRequest
class ValidationError < Exception ; end

attr_reader :antecedent_name, :consequent_name, :reason, :tag_alias, :forum_topic

def initialize(antecedent_name, consequent_name, reason)
@antecedent_name = antecedent_name
@consequent_name = consequent_name
@reason = reason
end

def create
TagAlias.transaction do
create_alias
create_forum_topic
end
end

def create_alias
@tag_alias = TagAlias.create(:antecedent_name => antecedent_name, :consequent_name => consequent_name, :status => "pending")
if @tag_alias.errors.any?
raise ValidationError.new(@tag_alias.errors.full_messages.join("; "))
end
end

def create_forum_topic
@forum_topic = ForumTopic.create(
:title => "Tag alias: #{antecedent_name} -> #{consequent_name}",
:original_post_attributes => {
:body => reason + "\n\n\"Link to alias\":/tag_aliases?search[id]=#{tag_alias.id}"
}
)
if @forum_topic.errors.any?
raise ValidationError.new(@forum_topic.errors.full_messages.join("; "))
end

tag_alias.update_attribute(:forum_topic_id, @forum_topic.id)
end
end
39 changes: 39 additions & 0 deletions app/logical/tag_implication_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class TagImplicationRequest
class ValidationError < Exception ; end

attr_reader :antecedent_name, :consequent_name, :reason, :tag_implication, :forum_topic

def initialize(antecedent_name, consequent_name, reason)
@antecedent_name = antecedent_name
@consequent_name = consequent_name
@reason = reason
end

def create
TagImplication.transaction do
create_implication
create_forum_topic
end
end

def create_implication
@tag_implication = TagImplication.create(:antecedent_name => antecedent_name, :consequent_name => consequent_name, :status => "pending")
if @tag_implication.errors.any?
raise ValidationError.new(@tag_implication.errors.full_messages.join("; "))
end
end

def create_forum_topic
@forum_topic = ForumTopic.create(
:title => "Tag implication: #{antecedent_name} -> #{consequent_name}",
:original_post_attributes => {
:body => reason + "\n\n\"Link to implication\":/tag_implications?search[id]=#{tag_implication.id}"
}
)
if @forum_topic.errors.any?
raise ValidationError.new(@forum_topic.errors.full_messages.join("; "))
end

tag_implication.update_attribute(:forum_topic_id, @forum_topic.id)
end
end
3 changes: 2 additions & 1 deletion app/models/tag_alias.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ class TagAlias < ActiveRecord::Base
after_save :ensure_category_consistency
after_destroy :clear_all_cache
before_validation :initialize_creator, :on => :create
validates_presence_of :creator_id
validates_presence_of :creator_id, :antecedent_name, :consequent_name
validates_uniqueness_of :antecedent_name
validate :absence_of_transitive_relation
belongs_to :creator, :class_name => "User"
belongs_to :forum_topic

module SearchMethods
def name_matches(name)
Expand Down
2 changes: 1 addition & 1 deletion app/models/tag_implication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class TagImplication < ActiveRecord::Base
after_save :update_descendant_names_for_parent
belongs_to :creator, :class_name => "User"
before_validation :initialize_creator, :on => :create
validates_presence_of :creator_id
validates_presence_of :creator_id, :antecedent_name, :consequent_name
validates_uniqueness_of :antecedent_name, :scope => :consequent_name
validate :absence_of_circular_relation

Expand Down
3 changes: 1 addition & 2 deletions app/views/tag_alias_corrections/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

<p>You can try to fix this alias. This will clear the cache and re-save all posts associated with <strong><%= @correction.antecedent_name %></strong>.</p>

<%= form_tag(tag_alias_correction_path) do %>
<%= hidden_field "tag_alias_id", params[:tag_alias_id] %>
<%= form_tag(tag_alias_correction_path(:tag_alias_id => @correction.tag_alias_id)) do %>
<%= submit_tag "Fix" %>
<%= submit_tag "Cancel" %>
<% end %>
Expand Down
30 changes: 30 additions & 0 deletions app/views/tag_alias_requests/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<div id="c-tag-aliases">
<div id="a-request-new">
<h1>Tag Alias Request</h1>

<p>You can request a new tag alias be created. This will create a corresponding forum topic for community review.</p>

<%= form_tag(tag_alias_request_path, :class => "simple_form") do %>
<div class="input">
<label>Antecedent</label>
<%= text_field "tag_alias_request", "antecedent_name" %>
</div>

<div class="input">
<label>Consequent</label>
<%= text_field "tag_alias_request", "consequent_name" %>
</div>

<div class="input">
<label>Reason</label>
<%= text_area "tag_alias_request", "reason" %>
</div>

<div class="input">
<%= submit_tag "Submit" %>
</div>
<% end %>
<%= render "tag_aliases/secondary_links" %>
</div>
</div>
6 changes: 5 additions & 1 deletion app/views/tag_aliases/_secondary_links.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<% content_for(:secondary_links) do %>
<menu>
<li><%= link_to "Listing", tag_aliases_path %></li>
<li><%= link_to "New", new_tag_alias_path %></li>
<% if CurrentUser.is_admin? %>
<li><%= link_to "New", new_tag_alias_path %></li>
<% else %>
<li><%= link_to "Request", new_tag_alias_request_path %></li>
<% end %>
<li><%= link_to "Help", wiki_pages_path(:title => "help:tag_aliases") %></li>
</menu>
<% end %>
30 changes: 30 additions & 0 deletions app/views/tag_implication_requests/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<div id="c-tag-implications">
<div id="a-request-new">
<h1>Tag Implication Request</h1>

<p>You can request a new tag implication be created. This will create a corresponding forum topic for community review.</p>

<%= form_tag(tag_implication_request_path, :class => "simple_form") do %>
<div class="input">
<label>Antecedent</label>
<%= text_field "tag_implication_request", "antecedent_name" %>
</div>

<div class="input">
<label>Consequent</label>
<%= text_field "tag_implication_request", "consequent_name" %>
</div>

<div class="input">
<label>Reason</label>
<%= text_area "tag_implication_request", "reason" %>
</div>

<div class="input">
<%= submit_tag "Submit" %>
</div>
<% end %>
<%= render "tag_implications/secondary_links" %>
</div>
</div>
6 changes: 5 additions & 1 deletion app/views/tag_implications/_secondary_links.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<% content_for(:secondary_links) do %>
<menu>
<li><%= link_to "Listing", tag_implications_path %></li>
<li><%= link_to "New", new_tag_implication_path %></li>
<% if CurrentUser.is_admin? %>
<li><%= link_to "New", new_tag_implication_path %></li>
<% else %>
<li><%= link_to "Request", new_tag_implication_request_path %></li>
<% end %>
<li><%= link_to "Help", wiki_pages_path(:title => "help:tag_implications") %></li>
</menu>
<% end %>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,13 @@
end
end
resource :tag_alias_correction, :only => [:new, :create, :show]
resource :tag_alias_request, :only => [:new, :create]
resources :tag_implications do
member do
post :approve
end
end
resource :tag_implication_request, :only => [:new, :create]
resources :tag_subscriptions do
member do
get :posts
Expand Down
33 changes: 33 additions & 0 deletions test/functional/tag_alias_corrections_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'test_helper'

class TagAliasCorrectionsControllerTest < ActionController::TestCase
context "The tag alias correction controller" do
setup do
@admin = FactoryGirl.create(:admin_user)
CurrentUser.user = @admin
CurrentUser.ip_addr = "127.0.0.1"
MEMCACHE.flush_all
Delayed::Worker.delay_jobs = false
@tag_alias = FactoryGirl.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb")
end

teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end

context "show action" do
should "render" do
get :show, {:tag_alias_id => @tag_alias.id}, {:user => @admin.id}
assert_response :success
end
end

context "create action" do
should "render" do
post :create, {:tag_alias_id => @tag_alias.id, :commit => "Fix"}, {:user => @admin.id}
assert_redirected_to(tag_alias_correction_path(:tag_alias_id => @tag_alias.id))
end
end
end
end
34 changes: 34 additions & 0 deletions test/functional/tag_alias_requests_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'test_helper'

class TagAliasRequestsControllerTest < ActionController::TestCase
context "The tag alias request controller" do
setup do
@user = FactoryGirl.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
MEMCACHE.flush_all
Delayed::Worker.delay_jobs = false
end

teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end

context "new action" do
should "render" do
get :new, {}, {:user => @user.id}
assert_response :success
end
end

context "create action" do
should "render" do
assert_difference("ForumTopic.count", 1) do
post :create, {:tag_alias_request => {:antecedent_name => "aaa", :consequent_name => "bbb", :reason => "ccc"}}, {:user => @user.id}
end
assert_redirected_to(forum_topic_path(ForumTopic.last))
end
end
end
end
34 changes: 34 additions & 0 deletions test/functional/tag_implication_requests_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'test_helper'

class TagImplicationRequestsControllerTest < ActionController::TestCase
context "The tag implication request controller" do
setup do
@user = FactoryGirl.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
MEMCACHE.flush_all
Delayed::Worker.delay_jobs = false
end

teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end

context "new action" do
should "render" do
get :new, {}, {:user => @user.id}
assert_response :success
end
end

context "create action" do
should "render" do
assert_difference("ForumTopic.count", 1) do
post :create, {:tag_implication_request => {:antecedent_name => "aaa", :consequent_name => "bbb", :reason => "ccc"}}, {:user => @user.id}
end
assert_redirected_to(forum_topic_path(ForumTopic.last))
end
end
end
end

0 comments on commit e53d60d

Please sign in to comment.