Skip to content

Commit

Permalink
cache association options
Browse files Browse the repository at this point in the history
  • Loading branch information
scambra committed Jun 6, 2013
1 parent edca966 commit f9c495d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Fix update calculations after destroy a row
- Fix count and calculate searching with count_includes
- Fix action_after_create
- Cache associations options (for :select form_ui on associations), it can be disabled with config.cache_association_options = false

= 3.3.0
- Unify field overrides and list_ui method signatures
Expand Down
8 changes: 8 additions & 0 deletions lib/active_scaffold/config/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def self.actions=(val)
cattr_accessor :cache_action_link_urls
@@cache_action_link_urls = true

# enable caching of association options
cattr_accessor :cache_association_options
@@cache_association_options = true

# lets you disable the DHTML history
def self.dhtml_history=(val)
@@dhtml_history = val
Expand Down Expand Up @@ -98,6 +102,9 @@ def columns=(val)
# enable caching of action link urls
attr_accessor :cache_action_link_urls

# enable caching of association options
attr_accessor :cache_association_options

# lets you specify whether add a create link for each sti child for a specific controller
attr_accessor :sti_create_links
def add_sti_create_links?
Expand Down Expand Up @@ -147,6 +154,7 @@ def initialize(model_id)
@frontend = self.class.frontend
@theme = self.class.theme
@cache_action_link_urls = self.class.cache_action_link_urls
@cache_association_options = self.class.cache_association_options
@sti_create_links = self.class.sti_create_links

# inherit from the global set of action links
Expand Down
25 changes: 20 additions & 5 deletions lib/active_scaffold/helpers/association_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
module ActiveScaffold
module Helpers
module AssociationHelpers
# Cache the optins for select
def cache_association_options(association, conditions, klass, cache = true)
if active_scaffold_config.cache_association_options && cache
@_associations_cache ||= Hash.new { |h,k| h[k] = {} }
key = [association.name, association.active_record.name, klass.name].join('/')
@_associations_cache[key][conditions] ||= yield
else
yield
end
end

# Provides a way to honor the :conditions on an association while searching the association's klass
def association_options_find(association, conditions = nil, klass = nil)
if klass.nil? && association.options[:polymorphic]
Expand All @@ -10,18 +21,22 @@ def association_options_find(association, conditions = nil, klass = nil)
else
return []
end
cache = !block_given?
else
cache = !block_given? && klass.nil?
klass ||= association.klass
end

conditions = options_for_association_conditions(association) if conditions.nil?
relation = klass.where(conditions).where(association.options[:conditions])
relation = relation.includes(association.options[:include]) if association.options[:include]
relation = yield(relation) if block_given?
relation.all
cache_association_options(association, conditions, klass, cache) do
relation = klass.where(conditions).where(association.options[:conditions])
relation = relation.includes(association.options[:include]) if association.options[:include]
relation = yield(relation) if block_given?
relation.to_a
end
end

# Provides a way to honor the :conditions on an association while searching the association's klass
# Sorts the options for select
def sorted_association_options_find(association, conditions = nil)
association_options_find(association, conditions).sort_by(&:to_label)
end
Expand Down

0 comments on commit f9c495d

Please sign in to comment.