Commit 28f34221 by tady

tag move

parent 035c27af
class TagsController < ApplicationController class TagsController < ApplicationController
before_action :set_tag, only: [:show, :edit, :update, :destroy] before_action :set_tag, only: [:show, :edit, :update, :destroy, :merge_to, :move_to]
def index def index
end end
...@@ -48,12 +48,28 @@ class TagsController < ApplicationController ...@@ -48,12 +48,28 @@ class TagsController < ApplicationController
end end
end end
# このタグを他のタグにマージ
# すべてのPostを移動先のタグに移動し
# このタグを削除する
def merge_to def merge_to
@merge_to_tag = Tag.find_by(name: params[:merge_to_name]) or raise ActiveRecord::RecordNotFound @merge_to_tag = Tag.find_by(name: params[:merge_to_name]) or raise ActiveRecord::RecordNotFound
@tag.move_all_posts_to!(@merge_to_tag) @tag.move_all_posts_to!(@merge_to_tag)
@tag.delete @tag.delete
flash[:notice] = "「#{@tag.name}」は「#{@merge_to_tag.name}」にmergeされました"
render json: { status: 'OK' }
end
# このタグを他のタグの下に移動
def move_to
@move_to_tag = Tag.find_by(name: params[:move_to_name]) or raise ActiveRecord::RecordNotFound
@tag.set_parent!(@move_to_tag)
flash[:notice] = "「#{@tag.name}」は「#{@move_to_tag.name}」の下に移動しました"
render json: { status: 'OK' } render json: { status: 'OK' }
end end
......
...@@ -17,4 +17,10 @@ class Tag < ActiveRecord::Base ...@@ -17,4 +17,10 @@ class Tag < ActiveRecord::Base
_post.tags << other_tag unless _post.tags.include?(other_tag) _post.tags << other_tag unless _post.tags.include?(other_tag)
end end
end end
# 親タグを設定する
def set_parent!(other_tag)
self.parent = other_tag
self.save!
end
end end
...@@ -15,8 +15,7 @@ ...@@ -15,8 +15,7 @@
</button> </button>
<ul class="dropdown-menu" role="menu"> <ul class="dropdown-menu" role="menu">
<li><a href="#" data-toggle="modal" data-target="#modal-merge">Merge to...</a></li> <li><a href="#" data-toggle="modal" data-target="#modal-merge">Merge to...</a></li>
<li><a href="#" data-toggle="modal" data-target="#myModal">Rename to...</a></li> <li><a href="#" data-toggle="modal" data-target="#modal-moveUnder">Move this tag under...</a></li>
<li><a href="#" data-toggle="modal" data-target="#myModal">Open Tag Tree editor...</a></li>
</ul> </ul>
</div> </div>
</div> </div>
...@@ -52,8 +51,6 @@ ...@@ -52,8 +51,6 @@
<div class="modal-dialog"> <div class="modal-dialog">
<div class="modal-content"> <div class="modal-content">
<%= form_tag '#', method: :post, class: 'form-horizontal', role: 'form' do %>
<div class="modal-header"> <div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h3 class="modal-title" id="myModalLabel">Merge to other tag</h4> <h3 class="modal-title" id="myModalLabel">Merge to other tag</h4>
...@@ -66,7 +63,7 @@ ...@@ -66,7 +63,7 @@
</div> </div>
</div> </div>
<% content_for :footer_js do %> <% content_for :footer_js do %>
<script type="text/javascript"> <script type="text/javascript">
$(function(){ $(function(){
$('#merge-tag-tree a').on('click', function(e){ $('#merge-tag-tree a').on('click', function(e){
...@@ -91,15 +88,65 @@ ...@@ -91,15 +88,65 @@
}) })
}) })
</script> </script>
<% end %> <% end %>
<div class="modal-footer"> <div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div> </div>
</div><!-- /.modal-content --> </div><!-- /.modal-content -->
<% end %><%# form %>
</div><!-- /.modal-dialog --> </div><!-- /.modal-dialog -->
</div><!-- /.modal --> </div><!-- /.modal -->
<!-- Modal -->
<div class="modal fade" id="modal-moveUnder" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h3 class="modal-title" id="myModalLabel">Merge to other tag</h4>
</div>
<div class="modal-body">
<h4>マージ先のTagを選んでください</h4>
<div id="move-tag-tree">
<%= Tag.all.decorate.tree_view %>
</div>
</div>
<% content_for :footer_js do %>
<script type="text/javascript">
$(function(){
$('#move-tag-tree a').on('click', function(e){
console.log(e)
e.preventDefault();
var $this = $(this);
if(window.confirm('「<%= j @tag.name %>」を「' + $this.data('name') + '」の下に移動します')) {
$.ajax({
type: "POST",
url: "/tags/<%= @tag.name %>/move_to/" + encodeURIComponent($this.data('name'))
}).done(function(data){
location.href = "/tags/<%= j @tag.name %>";
}).fail(function(data){
alert('error');
});
} else {
return false;
}
})
})
</script>
<% end %>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
...@@ -10,6 +10,7 @@ Rendezvous::Application.routes.draw do ...@@ -10,6 +10,7 @@ Rendezvous::Application.routes.draw do
resources :posts resources :posts
post 'tags/:name/merge_to/:merge_to_name' => 'tags#merge_to', as: 'merge_to_tag' post 'tags/:name/merge_to/:merge_to_name' => 'tags#merge_to', as: 'merge_to_tag'
post 'tags/:name/move_to/:move_to_name' => 'tags#move_to', as: 'move_to_tag'
resources :tags, :param => :name resources :tags, :param => :name
devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' } devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
......
require 'spec_helper' require 'spec_helper'
describe Tag do describe Tag do
describe '#move_posts_to' do describe '#move_all_posts_to!' do
before :each do before :each do
@tag_ruby = Tag.create(name: 'ruby') @tag_ruby = Tag.create(name: 'ruby')
@tag_java = Tag.create(name: 'java') @tag_java = Tag.create(name: 'java')
...@@ -18,4 +18,19 @@ describe Tag do ...@@ -18,4 +18,19 @@ describe Tag do
expect(Tag.find_by(name: 'java').posts).to have(0).items expect(Tag.find_by(name: 'java').posts).to have(0).items
end end
end end
describe '#set_parent!' do
before :each do
@tag_ruby = Tag.create(name: 'ruby')
@tag_lang = Tag.create(name: 'lang')
end
it 'successfully moved' do
expect(@tag_ruby.parent).not_to eq(@tag_lang)
expect(@tag_lang.children).not_to include(@tag_ruby)
@tag_ruby.set_parent!(@tag_lang)
expect(@tag_ruby.parent).to eq(@tag_lang)
expect(@tag_lang.children).to include(@tag_ruby)
end
end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment