Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests refactored to use rspec 3 + lot of fixes #511

Merged
merged 23 commits into from
Apr 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/pkg/*
.bundle
.ruby-version
spec/database.yml
spec/internal/config/database.yml
tmp*.sw?
*.sw?
tmp
Expand Down
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ rvm:
- 2.0.0
- 2.1.0
- 2.1.1
- rbx-2
env:
- DB=sqlite3
- DB=mysql
Expand All @@ -14,8 +15,10 @@ gemfile:
- gemfiles/rails_edge.gemfile
cache: bundler
script: bundle exec rake
before_install:
- gem install bundler
before_install: gem install bundler
bundler_args: '--without local_development'
matrix:
fast_finish: true
allow_failures:
- gemfile: gemfiles/rails_edge.gemfile
- rvm: rbx-2
2 changes: 1 addition & 1 deletion Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ appraise "rails-4.0" do
end

appraise "rails-4.1" do
gem "rails", "~> 4.1.0.beta1"
gem "rails", "~> 4.1.0"
end

appraise "rails-edge" do
Expand Down
22 changes: 5 additions & 17 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
require 'rubygems'
begin
require 'bundler/setup'
rescue LoadError
STDERR.puts "Bundler not loaded"
end
require 'bundler/setup'

desc 'Default: run specs'
task :default => :spec
task default: :spec

desc 'Copy sample spec database.yml over if not exists'
task :copy_db_config do
cp 'spec/database.yml.sample', 'spec/database.yml'
cp 'spec/internal/config/database.yml.sample', 'spec/internal/config/database.yml'
end

task :spec => [:copy_db_config]

begin
require 'appraisal'
desc 'Run tests across gemfiles specified in Appraisals'
task :appraise => ['appraisal:cleanup', 'appraisal:install', 'appraisal']
rescue LoadError
puts "appraisal tasks not available"
end
task spec: [:copy_db_config]

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new do |t|
t.pattern = "spec/**/*_spec.rb"
t.pattern = 'spec/**/*_spec.rb'
end

Bundler::GemHelper.install_tasks
5 changes: 3 additions & 2 deletions acts-as-taggable-on.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'mysql2', '~> 0.3.7'
gem.add_development_dependency 'pg'

gem.add_development_dependency 'rspec-rails', '2.13.0' # 2.13.1 is broken
gem.add_development_dependency 'rspec', '~> 2.6'
gem.add_development_dependency 'rspec-rails' , '~> 3.0.0.beta'
gem.add_development_dependency 'rspec-its'
gem.add_development_dependency 'rspec'
gem.add_development_dependency 'ammeter'
gem.add_development_dependency 'barrier'
end
6 changes: 3 additions & 3 deletions db/migrate/1_acts_as_taggable_on_migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ def self.up

# You should make sure that the column created is
# long enough to store the required class names.
t.references :taggable, :polymorphic => true
t.references :tagger, :polymorphic => true
t.references :taggable, polymorphic: true
t.references :tagger, polymorphic: true

# Limit is created to prevent MySQL error on index
# length for MyISAM table type: http://bit.ly/vgW2Ql
t.string :context, :limit => 128
t.string :context, limit: 128

t.datetime :created_at
end
Expand Down
8 changes: 3 additions & 5 deletions db/migrate/2_add_missing_unique_indices.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
class AddMissingUniqueIndices < ActiveRecord::Migration

def self.up
add_index :tags, :name, unique: true

remove_index :taggings, :tag_id
remove_index :taggings, [:taggable_id, :taggable_type, :context]
add_index :taggings,
[:tag_id, :taggable_id, :taggable_type, :context, :tagger_id, :tagger_type],
unique: true, name: 'taggings_idx'
end
[:tag_id, :taggable_id, :taggable_type, :context, :tagger_id, :tagger_type],
unique: true, name: 'taggings_idx'
end

def self.down
remove_index :tags, :name
Expand All @@ -17,5 +16,4 @@ def self.down
add_index :taggings, :tag_id
add_index :taggings, [:taggable_id, :taggable_type, :context]
end

end
3 changes: 2 additions & 1 deletion db/migrate/3_add_taggings_counter_cache_to_tags.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
class AddTaggingsCounterCacheToTags < ActiveRecord::Migration
def self.up
add_column :tags, :taggings_count, :integer, :default => 0
add_column :tags, :taggings_count, :integer, default: 0

ActsAsTaggableOn::Tag.reset_column_information
ActsAsTaggableOn::Tag.find_each do |tag|
ActsAsTaggableOn::Tag.reset_counters(tag.id, :taggings)
end
Expand Down
9 changes: 8 additions & 1 deletion gemfiles/rails_3.2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,11 @@ source "https://rubygems.org"

gem "rails", "~> 3.2"

gemspec :path=>"../"
group :local_development do
gem "guard"
gem "guard-rspec"
gem "appraisal"
gem "rake"
end

