From 73d5f9083516e6e56e38ba193385e4e40b1fdbe6 Mon Sep 17 00:00:00 2001 From: ProGM Date: Mon, 20 Feb 2017 11:54:32 +0100 Subject: [PATCH 1/3] Adding a test to demonstrate the bug --- spec/acts_as_taggable_on/caching_spec.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/spec/acts_as_taggable_on/caching_spec.rb b/spec/acts_as_taggable_on/caching_spec.rb index 18acae984..39565ccfb 100644 --- a/spec/acts_as_taggable_on/caching_spec.rb +++ b/spec/acts_as_taggable_on/caching_spec.rb @@ -99,6 +99,29 @@ end end + describe 'Cache methods initialization on new models' do + before(:all) do + ActiveRecord::Schema.define do + create_table :cache_methods_injected_models do |t| + t.string :cached_tag_list + end + end + ActiveRecord::Base.connection.execute( + 'INSERT INTO cache_methods_injected_models VALUES (NULL, \'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 From 4757ae5d4c7350a47dffa88e024369801530014f Mon Sep 17 00:00:00 2001 From: ProGM Date: Mon, 20 Feb 2017 11:58:39 +0100 Subject: [PATCH 2/3] Ensure that `caching_tag_list_on?` is injected before using it --- lib/acts_as_taggable_on/taggable/core.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/acts_as_taggable_on/taggable/core.rb b/lib/acts_as_taggable_on/taggable/core.rb index 645a563c3..83d4a198b 100644 --- a/lib/acts_as_taggable_on/taggable/core.rb +++ b/lib/acts_as_taggable_on/taggable/core.rb @@ -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))) @@ -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"} From 2011c16549c0165e81beb774bf1050ff8769af8a Mon Sep 17 00:00:00 2001 From: ProGM Date: Mon, 20 Feb 2017 15:05:19 +0100 Subject: [PATCH 3/3] Fix insert query for postgresql. Move schema definition in schema.rb --- spec/acts_as_taggable_on/caching_spec.rb | 7 +------ spec/internal/db/schema.rb | 3 +++ 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/spec/acts_as_taggable_on/caching_spec.rb b/spec/acts_as_taggable_on/caching_spec.rb index 39565ccfb..a9f762c4a 100644 --- a/spec/acts_as_taggable_on/caching_spec.rb +++ b/spec/acts_as_taggable_on/caching_spec.rb @@ -101,13 +101,8 @@ describe 'Cache methods initialization on new models' do before(:all) do - ActiveRecord::Schema.define do - create_table :cache_methods_injected_models do |t| - t.string :cached_tag_list - end - end ActiveRecord::Base.connection.execute( - 'INSERT INTO cache_methods_injected_models VALUES (NULL, \'ciao\')' + 'INSERT INTO cache_methods_injected_models (cached_tag_list) VALUES (\'ciao\')' ) class CacheMethodsInjectedModel < ActiveRecord::Base acts_as_taggable diff --git a/spec/internal/db/schema.rb b/spec/internal/db/schema.rb index d72f881cb..af9ba2ab4 100644 --- a/spec/internal/db/schema.rb +++ b/spec/internal/db/schema.rb @@ -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?