Class: ActiveScaffold::DataStructures::NestedInfoAssociation

Inherits:
NestedInfo show all
Defined in:
lib/active_scaffold/data_structures/nested_info.rb

Instance Attribute Summary

Attributes inherited from NestedInfo

#association, #child_association, #constrained_fields, #param_name, #parent_id, #parent_model, #parent_scaffold, #scope

Instance Method Summary collapse

Methods inherited from NestedInfo

#belongs_to?, get, #habtm?, #has_many?, #has_one?, #new_instance?, #plural_association?, #readonly?, #singular_association?

Constructor Details

#initialize(model, params) ⇒ NestedInfoAssociation

Returns a new instance of NestedInfoAssociation.



72
73
74
75
76
77
78
79
80
# File 'lib/active_scaffold/data_structures/nested_info.rb', line 72

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.try(:association)
  @child_association = association.reverse_association(model) if association
  setup_constrained_fields
end

Instance Method Details

#default_sorting(chain) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/active_scaffold/data_structures/nested_info.rb', line 105

def default_sorting(chain)
  return @default_sorting if defined? @default_sorting
  if 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
end

#readonly_through_association?(columns) ⇒ Boolean

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

Returns:

  • (Boolean)


89
90
91
92
93
94
95
# File 'lib/active_scaffold/data_structures/nested_info.rb', line 89

def readonly_through_association?(columns)
  return false unless through_association?
  return true if association.through_reflection.options[:through]
  !association.source_reflection.belongs_to? && (
    !child_association || !columns.include?(child_association.through_reflection.name)
  )
end

#sorted?(chain) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/active_scaffold/data_structures/nested_info.rb', line 101

def sorted?(chain)
  default_sorting(chain).present?
end

#through_association?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/active_scaffold/data_structures/nested_info.rb', line 97

def through_association?
  association.through?
end

#to_paramsObject



114
115
116
# File 'lib/active_scaffold/data_structures/nested_info.rb', line 114

def to_params
  super.merge(:association => @association.name, :assoc_id => parent_id)
end