class ActiveScaffold::DataStructures::NestedInfoAssociation

Public Class Methods

new(model, params) click to toggle source
# File lib/active_scaffold/data_structures/nested_info.rb, line 80
def initialize(model, params)
  super
  column = parent_scaffold.active_scaffold_config.columns[params[:association].to_sym]
  @param_name = column.model.name.foreign_key.to_sym
  @parent_id = params[@param_name]
  @association = column&.association
  @child_association = association.reverse_association(model) if association
  setup_constrained_fields
end

Public Instance Methods

create_through_singular?() click to toggle source
# File lib/active_scaffold/data_structures/nested_info.rb, line 107
def create_through_singular?
  association.through_singular? && source_reflection.reverse
end
create_with_parent?() click to toggle source
# File lib/active_scaffold/data_structures/nested_info.rb, line 111
def create_with_parent?
  if has_many? && !association.through?
    false
  elsif child_association || create_through_singular?
    true
  end
end
default_sorting(chain) click to toggle source
# File lib/active_scaffold/data_structures/nested_info.rb, line 139
def default_sorting(chain)
  return @default_sorting if defined? @default_sorting
  return unless association.scope.is_a?(Proc) && chain.respond_to?(:values) && chain.values[:order]
  @default_sorting = chain.values[:order]
  @default_sorting = @default_sorting.map(&:to_sql) if @default_sorting[0].is_a? Arel::Nodes::Node
  @default_sorting = @default_sorting.join(', ')
end
match_model?(model) click to toggle source
# File lib/active_scaffold/data_structures/nested_info.rb, line 127
def match_model?(model)
  if association.polymorphic?
    child_association&.inverse_klass == model
  else
    association.klass == model
  end
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 is through singular or 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 97
def readonly_through_association?(columns)
  return false unless through_association?
  return true if association.through_reflection.options[:through] # create not possible, too many levels
  return true if association.source_reflection.options[:through] # create not possible, too many levels
  return false if create_through_singular? # create allowed, AS has code for this

  # create allowed only if through reflection in record to be created is included in create columns
  !child_association || !columns.include?(child_association.through_reflection.name)
end
sorted?(chain) click to toggle source
# File lib/active_scaffold/data_structures/nested_info.rb, line 135
def sorted?(chain)
  default_sorting(chain).present?
end
source_reflection() click to toggle source
# File lib/active_scaffold/data_structures/nested_info.rb, line 119
def source_reflection
  @source_reflection ||= ActiveScaffold::DataStructures::Association::ActiveRecord.new(association.source_reflection)
end
through_association?() click to toggle source
# File lib/active_scaffold/data_structures/nested_info.rb, line 123
def through_association?
  association.through?
end
to_params() click to toggle source
# File lib/active_scaffold/data_structures/nested_info.rb, line 147
def to_params
  super.merge(:association => @association.name, @param_name => parent_id)
end

Protected Instance Methods

setup_constrained_fields() click to toggle source
# File lib/active_scaffold/data_structures/nested_info.rb, line 153
def setup_constrained_fields
  @constrained_fields = [] if association.belongs_to? || association.through?
  @constrained_fields ||= Array(association.foreign_key).map(&:to_sym)
  return unless child_association && child_association != association

  @constrained_fields << child_association.name
  @constrained_fields << child_association.foreign_type.to_sym if child_association.polymorphic?
end