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

get, #new_instance?, #plural_association?, #singular_association?

Constructor Details

#initialize(model, params) ⇒ NestedInfoAssociation

Returns a new instance of NestedInfoAssociation.



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

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

Instance Method Details

#belongs_to?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/active_scaffold/data_structures/nested_info.rb', line 95

def belongs_to?
  association.belongs_to?
end

#default_sortingObject



125
126
127
128
129
130
131
# File 'lib/active_scaffold/data_structures/nested_info.rb', line 125

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?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/active_scaffold/data_structures/nested_info.rb', line 91

def habtm?
  association.macro == :has_and_belongs_to_many 
end

#has_many?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/active_scaffold/data_structures/nested_info.rb', line 87

def has_many?
  association.macro == :has_many 
end

#has_one?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/active_scaffold/data_structures/nested_info.rb', line 99

def has_one?
  association.macro == :has_one
end

#nameObject



83
84
85
# File 'lib/active_scaffold/data_structures/nested_info.rb', line 83

def name
  self.association.name
end

#readonly?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/active_scaffold/data_structures/nested_info.rb', line 117

def readonly?
  association.options[:readonly]
end

#readonly_through_association?(columns) ⇒ Boolean

A through association with has_one or has_many as source association create cannot be called in such through association, 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)


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

def readonly_through_association?(columns)
  through_association? && association.source_reflection.macro != :belongs_to && (
    !child_association || !columns.include?(child_association.through_reflection.name)
  )
end

#sorted?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/active_scaffold/data_structures/nested_info.rb', line 121

def sorted?
  association.options.has_key? :order
end

#through_association?Boolean

Returns:

  • (Boolean)


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

def through_association?
  association.options[:through]
end

#to_paramsObject



133
134
135
# File 'lib/active_scaffold/data_structures/nested_info.rb', line 133

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