Merge pull request #92 from srgpqt/filters
simplify and optimize Filters controller and template
This commit is contained in:
commit
65a9a77104
|
@ -5,49 +5,28 @@ class FiltersController < ApplicationController
|
|||
@cur_url = "/filters"
|
||||
@title = "Filtered Tags"
|
||||
|
||||
if @user
|
||||
@filtered_tags = @user.tag_filters.reload
|
||||
else
|
||||
@filtered_tags = tags_filtered_by_cookie.map{|t|
|
||||
tf = TagFilter.new
|
||||
tf.tag = t
|
||||
tf
|
||||
}
|
||||
end
|
||||
@tags = Tag.order(:tag).accessible_to(@user)
|
||||
|
||||
render :action => "index"
|
||||
if @user
|
||||
@filtered_tags = @user.tag_filter_tags.to_a
|
||||
else
|
||||
@filtered_tags = tags_filtered_by_cookie.to_a
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
new_filters = []
|
||||
|
||||
params.each do |k,v|
|
||||
if (m = k.match(/^tag_(.+)$/)) && v.to_i == 1 &&
|
||||
(t = Tag.where(:tag => m[1]).first) && t.valid_for?(@user)
|
||||
new_filters.push m[1]
|
||||
end
|
||||
end
|
||||
tags_param = params.permit(:tags => [])[:tags]
|
||||
new_tags = tags_param.blank? ? [] : Tag.where(:tag => tags_param).to_a
|
||||
new_tags.keep_if {|t| t.valid_for? @user }
|
||||
|
||||
if @user
|
||||
@user.tag_filters(:include => :tag).each do |tf|
|
||||
if tf.tag && new_filters.include?(tf.tag.tag)
|
||||
new_filters.reject!{|t| t == tf.tag.tag }
|
||||
else
|
||||
tf.destroy
|
||||
end
|
||||
end
|
||||
|
||||
new_filters.each do |t|
|
||||
tf = TagFilter.new
|
||||
tf.user_id = @user.id
|
||||
tf.tag_id = Tag.where(:tag => t).first.id
|
||||
tf.save!
|
||||
end
|
||||
@user.tag_filter_tags = new_tags
|
||||
else
|
||||
cookies.permanent[TAG_FILTER_COOKIE] = new_filters.join(",")
|
||||
cookies.permanent[TAG_FILTER_COOKIE] = new_tags.map(&:tag).join(",")
|
||||
end
|
||||
|
||||
flash[:success] = "Your filters have been updated."
|
||||
return redirect_to "/filters"
|
||||
|
||||
redirect_to filters_path
|
||||
end
|
||||
end
|
||||
|
|
|
@ -10,6 +10,10 @@ class Tag < ActiveRecord::Base
|
|||
user && user.is_admin?? all : where(:privileged => false)
|
||||
end
|
||||
|
||||
def to_param
|
||||
self.tag
|
||||
end
|
||||
|
||||
def self.all_with_filtered_counts_for(user)
|
||||
counts = TagFilter.group(:tag_id).count
|
||||
|
||||
|
|
|
@ -9,6 +9,11 @@ class User < ActiveRecord::Base
|
|||
:class_name => "Message",
|
||||
:foreign_key => "recipient_user_id"
|
||||
has_many :tag_filters
|
||||
has_many :tag_filter_tags,
|
||||
:class_name => "Tag",
|
||||
:through => :tag_filters,
|
||||
:source => :tag,
|
||||
:dependent => :delete_all
|
||||
belongs_to :invited_by_user,
|
||||
:class_name => "User"
|
||||
belongs_to :banned_by_user,
|
||||
|
|
|
@ -23,13 +23,13 @@
|
|||
<th width="15%">Tag</th>
|
||||
<th width="78%">Description</th>
|
||||
</tr>
|
||||
<% Tag.order(:tag).accessible_to(@user).each do |tag| %>
|
||||
<% @tags.each do |tag| %>
|
||||
<tr>
|
||||
<td><input type="checkbox" name="tag_<%= tag.tag %>" value=1
|
||||
<%= @filtered_tags.map{|ft| ft.tag_id }.include?(tag.id) ?
|
||||
"checked" : "" %>></td>
|
||||
<td><a href="/t/<%= tag.tag %>" class="<%= tag.css_class %>"><%=
|
||||
tag.tag %></a></td>
|
||||
<td>
|
||||
<%= check_box_tag "tags[]", tag.tag, @filtered_tags.include?(tag) %>
|
||||
</td>
|
||||
<td>
|
||||
<%= link_to tag.tag, tag_path(tag), :class => tag.css_class %>
|
||||
<td><%= tag.description %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
|
|
Loading…
Reference in a new issue