Module: ActiveScaffold::Helpers::AssociationHelpers

Included in:
ViewHelpers
Defined in:
lib/active_scaffold/helpers/association_helpers.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) association_klass_scoped(association, klass, record)



61
62
63
# File 'lib/active_scaffold/helpers/association_helpers.rb', line 61

def association_klass_scoped(association, klass, record)
  klass
end

- (Object) association_options_count(association, conditions = nil)



76
77
78
# File 'lib/active_scaffold/helpers/association_helpers.rb', line 76

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

- (Object) association_options_find(association, conditions = nil, klass = nil, record = nil)

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/active_scaffold/helpers/association_helpers.rb', line 16

def association_options_find(association, conditions = nil, klass = nil, record = nil)
  ActiveSupport::Deprecation.warn "Relying on @record is deprecated, call with record.", caller if record.nil? # TODO Remove when relying on @record is removed
  record ||= @record # TODO Remove when relying on @record is removed
  if klass.nil? && association.options[: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

  if conditions.nil?
    if method(:options_for_association_conditions).arity.abs == 2
      conditions = options_for_association_conditions(association, record)
    else
      ActiveSupport::Deprecation.warn "Relying on @record is deprecated, include record in your options_for_association_conditions overrided method.", caller if record.nil? # TODO Remove when relying on @record is removed
      conditions = options_for_association_conditions(association)
    end
  end
  cache_association_options(association, conditions, klass, cache) do
    klass = association_klass_scoped(association, klass, record)
    relation = klass.where(conditions).where(association.options[:conditions])
    relation = relation.includes(association.options[:include]) if association.options[:include]
    column = column_for_association(association, record)
    if column && column.try(:sort) && column.sort[:sql]
      if column.includes
        include_assoc = column.includes.find { |assoc| assoc.is_a?(Hash) && assoc.include?(association.name) }
        relation = relation.includes(include_assoc[association.name]) if include_assoc
      end
      relation = relation.order(column.sort[:sql])
    end
    relation = yield(relation) if block_given?
    relation.to_a
  end
end

- (Object) cache_association_options(association, conditions, klass, cache = true)

Cache the options for select



5
6
7
8
9
10
11
12
13
# File 'lib/active_scaffold/helpers/association_helpers.rb', line 5

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

- (Object) column_for_association(association, record)



57
58
59
# File 'lib/active_scaffold/helpers/association_helpers.rb', line 57

def column_for_association(association, record)
  active_scaffold_config_for(record.class).columns[association.name] rescue nil
end

- (Object) options_for_association_conditions(association, record = nil)

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.



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/active_scaffold/helpers/association_helpers.rb', line 93

def options_for_association_conditions(association, record = nil)
  return nil if association.options[:through]
  case association.macro
    when :has_one, :has_many
      # Find only orphaned objects
      {association.foreign_key => nil}
    when :belongs_to, :has_and_belongs_to_many
      # Find all
      nil
  end
end

- (Object) options_for_association_count(association, record)



80
81
82
83
84
85
86
87
88
# File 'lib/active_scaffold/helpers/association_helpers.rb', line 80

def options_for_association_count(association, record)
  if method(:options_for_association_conditions).arity.abs == 2
    conditions = options_for_association_conditions(association, record)
  else
    ActiveSupport::Deprecation.warn "Relying on @record is deprecated, include record in your options_for_association_conditions overrided method.", caller if record.nil? # TODO Remove when relying on @record is removed
    conditions = options_for_association_conditions(association)
  end
  association_options_count(association, conditions)
end

- (Object) sorted_association_options_find(association, conditions = nil, record = nil)

Sorts the options for select



66
67
68
69
70
71
72
73
74
# File 'lib/active_scaffold/helpers/association_helpers.rb', line 66

def sorted_association_options_find(association, conditions = nil, record = nil)
  options = association_options_find(association, conditions, nil, record)
  column = column_for_association(association, record)
  unless column && column.try(:sort) && column.sort[:sql]
    method = column.options[:label_method] if column
    options = options.sort_by(&(method || :to_label).to_sym)
  end
  options
end