gemspec :path => "../"
9 changes: 8 additions & 1 deletion gemfiles/rails_4.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,11 @@ source "https://rubygems.org"

gem "rails", "~> 4.0"

gemspec :path=>"../"
group :local_development do
gem "guard"
gem "guard-rspec"
gem "appraisal"
gem "rake"
end

gemspec :path => "../"
11 changes: 9 additions & 2 deletions gemfiles/rails_4.1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

source "https://rubygems.org"

gem "rails", "~> 4.1.0.beta1"
gem "rails", "~> 4.1.0"

gemspec :path=>"../"
group :local_development do
gem "guard"
gem "guard-rspec"
gem "appraisal"
gem "rake"
end

gemspec :path => "../"
11 changes: 9 additions & 2 deletions gemfiles/rails_edge.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

source "https://rubygems.org"

gem "rails", :github=>"rails/rails"
gem "rails", :github => "rails/rails"

gemspec :path=>"../"
group :local_development do
gem "guard"
gem "guard-rspec"
gem "appraisal"
gem "rake"
end

gemspec :path => "../"
41 changes: 20 additions & 21 deletions lib/acts-as-taggable-on.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require "active_record"
require "active_record/version"
require "active_support/core_ext/module"
require "action_view"
require 'active_record'
require 'active_record/version'
require 'active_support/core_ext/module'
require 'action_view'

require "digest/sha1"
require 'digest/sha1'

module ActsAsTaggableOn
class DuplicateTagError < StandardError
Expand All @@ -26,7 +26,7 @@ def self.respond_to?(method_name, include_private=false)
def self.glue
setting = @configuration.delimiter
delimiter = setting.kind_of?(Array) ? setting[0] : setting
delimiter.ends_with?(" ") ? delimiter : "#{delimiter} "
delimiter.ends_with?(' ') ? delimiter : "#{delimiter} "
end

class Configuration
Expand All @@ -45,23 +45,22 @@ def initialize
setup
end

require 'acts_as_taggable_on/utils'

require "acts_as_taggable_on/utils"
require 'acts_as_taggable_on/taggable'
require 'acts_as_taggable_on/acts_as_taggable_on/compatibility'
require 'acts_as_taggable_on/acts_as_taggable_on/core'
require 'acts_as_taggable_on/acts_as_taggable_on/collection'
require 'acts_as_taggable_on/acts_as_taggable_on/cache'
require 'acts_as_taggable_on/acts_as_taggable_on/ownership'
require 'acts_as_taggable_on/acts_as_taggable_on/related'
require 'acts_as_taggable_on/acts_as_taggable_on/dirty'

require "acts_as_taggable_on/taggable"
require "acts_as_taggable_on/acts_as_taggable_on/compatibility"
require "acts_as_taggable_on/acts_as_taggable_on/core"
require "acts_as_taggable_on/acts_as_taggable_on/collection"
require "acts_as_taggable_on/acts_as_taggable_on/cache"
require "acts_as_taggable_on/acts_as_taggable_on/ownership"
require "acts_as_taggable_on/acts_as_taggable_on/related"
require "acts_as_taggable_on/acts_as_taggable_on/dirty"

require "acts_as_taggable_on/tagger"
require "acts_as_taggable_on/tag"
require "acts_as_taggable_on/tag_list"
require "acts_as_taggable_on/tags_helper"
require "acts_as_taggable_on/tagging"
require 'acts_as_taggable_on/tagger'
require 'acts_as_taggable_on/tag'
require 'acts_as_taggable_on/tag_list'
require 'acts_as_taggable_on/tags_helper'
require 'acts_as_taggable_on/tagging'
require 'acts_as_taggable_on/engine'

ActiveSupport.on_load(:active_record) do
Expand Down
5 changes: 1 addition & 4 deletions lib/acts_as_taggable_on/acts_as_taggable_on/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ def _add_tags_caching_methods
def columns
@acts_as_taggable_on_cache_columns ||= begin
db_columns = super
if _has_tags_cache_columns?(db_columns)
_add_tags_caching_methods
end
_add_tags_caching_methods if _has_tags_cache_columns?(db_columns)
db_columns
end
end
Expand Down Expand Up @@ -79,6 +77,5 @@ def save_cached_tag_list
true
end
end

end
end
30 changes: 14 additions & 16 deletions lib/acts_as_taggable_on/acts_as_taggable_on/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ def #{tag_type.singularize}_counts(options = {})
end

def top_#{tag_type}(limit = 10)
tag_counts_on('#{tag_type}', :order => 'count desc', :limit => limit.to_i)
tag_counts_on('#{tag_type}', order: 'count desc', limit: limit.to_i)
end

