Skip to content

Commit

Permalink
Merge pull request #759 from CristinaRO/bookmark_tags_with_master
Browse files Browse the repository at this point in the history
Bookmark tags with master
  • Loading branch information
shalott committed Aug 8, 2012
2 parents fbc22ae + daf1e22 commit 30b77f3
Show file tree
Hide file tree
Showing 14 changed files with 553 additions and 147 deletions.
4 changes: 4 additions & 0 deletions app/controllers/tags_controller.rb
Expand Up @@ -209,6 +209,10 @@ def update
# so that the associations are there to move when the synonym is created
syn_string = params[:tag].delete(:syn_string)
@tag.attributes = params[:tag]
# Limiting the conditions under which you can update the tag type
if @tag.can_change_type? && %w(Fandom Character Relationship Freeform UnsortedTag).include?(params[:tag][:type])
@tag.type = params[:tag][:type]
end
@tag.syn_string = syn_string if @tag.save
if @tag.errors.empty? && @tag.save
setflash; flash[:notice] = ts('Tag was updated.')
Expand Down
31 changes: 31 additions & 0 deletions app/controllers/unsorted_tags_controller.rb
@@ -0,0 +1,31 @@
class UnsortedTagsController < ApplicationController

before_filter :check_user_status
before_filter :check_permission_to_wrangle

def index
@tags = UnsortedTag.page(params[:page])
@counts = {}
[Fandom, Character, Relationship, Freeform].each do |klass|
@counts[klass.to_s.downcase.pluralize.to_sym] = klass.unwrangled.in_use.count
end
end

def mass_update
unless params[:tags].blank?
params[:tags].delete_if {|tag_id, tag_type| tag_type.blank? }
tags = UnsortedTag.find(params[:tags].keys)
tags.each do |tag|
new_type = params[:tags][tag.id.to_s]
if %w(Fandom Character Relationship Freeform).include?(new_type)
tag.update_attribute(:type, new_type)
else
raise ts("#{new_type} is not a valid tag type")
end
end
flash[:notice] = ts("Tags were successfully sorted.")
end
redirect_to unsorted_tags_path
end

end
2 changes: 1 addition & 1 deletion app/helpers/tags_helper.rb
Expand Up @@ -165,7 +165,7 @@ def tag_comment_link(tag)

def show_wrangling_dashboard
can_wrangle? &&
(%w(tags tag_wranglings tag_wranglers tag_wrangling_requests).include?(controller.controller_name) ||
(%w(tags tag_wranglings tag_wranglers tag_wrangling_requests unsorted_tags).include?(controller.controller_name) ||
(@tag && controller.controller_name == 'comments'))
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/bookmark.rb
Expand Up @@ -122,7 +122,7 @@ def tag_string=(tag_string)
if tag
self.tags << tag
else
self.tags << Freeform.create(:name => string)
self.tags << UnsortedTag.create(:name => string)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/fandom.rb
Expand Up @@ -26,7 +26,7 @@ def check_media(media)

after_save :add_media_for_uncategorized
def add_media_for_uncategorized
if self.medias.empty?
if self.medias.empty? && self.type == "Fandom" # type could be something else if the tag is in the process of being re-categorised (re-sorted)
self.parents << Media.uncategorized
end
true
Expand Down
22 changes: 20 additions & 2 deletions app/models/tag.rb
Expand Up @@ -51,7 +51,7 @@ def commentable_owners
has_many :common_taggings, :foreign_key => 'common_tag_id', :dependent => :destroy
has_many :child_taggings, :class_name => 'CommonTagging', :as => :filterable
has_many :children, :through => :child_taggings, :source => :common_tag
has_many :parents, :through => :common_taggings, :source => :filterable, :source_type => 'Tag', :before_remove => :update_wrangler
has_many :parents, :through => :common_taggings, :source => :filterable, :source_type => 'Tag', :after_remove => :update_wrangler

has_many :meta_taggings, :foreign_key => 'sub_tag_id', :dependent => :destroy
has_many :meta_tags, :through => :meta_taggings, :source => :meta_tag, :before_remove => :remove_meta_filters
Expand Down Expand Up @@ -132,6 +132,18 @@ def update_wrangler(tag)
end
end

before_save :check_type_changes, :if => :type_changed?
def check_type_changes
# if the tag used to be a Fandom and is now something else, no parent type will fit, remove all parents
# if the tag had a type and is now an UnsortedTag, it can't be put into fandoms, so remove all parents
if self.type_was == "Fandom" || self.type == "UnsortedTag" && !self.type_was.nil?
self.parents = []
# if the tag has just become a Fandom, it needs the Uncategorized media added to it manually, and no other parents (the after_save hook on Fandom won't take effect, since it's not a Fandom yet)
elsif self.type == "Fandom" && !self.type_was.nil?
self.parents = [Media.uncategorized]
end
end

scope :id_only, select("tags.id")

scope :canonical, where(:canonical => true)
Expand Down Expand Up @@ -447,7 +459,8 @@ def self.find_or_create_by_name(new_name)
if new_name && new_name.is_a?(String)
new_name.squish!
tag = Tag.find_by_name(new_name)
if tag && tag.class == self
# if the tag exists and has the proper class, or it is an unsorted tag and it can be sorted to the self class
if tag && (tag.class == self || tag.class == UnsortedTag && tag = tag.recategorize(self.to_s))
tag
elsif tag
self.find_or_create_by_name(new_name + " - " + self.to_s)
Expand Down Expand Up @@ -486,6 +499,11 @@ def <=>(another_tag)
name.downcase <=> another_tag.name.downcase
end

# only allow changing the tag type for unwrangled tags not used in any tag sets or on any works
def can_change_type?
self.unwrangled? && self.set_taggings.count == 0 && self.works.count == 0
end

#### FILTERING ####

# Add any filter taggings that should exist but don't
Expand Down
13 changes: 13 additions & 0 deletions app/models/unsorted_tag.rb
@@ -0,0 +1,13 @@
class UnsortedTag < Tag

NAME = "Unsorted Tag"

# unsorted tags can have their type changed
# but they need to be reloaded to be seen as an instance of the proper subclass
def recategorize(new_type)
self.update_attribute(:type, new_type)
# return a new instance of the tag, with the correct class
Tag.find(self.id)
end

end
1 change: 1 addition & 0 deletions app/views/tag_wranglings/_wrangler_dashboard.html.erb
Expand Up @@ -8,6 +8,7 @@
<li><%= span_if_current('Discussion', discuss_tag_wranglings_path) %></li>
<li><%= span_if_current('Search Tags', search_tags_path) %></li>
<li><%= span_if_current('New Tag', new_tag_path) %></li>
<li><%= span_if_current(ts("Unsorted Tags (%{count})", :count => UnsortedTag.count), unsorted_tags_path) %></li>

<% if @counts %>
</ul>
Expand Down

0 comments on commit 30b77f3

Please sign in to comment.