Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #12 from dougcole/index_tags

Index tags
  • Loading branch information...
commit 679ad7e255ff2ad5e14baac3835e451f78a65cb6 2 parents 41bd24b + 7165e7f
@aross aross authored
View
2  Gemfile
@@ -81,7 +81,7 @@ gem 'will_paginate', '~> 3.0.pre2'
gem "validates_captcha"
gem "okkez-open_id_authentication"
-gem "acts-as-taggable-on", :git => 'git://github.com/mbleigh/acts-as-taggable-on.git'
+gem "acts-as-taggable-on", :git => 'git://github.com/dougcole/acts-as-taggable-on.git', :branch => 'use_equality_where_possible'
gem 'mechanize'
#gem 'formageddon', '0.0.0', :require => 'formageddon', :path => '/Users/aross/Sites/formageddon'
View
14 Gemfile.lock
@@ -1,14 +1,16 @@
GIT
- remote: git://github.com/galetahub/simple-captcha.git
- revision: b8493942e9a9bb58c58712fa21d41963330da6aa
+ remote: git://github.com/dougcole/acts-as-taggable-on.git
+ revision: cf52b870c07e8d6825a478a9669c9e384f580a32
+ branch: use_equality_where_possible
specs:
- simple_captcha (0.1.1)
+ acts-as-taggable-on (2.2.2)
+ rails (~> 3.0)
GIT
- remote: git://github.com/mbleigh/acts-as-taggable-on.git
- revision: 2752cfef4c66318d0925fbe880d5392ad188bb50
+ remote: git://github.com/galetahub/simple-captcha.git
+ revision: b8493942e9a9bb58c58712fa21d41963330da6aa
specs:
- acts-as-taggable-on (2.0.6)
+ simple_captcha (0.1.1)
GIT
remote: git://github.com/opencongress/formageddon.git
View
6 app/models/bill.rb
@@ -757,7 +757,13 @@ def unique_referrers(since = 2.days)
ref_views.collect { |v| v.referrer }
end
+ def related_articles
+ Article.tagged_with(subject_terms, :order => 'created_at DESC', :limit => 5)
+ end
+ def subject_terms
+ subjects.collect{|s| s.term}.join(',')
+ end
def subject
#most popular subject that is not in the top X
View
4 app/views/bill/_related_oc_blog_articles.html.erb
@@ -1,4 +1,4 @@
-<% articles = Article.tagged_with("#{@bill.subjects.collect{|s| s.term}.join(',')}", { :order => 'created_at DESC', :limit => 5 })%>
+<% articles = @bill.related_articles %>
<% unless articles.empty? %>
<div class="sidebar_recent_articles">
@@ -14,4 +14,4 @@
<% end %>
</ul>
</div>
-<% end %>
+<% end %>
View
67 config/initializers/acts_as_taggable_fix.rb
@@ -1,67 +0,0 @@
-module ActsAsTaggableOn::Taggable
- module Core
- module ClassMethods
- def tagged_with(tags, options = {})
- tag_list = ActsAsTaggableOn::TagList.from(tags)
-
- return {} if tag_list.empty?
-
- joins = []
- conditions = []
-
- context = options.delete(:on)
-
- if options.delete(:exclude)
- tags_conditions = tag_list.map { |t| sanitize_sql(["#{ActsAsTaggableOn::Tag.table_name}.name ILIKE ?", t]) }.join(" OR ")
- conditions << "#{table_name}.#{primary_key} NOT IN (SELECT #{ActsAsTaggableOn::Tagging.table_name}.taggable_id FROM #{ActsAsTaggableOn::Tagging.table_name} JOIN #{ActsAsTaggableOn::Tag.table_name} ON #{ActsAsTaggableOn::Tagging.table_name}.tag_id = #{ActsAsTaggableOn::Tag.table_name}.id AND (#{tags_conditions}) WHERE #{ActsAsTaggableOn::Tagging.table_name}.taggable_type = #{quote_value(base_class.name)})"
-
- elsif options.delete(:any)
- tags_conditions = tag_list.map { |t| sanitize_sql(["#{ActsAsTaggableOn::Tag.table_name}.name ILIKE ?", t]) }.join(" OR ")
- conditions << "#{table_name}.#{primary_key} IN (SELECT #{ActsAsTaggableOn::Tagging.table_name}.taggable_id FROM #{ActsAsTaggableOn::Tagging.table_name} JOIN #{ActsAsTaggableOn::Tag.table_name} ON #{ActsAsTaggableOn::Tagging.table_name}.tag_id = #{ActsAsTaggableOn::Tag.table_name}.id AND (#{tags_conditions}) WHERE #{ActsAsTaggableOn::Tagging.table_name}.taggable_type = #{quote_value(base_class.name)})"
-
- else
- tags = ActsAsTaggableOn::Tag.named_any(tag_list)
- return scoped(:conditions => "1 = 0") unless tags.length == tag_list.length
-
- tags.each do |tag|
- safe_tag = tag.name.gsub(/[^a-zA-Z0-9]/, '')
- prefix = "#{safe_tag}_#{rand(1024)}"
-
- taggings_alias = "#{undecorated_table_name}_taggings_#{prefix}"
-
- tagging_join = "JOIN #{ActsAsTaggableOn::Tagging.table_name} #{taggings_alias}" +
- " ON #{taggings_alias}.taggable_id = #{table_name}.#{primary_key}" +
- " AND #{taggings_alias}.taggable_type = #{quote_value(base_class.name)}" +
- " AND #{taggings_alias}.tag_id = #{tag.id}"
- tagging_join << " AND " + sanitize_sql(["#{taggings_alias}.context = ?", context.to_s]) if context
-
- joins << tagging_join
- end
- end
-
- taggings_alias, tags_alias = "#{undecorated_table_name}_taggings_group", "#{undecorated_table_name}_tags_group"
-
- if options.delete(:match_all)
- joins << "LEFT OUTER JOIN #{ActsAsTaggableOn::Tagging.table_name} #{taggings_alias}" +
- " ON #{taggings_alias}.taggable_id = #{table_name}.#{primary_key}" +
- " AND #{taggings_alias}.taggable_type = #{quote_value(base_class.name)}"
-
-
- group_columns = ActsAsTaggableOn::Tag.using_postgresql? ? grouped_column_names_for(self) : "#{table_name}.#{primary_key}"
- group = "#{group_columns} HAVING COUNT(#{taggings_alias}.taggable_id) = #{tags.size}"
- end
-
-
- scoped(:joins => joins.join(" "),
- :group => group,
- :conditions => conditions.join(" AND "),
- :order => options[:order],
- :readonly => false)
- end
-
- def is_taggable?
- true
- end
- end
- end
-end
View
9 db/migrate/20120223065756_index_tags.rb
@@ -0,0 +1,9 @@
+class IndexTags < ActiveRecord::Migration
+ def self.up
+ execute "CREATE INDEX index_lower_tag_names ON tags (lower(name))"
+ end
+
+ def self.down
+ execute "DROP INDEX index_lower_tag_names"
+ end
+end
View
35 spec/models/bill_spec.rb
@@ -10,5 +10,40 @@
Bill.long_type_to_short("Fake Bill Type").should be_nil
end
end
+
+ describe "related_articles" do
+ let(:bill) { Bill.new }
+
+ before(:each) do
+ @article = Article.create!
+ @article.tag_list = 'foo,bar,baz'
+ @article.save!
+ end
+
+ it "finds related articles" do
+ bill.stub(:subject_terms).and_return("foo")
+ bill.related_articles.should == [@article]
+ end
+
+ it "can match on multiple tags" do
+ bill.stub(:subject_terms).and_return("foo,bar")
+ bill.related_articles.should == [@article]
+ end
+
+ it "must match all of a bill's subjects" do
+ bill.stub(:subject_terms).and_return("foo,bar,other")
+ bill.related_articles.should be_empty
+ end
+
+ it "won't match if there are no matching tags" do
+ bill.stub(:subject_terms).and_return("other")
+ bill.related_articles.should be_empty
+ end
+
+ it "won't match if there are no tags" do
+ bill.stub(:subject_terms).and_return("")
+ bill.related_articles.should be_empty
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.