From 2c906efa7ef47dabe1e31c881a34f8f624bef3da Mon Sep 17 00:00:00 2001 From: Serge Paquet Date: Thu, 9 Jan 2014 01:44:03 -0500 Subject: [PATCH] simplify and optimize Filters controller and template --- app/controllers/filters_controller.rb | 47 ++++++++------------------- app/models/tag.rb | 4 +++ app/models/user.rb | 5 +++ app/views/filters/index.html.erb | 12 +++---- 4 files changed, 28 insertions(+), 40 deletions(-) diff --git a/app/controllers/filters_controller.rb b/app/controllers/filters_controller.rb index b398c13..c4d86d8 100644 --- a/app/controllers/filters_controller.rb +++ b/app/controllers/filters_controller.rb @@ -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 diff --git a/app/models/tag.rb b/app/models/tag.rb index d43b9e6..3565739 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index e12aa1a..4cd55ae 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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" diff --git a/app/views/filters/index.html.erb b/app/views/filters/index.html.erb index 581d24f..87a4b8b 100644 --- a/app/views/filters/index.html.erb +++ b/app/views/filters/index.html.erb @@ -23,13 +23,13 @@ Tag Description - <% Tag.order(:tag).accessible_to(@user).each do |tag| %> + <% @tags.each do |tag| %> - > - <%= - tag.tag %> + + <%= check_box_tag "tags[]", tag.tag, @filtered_tags.include?(tag) %> + + + <%= link_to tag.tag, tag_path(tag), :class => tag.css_class %> <%= tag.description %> <% end %>