class ActiveScaffold::DataStructures::NestedInfoAssociation

Public Class Methods

new(model, params) click to toggle source
# File lib/active_scaffold/data_structures/nested_info.rb, line 72
def initialize(model, params)
  super
  @association = parent_model.reflect_on_association(params[:association].to_sym)
  @param_name = @association.active_record.name.foreign_key.to_sym
  @parent_id = params[@param_name]
  iterate_model_associations(model)
end

Public Instance Methods

default_sorting() click to toggle source
# File lib/active_scaffold/data_structures/nested_info.rb, line 121
def default_sorting
  if association.options[:order] # TODO: remove when rails 3 compatibility is removed
    association.options[:order]
  elsif association.respond_to?(:scope) # rails 4
    association.klass.class_eval(&association.scope).values[:order] if association.scope.is_a? Proc
  end
end
habtm?() click to toggle source
# File lib/active_scaffold/data_structures/nested_info.rb, line 86
def habtm?
  association.macro == :has_and_belongs_to_many
end
has_many?() click to toggle source
# File lib/active_scaffold/data_structures/nested_info.rb, line 82
def has_many?
  association.macro == :has_many
end
has_one?() click to toggle source
# File lib/active_scaffold/data_structures/nested_info.rb, line 92
def has_one?
  association.macro == :has_one
end
readonly?() click to toggle source
# File lib/active_scaffold/data_structures/nested_info.rb, line 113
def readonly?
  association.options[:readonly]
end
readonly_through_association?(columns) click to toggle source

A through association with has_one or has_many as source association create cannot be called in nested through associations, and not-nested through associations unless create columns include through reflection of reverse association e.g. customer -> networks -> firewall, reverse is firewall -> network -> customer, firewall can be created if create columns include network

# File lib/active_scaffold/data_structures/nested_info.rb, line 101
def readonly_through_association?(columns)
  return false unless through_association?
  return true if association.through_reflection.options[:through]
  association.source_reflection.macro != :belongs_to && (
    !child_association || !columns.include?(child_association.through_reflection.name)
  )
end
sorted?() click to toggle source
# File lib/active_scaffold/data_structures/nested_info.rb, line 117
def sorted?
  association.options.key? :order
end
through_association?() click to toggle source
# File lib/active_scaffold/data_structures/nested_info.rb, line 109
def through_association?
  association.options[:through]
end
to_params() click to toggle source
# File lib/active_scaffold/data_structures/nested_info.rb, line 129
def to_params
  super.merge(:association => @association.name, :assoc_id => parent_id)
end

Protected Instance Methods

iterate_model_associations(model) click to toggle source
# File lib/active_scaffold/data_structures/nested_info.rb, line 135
def iterate_model_associations(model)
  @constrained_fields = []
  constrained_fields << Array(association.foreign_key).map(&:to_sym) unless association.belongs_to?
  return if (reverse = association.reverse(model)).nil?
  @child_association = model.reflect_on_association(reverse)
  constrained_fields << @child_association.name unless @child_association == association
end