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

Move cache table existence check in method #433

Closed
wants to merge 1 commit into from
Closed
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
29 changes: 13 additions & 16 deletions lib/acts_as_taggable_on/acts_as_taggable_on/cache.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
module ActsAsTaggableOn::Taggable
module Cache
def self.included(base)
# Skip adding caching capabilities if table not exists or no cache columns exist
return unless base.connected? && base.table_exists? && base.tag_types.any? { |context| base.column_names.include?("cached_#{context.to_s.singularize}_list") }

base.send :include, ActsAsTaggableOn::Taggable::Cache::InstanceMethods
base.extend ActsAsTaggableOn::Taggable::Cache::ClassMethods

base.class_eval do
before_save :save_cached_tag_list
before_save :save_cached_tag_list
end

base.initialize_acts_as_taggable_on_cache
end

module ClassMethods
def initialize_acts_as_taggable_on_cache
def initialize_acts_as_taggable_on_cache
tag_types.map(&:to_s).each do |tag_type|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def self.caching_#{tag_type.singularize}_list?
caching_tag_list_on?("#{tag_type}")
end
end
RUBY
end
end
end

def acts_as_taggable_on(*args)
super(*args)
initialize_acts_as_taggable_on_cache
end

def caching_tag_list_on?(context)
column_names.include?("cached_#{context.to_s.singularize}_list")
table_exists? && column_names.include?("cached_#{context.to_s.singularize}_list")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this cause extra queries to fire?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's cached :)

# File activerecord/lib/active_record/model_schema.rb, line 202
def table_exists?
  connection.schema_cache.table_exists?(table_name)
end

end
end
module InstanceMethods

module InstanceMethods
def save_cached_tag_list
tag_types.map(&:to_s).each do |tag_type|
if self.class.send("caching_#{tag_type.singularize}_list?")
Expand All @@ -45,7 +42,7 @@ def save_cached_tag_list
end
end
end

true
end
end
Expand Down