Sha256: 279067297b09e2edd91a9fc86878bd25bdbbbaca1ee24b43168a8887d1bbff98

Contents?: true

Size: 1.87 KB

Versions: 37

Compression:

Stored size: 1.87 KB

Contents

module ActiveScaffold
  module Helpers
    module AssociationHelpers
      # Provides a way to honor the :conditions on an association while searching the association's klass
      def association_options_find(association, conditions = nil)
        relation = association.klass.where(conditions).where(association.options[:conditions])
        relation = relation.includes(association.options[:include]) if association.options[:include]
        relation.all
      end

      def association_options_count(association, conditions = nil)
        association.klass.where(conditions).where(association.options[:conditions]).count
      end

      # returns options for the given association as a collection of [id, label] pairs intended for the +options_for_select+ helper.
      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

      def options_for_association_count(association)
        association_options_count(association, options_for_association_conditions(association))
      end

      # 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.
      def options_for_association_conditions(association)
        return nil if association.options[: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
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
active_scaffold-3.2.20 lib/active_scaffold/helpers/association_helpers.rb
active_scaffold-3.2.19 lib/active_scaffold/helpers/association_helpers.rb
active_scaffold-3.2.17 lib/active_scaffold/helpers/association_helpers.rb
active_scaffold-3.2.16 lib/active_scaffold/helpers/association_helpers.rb
active_scaffold-3.2.15 lib/active_scaffold/helpers/association_helpers.rb
active_scaffold-3.2.14 lib/active_scaffold/helpers/association_helpers.rb
active_scaffold-3.2.13 lib/active_scaffold/helpers/association_helpers.rb
active_scaffold-3.2.12 lib/active_scaffold/helpers/association_helpers.rb
active_scaffold-3.2.11 lib/active_scaffold/helpers/association_helpers.rb
active_scaffold-3.2.10 lib/active_scaffold/helpers/association_helpers.rb
active_scaffold-3.2.9 lib/active_scaffold/helpers/association_helpers.rb
active_scaffold-3.2.8 lib/active_scaffold/helpers/association_helpers.rb
active_scaffold-3.2.7 lib/active_scaffold/helpers/association_helpers.rb
active_scaffold-3.2.6 lib/active_scaffold/helpers/association_helpers.rb
active_scaffold-3.2.5 lib/active_scaffold/helpers/association_helpers.rb
active_scaffold-3.2.4 lib/active_scaffold/helpers/association_helpers.rb
active_scaffold-3.2.3 lib/active_scaffold/helpers/association_helpers.rb
active_scaffold-3.2.2 lib/active_scaffold/helpers/association_helpers.rb
active_scaffold-3.2.1 lib/active_scaffold/helpers/association_helpers.rb
active_scaffold-3.2.0 lib/active_scaffold/helpers/association_helpers.rb