Skip to content

Commit 5e97796

Browse files
committed
Merge pull request #99 from rueckerl/master
Replaced ActsAsTaggableOnSteroids with ActsAsTaggableOn.
2 parents 3c17dcd + 196d564 commit 5e97796

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+120
-1778
lines changed

Gemfile

+4-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ gem 'rails', '~> 4.2.4'
1010
gem 'sass-rails'
1111

1212
platforms :ruby do
13+
# gem 'pg'
1314
gem 'sqlite3'
1415
end
1516

@@ -35,7 +36,7 @@ gem 'jquery-rails'
3536
gem 'RedCloth', '~> 4.2.9', :require => 'redcloth'
3637
gem 'ruby-openid', :require => 'openid'
3738
gem 'rack-openid', :require => 'rack/openid'
38-
gem 'aaronh-chronic', :require => 'chronic' # Fixes for 1.9.2
39+
gem 'chronic'
3940
gem 'coderay', '~> 1.0.5'
4041
gem 'lesstile', '~> 1.1.0'
4142
gem 'formtastic'
@@ -44,6 +45,7 @@ gem 'exception_notification', '~> 2.5.2'
4445
gem 'omniauth'
4546
gem 'omniauth-google-oauth2'
4647
gem 'omniauth-openid'
48+
gem 'acts-as-taggable-on', '~> 3.5'
4749

4850
# Bundle gems for the local environment. Make sure to
4951
# put test-only gems in this group so their generators
@@ -54,13 +56,7 @@ group :test do
5456
gem 'cucumber-websteps', :require => false
5557
gem 'factory_girl'
5658
gem 'rspec'
57-
58-
# Temporary fix for current compatibility issue with Rails 4.2.x and rspec-activemodel-mocks. Should be able to go
59-
# back to the standard rspec-activemodel-mocks gem once this issue is resolved:
60-
# https://github.com/rspec/rspec-activemodel-mocks/pull/10
61-
gem 'rspec-activemodel-mocks', :git => 'https://github.com/jdelStrother/rspec-activemodel-mocks.git',
62-
:branch => 'read_attribute'
63-
59+
gem 'rspec-activemodel-mocks'
6460
gem 'rspec-collection_matchers'
6561
gem 'nokogiri', '~> 1.6.0'
6662
gem 'webrat'

app/helpers/navigation_helper.rb

+11-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,16 @@ def category_links_for_navigation
1515

1616
def get_tags_for_published_posts
1717
published_posts = Post.where.not(:published_at => nil)
18-
published_tag_names = published_posts.collect { |post| post.cached_tag_list.split(',') }.flatten.uniq
19-
published_tag_names.each { |tag| tag.strip! }
20-
Tag.where(:name => published_tag_names).sort_by { |tag| tag.taggings.size }.reverse
18+
published_tag_names = published_posts.collect{ |post|
19+
unless post.cached_tag_list.nil?
20+
post.cached_tag_list.split(',')
21+
end
22+
}.flatten.uniq
23+
published_tag_names.each do |tag|
24+
unless tag.nil?
25+
tag.strip!
26+
end
27+
end
28+
ActsAsTaggableOn::Tag.where(:name => published_tag_names).sort_by { |tag| tag.taggings.size }.reverse
2129
end
2230
end

app/models/post.rb

+9-10
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ def build_for_preview(params)
4444
post.generate_slug
4545
post.set_dates
4646
post.apply_filter
47-
TagList.from(params[:tag_list]).each do |tag|
48-
post.tags << Tag.new(:name => tag)
47+
post.tag_list.each do |tag|
48+
post.tags << ActsAsTaggableOn::Tag.find_or_create_with_like_by_name(tag)
4949
end
5050
post
5151
end
@@ -57,14 +57,11 @@ def find_recent(options = {})
5757
conditions = ['published_at < ?', Time.zone.now]
5858
limit = options[:limit] ||= DEFAULT_LIMIT
5959

