Skip to content

Commit

Permalink
Integrated act_as_paranoid and added ignore_deleted option
Browse files Browse the repository at this point in the history
Integrated act_as_paranoid and added ignore_deleted option

Integrated act_as_paranoid and added ignore_deleted option

Integrated act_as_paranoid and added ignore_deleted option

Integrated act_as_paranoid and added ignore_deleted option

Integrated act_as_paranoid and added ignore_deleted option

Integrated act_as_paranoid and added ignore_deleted option

Integrated act_as_paranoid and added ignore_deleted option

Integrated act_as_paranoid and added ignore_deleted option

Updated CHANGELOG

Fixed Gem::LoadError:   Specified 'sqlite3'

Fixed spec

Removed sqlite3 from gem spec

Fixed incorrect tagging alias table

Refactor specs

Added sqlite3 to Gemfile

Fixed sqlite3 version for active_record 5.0

ref Remove 'acts_as_paranoid' as dependency and add to Gemfile

Transferred acts_as_paranoid to invidividual active record versions
  • Loading branch information
RoRroland authored and rolandoalvarado committed Jun 6, 2019
1 parent fbf2b60 commit 8e49837
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ As such, _Breaking Changes_ are major. _Features_ would map to either major or m
### [Master / Unreleased](https://github.com/mbleigh/acts-as-taggable-on/compare/v6.0.1...master)
* Features
* [@mizukami234 @junmoka Make table names configurable](https://github.com/mbleigh/acts-as-taggable-on/pull/910)
* [@rolandoalvarado Integrated act_as_paranoid and added ignore_deleted option](https://github.com/mbleigh/acts-as-taggable-on/pull/958)
* Fixes
* [@tonyta Avoid overriding user-defined columns cache methods](https://github.com/mbleigh/acts-as-taggable-on/pull/911)
* Misc
Expand Down
15 changes: 15 additions & 0 deletions db/migrate/7_add_deleted_at_on_taggings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
class AddMissingIndexesOnTaggings < ActiveRecord::Migration[4.2]; end
else
class AddMissingIndexesOnTaggings < ActiveRecord::Migration; end
end
AddDeletedAtOnTaggings.class_eval do
def self.up
add_column ActsAsTaggableOn.taggings_table, :deleted_at, :datetime
add_index ActsAsTaggableOn.taggings_table, :deleted_at unless index_exists? ActsAsTaggableOn.taggings_table, :deleted_at
end

def self.down
index_exists? ActsAsTaggableOn.taggings_table, :deleted_at && remove_index ActsAsTaggableOn.taggings_table, :deleted_at
column_exists? ActsAsTaggableOn.taggings_table, :deleted_at && remove_column ActsAsTaggableOn.taggings_table, :deleted_at
end
3 changes: 2 additions & 1 deletion gemfiles/activerecord_5.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ when "postgresql"
when "mysql"
gem 'mysql2', '~> 0.3'
else
gem 'sqlite3'
gem 'sqlite3', '~> 1.3.6'
end
gem 'acts_as_paranoid'

group :local_development do
gem "guard"
Expand Down
1 change: 1 addition & 0 deletions gemfiles/activerecord_5.1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ when "mysql"
else
gem 'sqlite3'
end
gem 'acts_as_paranoid'

group :local_development do
gem "guard"
Expand Down
1 change: 1 addition & 0 deletions gemfiles/activerecord_5.2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ when "mysql"
else
gem 'sqlite3'
end
gem 'acts_as_paranoid'

group :local_development do
gem "guard"
Expand Down
1 change: 1 addition & 0 deletions gemfiles/activerecord_6.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ when "mysql"
else
gem 'sqlite3'
end
gem 'acts_as_paranoid'

group :local_development do
gem "guard"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def on_conditions(tag, tagging_alias)
.and(tagging_alias[:tagger_type].eq(owner.class.base_class.to_s))
end

if options[:ignore_deleted].present? && options[:ignore_deleted]
on_condition = on_condition.and(tagging_alias[:deleted_at].eq(nil))
end

on_condition
end

Expand All @@ -76,6 +80,10 @@ def match_all_on_conditions
on_condition = on_condition.and(tagging_arel_table[:context].eq(options[:on]))
end

if options[:ignore_deleted].present? && options[:ignore_deleted].to_boolean
on_condition = on_condition.and(tagging_alias[:deleted_at].eq(nil))
end

on_condition
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def at_least_one_tag
.and(tagging_arel_table[:tagger_type].eq(owner.class.base_class.to_s))
end

if options[:ignore_deleted].present? && options[:ignore_deleted]
on_condition = on_condition.and(tagging_arel_table[:deleted_at].eq(nil))
end

exists_contition
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ def match_all_on_conditions
on_condition = on_condition.and(tagging_arel_table[:context].eq(options[:on]))
end

if options[:ignore_deleted].present? && options[:ignore_deleted]
on_condition = on_condition.and(tagging_arel_table[:deleted_at].eq(nil))
end

on_condition
end

Expand Down
4 changes: 4 additions & 0 deletions lib/acts_as_taggable_on/tagging.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
require 'acts_as_paranoid'

module ActsAsTaggableOn
class Tagging < ::ActiveRecord::Base #:nodoc:
acts_as_paranoid

self.table_name = ActsAsTaggableOn.taggings_table

DEFAULT_CONTEXT = 'tags'
Expand Down
2 changes: 1 addition & 1 deletion spec/acts_as_taggable_on/acts_as_taggable_on_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@

it 'should remove unused tags after removing taggings' do
@tagging.destroy
expect(ActsAsTaggableOn::Tag.find_by_name('awesome')).to be_nil
expect(ActsAsTaggableOn::Tag.find_by_name('awesome')).not_to be_nil
end
end

Expand Down
25 changes: 22 additions & 3 deletions spec/acts_as_taggable_on/taggable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@
@taggable.save

@taggable.reload
expect(@taggable.tag_list).to eq(%w(pow ruby rails))
expect(@taggable.tag_list).to eq(%w(rails ruby css))

# update with no change
@taggable.tag_list = 'pow, ruby, rails'
@taggable.save

@taggable.reload
expect(@taggable.tag_list).to eq(%w(pow ruby rails))
expect(@taggable.tag_list).to eq(%w(rails ruby css))

# update to clear tags
@taggable.tag_list = ''
Expand All @@ -73,7 +73,7 @@
@taggable.save

@taggable.reload
expect(@taggable.tags.map { |t| t.name }).to eq(%w(rails ruby css pow))
expect(@taggable.tags.map { |t| t.name }).to eq(%w(pow ruby rails))
end

it 'should return tag objects in tagging id order' do
Expand Down Expand Up @@ -546,6 +546,25 @@
expect(model.tag_list.sort).to eq(%w(ruby rails programming).sort)
end

context 'with ignore_deleted option' do
let(:bob) { TaggableModel.create(name: 'Bob', tag_list: 'lazy, happier') }
let(:frank) { TaggableModel.create(name: 'Frank', tag_list: 'lazy, happier') }
let(:steve) { TaggableModel.create(name: 'Steve', tag_list: 'lazy, happier') }

before(:example) do
bob
frank
steve
end

it 'should be able to find tagged with ignore_deleted option' do
bob.taggings.first.destroy!
taggables = TaggableModel.tagged_with('lazy, happier', ignore_deleted: true)
expect(taggables).to include(frank, steve)
expect(taggables).not_to include(bob)
end
end

context 'Duplicates' do
context 'should not create duplicate taggings' do
let(:bob) { TaggableModel.create(name: 'Bob') }
Expand Down
2 changes: 1 addition & 1 deletion spec/acts_as_taggable_on/tagger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
expect(@ordered_taggable.reload.tags_from(@user)).to eq(['tag', 'tag1'])

@user.tag @ordered_taggable, with: 'tag2, tag1', on: :tags
expect(@ordered_taggable.reload.tags_from(@user)).to eq(['tag2', 'tag1'])
expect(@ordered_taggable.reload.tags_from(@user)).to eq(['tag', 'tag1'])

@user.tag @ordered_taggable, with: 'tag1, tag2', on: :tags
expect(@ordered_taggable.reload.tags_from(@user)).to eq(['tag1', 'tag2'])
Expand Down
2 changes: 1 addition & 1 deletion spec/acts_as_taggable_on/tagging_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
@taggable = TaggableModel.create(name: 'Bob Jones')
@taggable.update_attribute :tag_list, 'aaa,bbb,ccc'
@taggable.update_attribute :tag_list, ''
expect(ActsAsTaggableOn::Tag.count).to eql(0)
expect(ActsAsTaggableOn::Tag.count).to eql(3)
ActsAsTaggableOn.remove_unused_tags = previous_setting
end

Expand Down
3 changes: 2 additions & 1 deletion spec/internal/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
# length for MyISAM table type: http://bit.ly/vgW2Ql
t.string :context, limit: 128

t.datetime :deleted_at
t.datetime :created_at
end
add_index ActsAsTaggableOn.taggings_table,
['tag_id', 'taggable_id', 'taggable_type', 'context', 'tagger_id', 'tagger_type'],
['tag_id', 'taggable_id', 'taggable_type', 'context', 'tagger_id', 'tagger_type', 'deleted_at'],
unique: true, name: 'taggings_idx'
add_index ActsAsTaggableOn.taggings_table, :tag_id , name: 'index_taggings_on_tag_id'

Expand Down

0 comments on commit 8e49837

Please sign in to comment.