Module: ActiveScaffold::Helpers::AssociationHelpers
- Included in:
- ViewHelpers
- Defined in:
- lib/active_scaffold/helpers/association_helpers.rb
Instance Method Summary collapse
- #association_options_count(association, conditions = nil) ⇒ Object
-
#association_options_find(association, conditions = nil, klass = nil) ⇒ Object
Provides a way to honor the :conditions on an association while searching the association’s klass.
-
#cache_association_options(association, conditions, klass, cache = true) ⇒ Object
Cache the optins for select.
-
#options_for_association(association, include_all = false) ⇒ Object
returns options for the given association as a collection of [id, label] pairs intended for the
options_for_select
helper. -
#options_for_association_conditions(association) ⇒ Object
A useful override for customizing the records present in an association dropdown.
- #options_for_association_count(association) ⇒ Object
-
#sorted_association_options_find(association, conditions = nil) ⇒ Object
Sorts the options for select.
Instance Method Details
#association_options_count(association, conditions = nil) ⇒ Object
44 45 46 |
# File 'lib/active_scaffold/helpers/association_helpers.rb', line 44 def (association, conditions = nil) association.klass.where(conditions).where(association.[:conditions]).count end |
#association_options_find(association, conditions = nil, klass = nil) ⇒ Object
Provides a way to honor the :conditions on an association while searching the association’s klass
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/active_scaffold/helpers/association_helpers.rb', line 16 def (association, conditions = nil, klass = nil) if klass.nil? && association.[:polymorphic] class_name = @record.send(association.foreign_type) if class_name.present? klass = class_name.constantize else return [] end cache = !block_given? else cache = !block_given? && klass.nil? klass ||= association.klass end conditions = (association) if conditions.nil? (association, conditions, klass, cache) do relation = klass.where(conditions).where(association.[:conditions]) relation = relation.includes(association.[:include]) if association.[:include] relation = yield(relation) if block_given? relation.to_a end end |
#cache_association_options(association, conditions, klass, cache = true) ⇒ Object
Cache the optins for select
5 6 7 8 9 10 11 12 13 |
# File 'lib/active_scaffold/helpers/association_helpers.rb', line 5 def (association, conditions, klass, cache = true) if active_scaffold_config. && 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 |
#options_for_association(association, include_all = false) ⇒ Object
returns options for the given association as a collection of [id, label] pairs intended for the options_for_select
helper.
49 50 51 52 53 54 |
# File 'lib/active_scaffold/helpers/association_helpers.rb', line 49 def (association, include_all = false) ActiveSupport::Deprecation.warn "options_for_association should not be used, use association_options_find directly" available_records = (association, include_all ? nil : (association)) available_records ||= [] available_records.sort{|a,b| a.to_label <=> b.to_label}.collect { |model| [ model.to_label, model.id ] } end |
#options_for_association_conditions(association) ⇒ Object
A useful override for customizing the records present in an association dropdown. Should work in both the subform and form_ui=>:select modes. Check association.name to specialize the conditions per-column.
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/active_scaffold/helpers/association_helpers.rb', line 63 def (association) return nil if association.[:through] case association.macro when :has_one, :has_many # Find only orphaned objects "#{association.foreign_key} IS NULL" when :belongs_to, :has_and_belongs_to_many # Find all nil end end |
#options_for_association_count(association) ⇒ Object
56 57 58 |
# File 'lib/active_scaffold/helpers/association_helpers.rb', line 56 def (association) (association, (association)) end |
#sorted_association_options_find(association, conditions = nil) ⇒ Object
Sorts the options for select
40 41 42 |
# File 'lib/active_scaffold/helpers/association_helpers.rb', line 40 def (association, conditions = nil) (association, conditions).sort_by(&:to_label) end |