Skip to content

Commit

Permalink
Merge pull request #806 from ProGM/fix-caching-tag-list-on-bug
Browse files Browse the repository at this point in the history
Fix `caching_tag_list_on?` bug (#432)
  • Loading branch information
seuros committed May 3, 2017
2 parents 9bb5738 + 2011c16 commit 0590d4b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/acts_as_taggable_on/taggable/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def tag_list_cache_on(context)
variable_name = "@#{context.to_s.singularize}_list"
if instance_variable_get(variable_name)
instance_variable_get(variable_name)
elsif cached_tag_list_on(context) && self.class.caching_tag_list_on?(context)
elsif cached_tag_list_on(context) && ensure_included_cache_methods! && self.class.caching_tag_list_on?(context)
instance_variable_set(variable_name, ActsAsTaggableOn.default_parser.new(cached_tag_list_on(context)).parse)
else
instance_variable_set(variable_name, ActsAsTaggableOn::TagList.new(tags_on(context).map(&:name)))
Expand Down Expand Up @@ -418,6 +418,10 @@ def save_tags

private

def ensure_included_cache_methods!
self.class.columns
end

# Filters the tag lists from the attribute names.
def attributes_for_update(attribute_names)
tag_lists = tag_types.map {|tags_type| "#{tags_type.to_s.singularize}_list"}
Expand Down
18 changes: 18 additions & 0 deletions spec/acts_as_taggable_on/caching_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,24 @@
end
end

describe 'Cache methods initialization on new models' do
before(:all) do
ActiveRecord::Base.connection.execute(
'INSERT INTO cache_methods_injected_models (cached_tag_list) VALUES (\'ciao\')'
)
class CacheMethodsInjectedModel < ActiveRecord::Base
acts_as_taggable
end
end
after(:all) { Object.send(:remove_const, :CacheMethodsInjectedModel) }

it 'cached_tag_list_on? get injected correctly' do
expect do
CacheMethodsInjectedModel.first.tag_list
end.not_to raise_error
end
end

describe 'CachingWithArray' do
pending '#TODO'
end
Expand Down
3 changes: 3 additions & 0 deletions spec/internal/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@
t.column :type, :string
end

create_table :cache_methods_injected_models, force: true do |t|
t.column :cached_tag_list, :string
end

# Special cases for postgresql
if using_postgresql?
Expand Down

0 comments on commit 0590d4b

Please sign in to comment.