60-
options = {
61-
:order => order,
62-
:conditions => conditions,
63-
:limit => limit
64-
}.merge(options)
65-
6660
if tag
67-
find_tagged_with(tag, options)
61+
result = Post.tagged_with(tag)
62+
result = result.where(conditions)
63+
result = result.includes(:tags) if include_tags
64+
result.order(order).limit(limit)
6865
else
6966
result = where(conditions)
7067
result = result.includes(:tags) if include_tags
@@ -138,7 +135,9 @@ def generate_slug
138135

139136
def tag_list=(value)
140137
value = value.split(',') if value.is_a?(String)
141-
value.map!{ |tag_name| Tag::filter_name(tag_name) }
138+
value.map!{ |tag_name| tag_name.gsub!('&', 'and')
139+
tag_name.gsub!(/[^A-Za-z0-9_ \.-]/, '')
140+
tag_name }
142141

143142
# TODO: Contribute this back to acts_as_taggable_on_steroids plugin
144143
value = value.join(", ") if value.respond_to?(:join)

app/models/stats.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ def comment_count
88
end
99

1010
def tag_count
11-
Tag.count
11+
ActsAsTaggableOn::Tag.count
1212
end
1313
end

app/models/tag.rb

-43
This file was deleted.

app/models/tagging.rb

-12
This file was deleted.

app/views/admin/posts/_form.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<%= form.inputs do -%>
22
<%= form.input :title -%>
33
<%= form.input :body, :hint => "<a href='http://redcloth.org/textile'>Textile enabled</a>. Use Ctrl+E to switch between preview and edit mode.".html_safe -%>
4-
<%= form.input :tag_list, :as => 'string', :required => false, :hint => 'Comma separated: ruby, rails&hellip;'.html_safe -%>
4+
<%= form.input :tag_list, :input_html => { :value => @post.tag_list.join(', ')}, :as => 'string', :required => false, :hint => 'Comma separated: ruby, rails&hellip;'.html_safe -%>
55
<% end -%>
66
<%= form.inputs do -%>
77
<%= form.input :published_at_natural, :label => 'Published at', :as => 'string', :hint => 'Examples: now, yesterday, 1 hour from now, '.html_safe + link_to("etc".html_safe, "http://chronic.rubyforge.org/") + '. Leave blank for an unpublished draft.' -%>

config/initializers/acts_as_taggable_on_steroids.rb

-11
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class UpdateTaggingTables < ActiveRecord::Migration
2+
def up
3+
#update tables
4+
change_column_null :tags, :taggings_count, true
5+
6+
change_table :taggings do |t|
7+
#t.references :tag
8+
9+
# You should make sure that the column created is
10+
# long enough to store the required class names.
11+
# t.references :taggable, polymorphic: true
12+
t.references :tagger, polymorphic: true
13+
t.string :taggable_type, null: false, default: 'Post'
14+
15+
# Limit is created to prevent MySQL error on index
16+
# length for MyISAM table type: http://bit.ly/vgW2Ql
17+
t.string :context, limit: 128, default: 'tags', null: false
18+
19+
#t.datetime :created_at
20+
end
21+
# set default before to force the db filling this field
22+
change_column_default(:taggings, :taggable_type, nil)
23+
change_column_default(:taggings, :context, nil)
24+
25+
if ActsAsTaggableOn::Utils.using_mysql?
26+
execute("ALTER TABLE tags MODIFY name varchar(255) CHARACTER SET utf8 COLLATE utf8_bin;")
27+
end
28+
29+
#drop old index structures
30+
ActiveRecord::Base.connection.indexes(:taggings).each do |index|
31+
remove_index :taggings, :name => index.name
32+
end
33+
ActiveRecord::Base.connection.indexes(:tags).each do |index|
34+
remove_index :tags, :name => index.name
35+
end
36+
37+
#create new index structures
38+
39+
add_index :tags, :name, unique: true
40+
add_index :taggings,
41+
[:tag_id, :taggable_id, :taggable_type, :context, :tagger_id, :tagger_type],
42+
unique: true, name: 'taggings_idx'
43+
add_index :taggings, [:taggable_id, :taggable_type, :context]
44+
45+
#update data
46+
#TODO: test what this really does
47+
ActsAsTaggableOn::Tag.reset_column_information
48+
ActsAsTaggableOn::Tag.find_each do |tag|
49+
ActsAsTaggableOn::Tag.reset_counters(tag.id, :taggings)
50+
end
51+
end
52+
53+
def down
54+
raise ActiveRecord::IrreversibleMigration
55+
end
56+
end