def self.top_#{tag_type}(limit = 10)
tag_counts_on('#{tag_type}', :order => 'count desc', :limit => limit.to_i)
tag_counts_on('#{tag_type}', order: 'count desc', limit: limit.to_i)
end
RUBY
end
Expand All @@ -34,11 +34,11 @@ def acts_as_taggable_on(*args)
end

def tag_counts_on(context, options = {})
all_tag_counts(options.merge({:on => context.to_s}))
all_tag_counts(options.merge({on: context.to_s}))
end

def tags_on(context, options = {})
all_tags(options.merge({:on => context.to_s}))
all_tags(options.merge({on: context.to_s}))
end

##
Expand All @@ -64,7 +64,7 @@ def all_tags(options = {})

# Joins and conditions
tagging_conditions(options).each { |condition| tagging_scope = tagging_scope.where(condition) }
tag_scope = tag_scope.where(options[:conditions])
tag_scope = tag_scope.where(options[:conditions])

group_columns = "#{ActsAsTaggableOn::Tagging.table_name}.tag_id"

Expand All @@ -75,7 +75,6 @@ def all_tags(options = {})
tag_scope_joins(tag_scope, tagging_scope)
end


##
# Calculate the tag counts for all tags.
#
Expand All @@ -98,15 +97,14 @@ def all_tag_counts(options = {})
taggable_join = "INNER JOIN #{table_name} ON #{table_name}.#{primary_key} = #{ActsAsTaggableOn::Tagging.table_name}.taggable_id"
taggable_join << " AND #{table_name}.#{inheritance_column} = '#{name}'" unless descends_from_active_record? # Current model is STI descendant, so add type checking to the join condition


## Generate scope:
tagging_scope = ActsAsTaggableOn::Tagging.select("#{ActsAsTaggableOn::Tagging.table_name}.tag_id, COUNT(#{ActsAsTaggableOn::Tagging.table_name}.tag_id) AS tags_count")
tag_scope = ActsAsTaggableOn::Tag.select("#{ActsAsTaggableOn::Tag.table_name}.*, #{ActsAsTaggableOn::Tagging.table_name}.tags_count AS count").order(options[:order]).limit(options[:limit])

# Joins and conditions
tagging_scope = tagging_scope.joins(taggable_join)
tagging_conditions(options).each { |condition| tagging_scope = tagging_scope.where(condition) }
tag_scope = tag_scope.where(options[:conditions])
tag_scope = tag_scope.where(options[:conditions])

# GROUP BY and HAVING clauses:
having = ["COUNT(#{ActsAsTaggableOn::Tagging.table_name}.tag_id) > 0"]
Expand All @@ -128,21 +126,21 @@ def all_tag_counts(options = {})
end

def safe_to_sql(relation)
connection.respond_to?(:unprepared_statement) ? connection.unprepared_statement{relation.to_sql} : relation.to_sql
connection.respond_to?(:unprepared_statement) ? connection.unprepared_statement { relation.to_sql } : relation.to_sql
end

private

def tagging_conditions(options)
tagging_conditions = []
tagging_conditions.push sanitize_sql(["#{ActsAsTaggableOn::Tagging.table_name}.created_at <= ?", options.delete(:end_at)]) if options[:end_at]
tagging_conditions.push sanitize_sql(["#{ActsAsTaggableOn::Tagging.table_name}.created_at <= ?", options.delete(:end_at)]) if options[:end_at]
tagging_conditions.push sanitize_sql(["#{ActsAsTaggableOn::Tagging.table_name}.created_at >= ?", options.delete(:start_at)]) if options[:start_at]

taggable_conditions = sanitize_sql(["#{ActsAsTaggableOn::Tagging.table_name}.taggable_type = ?", base_class.name])
taggable_conditions = sanitize_sql(["#{ActsAsTaggableOn::Tagging.table_name}.taggable_type = ?", base_class.name])
taggable_conditions << sanitize_sql([" AND #{ActsAsTaggableOn::Tagging.table_name}.context = ?", options.delete(:on).to_s]) if options[:on]
taggable_conditions << sanitize_sql([" AND #{ActsAsTaggableOn::Tagging.table_name}.taggable_id = ?", options[:id]]) if options[:id]
taggable_conditions << sanitize_sql([" AND #{ActsAsTaggableOn::Tagging.table_name}.taggable_id = ?", options[:id]]) if options[:id]

tagging_conditions.push taggable_conditions
tagging_conditions.push taggable_conditions

tagging_conditions
end
Expand All @@ -154,13 +152,13 @@ def tag_scope_joins(tag_scope, tagging_scope)
end

def tag_counts_on(context, options={})
self.class.tag_counts_on(context, options.merge(:id => id))
self.class.tag_counts_on(context, options.merge(id: id))
end

module CalculationMethods
def count
def count(column_name=:all)
# https://github.com/rails/rails/commit/da9b5d4a8435b744fcf278fffd6d7f1e36d4a4f2
super(:all)
super
end
end
end
Expand Down
Loading