module ActiveScaffold::Helpers::AssociationHelpers

Public Instance Methods

association_options_count(association, conditions = nil) click to toggle source
# File lib/active_scaffold/helpers/association_helpers.rb, line 14
def association_options_count(association, conditions = nil)
  relation = association.associated_class.dataset
  relation = relation.where(conditions) if conditions
  relation = relation.where(association[:conditions]) if association[:conditions]
  relation.count
end
association_options_find(association, conditions = nil) click to toggle source

Provides a way to honor the :conditions on an association while searching the association’s klass

# File lib/active_scaffold/helpers/association_helpers.rb, line 5
def association_options_find(association, conditions = nil)
  relation = association.associated_class.dataset
  relation = relation.where(conditions) if conditions
  relation = relation.where(association[:conditions]) if association[:conditions]
  relation = relation.eager(association[:eager]) if association[:eager]
  relation = relation.eager_graph(association[:eager_graph]) if association[:eager_graph]
  relation.all
end
options_for_association(association, include_all = false) click to toggle source

returns options for the given association as a collection of [id, label] pairs intended for the options_for_select helper.

# File lib/active_scaffold/helpers/association_helpers.rb, line 22
def options_for_association(association, include_all = false)
  available_records = association_options_find(association, include_all ? nil : options_for_association_conditions(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) click to toggle source

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.

# File lib/active_scaffold/helpers/association_helpers.rb, line 35
def options_for_association_conditions(association)
  return nil if association[:join_table]
  case association[:type]
    when :one_to_one, :one_to_many
      # Find only orphaned objects
      {association[:key] => nil}
    when :many_to_one, :many_to_many
      # Find all
      nil
  end
end
options_for_association_count(association) click to toggle source
# File lib/active_scaffold/helpers/association_helpers.rb, line 28
def options_for_association_count(association)
  association_options_count(association, options_for_association_conditions(association))
end