db/schema.rb

+17-13
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
#
1212
# It's strongly recommended that you check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(version: 20150414113518) do
14+
ActiveRecord::Schema.define(version: 20151220095617) do
1515

16-
create_table "comments", force: true do |t|
16+
create_table "comments", force: :cascade do |t|
1717
t.integer "post_id", null: false
1818
t.string "author", null: false
1919
t.string "author_url", null: false
@@ -27,7 +27,7 @@
2727
add_index "comments", ["created_at"], name: "index_comments_on_created_at"
2828
add_index "comments", ["post_id"], name: "index_comments_on_post_id"
2929

30-
create_table "omni_auth_details", force: true do |t|
30+
create_table "omni_auth_details", force: :cascade do |t|
3131
t.string "provider", null: false
3232
t.string "uid"
3333
t.text "info"
@@ -37,7 +37,7 @@
3737
t.datetime "updated_at"
3838
end
3939

40-
create_table "pages", force: true do |t|
40+
create_table "pages", force: :cascade do |t|
4141
t.string "title", null: false
4242
t.string "slug", null: false
4343
t.text "body", null: false
@@ -50,7 +50,7 @@
5050
add_index "pages", ["slug"], name: "pages_slug_unique_idx"
5151
add_index "pages", ["title"], name: "index_pages_on_title"
5252

53-
create_table "posts", force: true do |t|
53+
create_table "posts", force: :cascade do |t|
5454
t.string "title", null: false
5555
t.string "slug", null: false
5656
t.text "body", null: false
@@ -67,7 +67,7 @@
6767
add_index "posts", ["published_at"], name: "index_posts_on_published_at"
6868
add_index "posts", ["slug"], name: "posts_slug_unique_idx"
6969

70-
create_table "sessions", force: true do |t|
70+
create_table "sessions", force: :cascade do |t|
7171
t.string "session_id", null: false
7272
t.text "data"
7373
t.datetime "created_at"
@@ -77,23 +77,27 @@
7777
add_index "sessions", ["session_id"], name: "index_sessions_on_session_id"
7878
add_index "sessions", ["updated_at"], name: "index_sessions_on_updated_at"
7979

80-
create_table "taggings", force: true do |t|
80+
create_table "taggings", force: :cascade do |t|
8181
t.integer "tag_id"
8282
t.integer "taggable_id"
8383
t.datetime "created_at"
84+
t.integer "tagger_id"
85+
t.string "tagger_type"
86+
t.string "taggable_type", null: false
87+
t.string "context", limit: 128
8488
end
8589

86-
add_index "taggings", ["tag_id"], name: "index_taggings_on_tag_id"
87-
add_index "taggings", ["taggable_id"], name: "index_taggings_on_taggable_id_and_taggable_type"
90+
add_index "taggings", ["tag_id", "taggable_id", "taggable_type", "context", "tagger_id", "tagger_type"], name: "taggings_idx", unique: true
91+
add_index "taggings", ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context"
8892

89-
create_table "tags", force: true do |t|
93+
create_table "tags", force: :cascade do |t|
9094
t.string "name"
91-
t.integer "taggings_count", default: 0, null: false
95+
t.integer "taggings_count", default: 0
9296
end
9397

94-
add_index "tags", ["name"], name: "index_tags_on_name"
98+
add_index "tags", ["name"], name: "index_tags_on_name", unique: true
9599

96-
create_table "undo_items", force: true do |t|
100+
create_table "undo_items", force: :cascade do |t|
97101
t.string "type", null: false
98102
t.datetime "created_at", null: false
99103
t.text "data"

features/step_definitions/factories.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'factory_girl'
22

33
FactoryGirl.define do
4-
factory :tag do
4+
factory :tag, :class => ActsAsTaggableOn::Tag do
55
name 'Tag'
66
end
77

0 commit comments

Comments
 (0)