module ActiveScaffold::Actions::Nested

The Nested module basically handles automatically linking controllers together. It does this by creating column links with the right parameters, and by providing any supporting systems (like a /:controller/nested action for returning associated scaffolds).

Public Class Methods

included(base) click to toggle source
Calls superclass method
# File lib/active_scaffold/actions/nested.rb, line 5
def self.included(base)
  super
  base.module_eval do
    prepend_before_filter :set_nested
    before_filter :configure_nested
    include ActiveScaffold::Actions::Nested::ChildMethods if active_scaffold_config.model.association_reflections.any? {|name,properties| properties[:type] == :many_to_many}
  end
  base.before_filter :include_habtm_actions
  base.helper_method :nested
  base.helper_method :nested_parent_record
end

Protected Instance Methods

beginning_of_chain() click to toggle source
Calls superclass method
# File lib/active_scaffold/actions/nested.rb, line 78
def beginning_of_chain
  if nested? && nested.association && !(nested.association[:type] == :many_to_one)
    if nested.association.returns_array?
      nested.parent_scope.send("#{nested.association[:name]}_dataset")
    elsif nested.child_association[:type] == :many_to_one
      active_scaffold_config.model.qualify.where((nested.child_association[:key] || nested.child_association[:left_key]) => nested.parent_id)
    end
  else
    # specified in actions/core.rb
    super
  end
end
configure_nested() click to toggle source
# File lib/active_scaffold/actions/nested.rb, line 36
def configure_nested
  if nested?
    active_scaffold_session_storage[:list][:label] =  if nested.belongs_to?
      as_(:nested_of_model, :nested_model => active_scaffold_config.model.model_name.human, :parent_model => nested_parent_record.to_label)
    else
      as_(:nested_for_model, :nested_model => active_scaffold_config.list.label, :parent_model => nested_parent_record.to_label)
    end
    if nested.sorted?
      active_scaffold_config.list.user.nested_default_sorting = {:table_name => active_scaffold_config.model.model_name, :default_sorting => nested.default_sorting}
    end
  end
end
create_association_with_parent(record) click to toggle source
# File lib/active_scaffold/actions/nested.rb, line 111
def create_association_with_parent(record)
  if nested?
    if (nested.belongs_to? || nested.has_one? || nested.habtm?) && nested.child_association
      parent = nested_parent_record(:read)
      case nested.child_association[:type]
      when :one_to_one
        record.send("#{nested.child_association[:name]}=", parent)
      when :many_to_one
        record.send("#{nested.child_association[:name]}=", parent)
      when :one_to_many, :many_to_many
        record.send("add_#{nested.child_association[:name].to_s.singularize}", parent)
      end unless parent.nil?
    end
  end
end
include_habtm_actions() click to toggle source
# File lib/active_scaffold/actions/nested.rb, line 53
def include_habtm_actions
  if nested?
    if nested.habtm?
      # Production mode is ok with adding a link everytime the scaffold is nested - we ar not ok with that.
      active_scaffold_config.action_links.add('new_existing', :label => :add_existing, :type => :collection, :security_method => :add_existing_authorized?) unless active_scaffold_config.action_links['new_existing']
      if active_scaffold_config.nested.shallow_delete
        active_scaffold_config.action_links.add('destroy_existing', :label => :remove, :type => :member, :confirm => :are_you_sure_to_delete, :method => :delete, :position => false, :security_method => :delete_existing_authorized?) unless active_scaffold_config.action_links['destroy_existing']
        if active_scaffold_config.actions.include?(:delete)
          active_scaffold_config.action_links.delete("delete") if active_scaffold_config.action_links['delete']
        end
      end
    else
      # Production mode is caching this link into a non nested scaffold
      active_scaffold_config.action_links.delete('new_existing') if active_scaffold_config.action_links['new_existing']
      
      if active_scaffold_config.nested.shallow_delete
        active_scaffold_config.action_links.delete("destroy_existing") if active_scaffold_config.action_links['destroy_existing']
        if active_scaffold_config.actions.include?(:delete)
          active_scaffold_config.action_links.add(ActiveScaffold::Config::Delete.link) unless active_scaffold_config.action_links['delete']
        end
      end
    end
  end
end
nested() click to toggle source
# File lib/active_scaffold/actions/nested.rb, line 18
def nested
  @nested
end
nested?() click to toggle source
# File lib/active_scaffold/actions/nested.rb, line 22
def nested?
  !nested.nil?
end
nested_authorized?(record = nil) click to toggle source
# File lib/active_scaffold/actions/nested.rb, line 49
def nested_authorized?(record = nil)
  true
end
nested_parent_record(crud = :read) click to toggle source
# File lib/active_scaffold/actions/nested.rb, line 107
def nested_parent_record(crud = :read)
  @nested_parent_record ||= find_if_allowed(nested.parent_id, crud, nested.parent_model)
end
origin_class() click to toggle source
# File lib/active_scaffold/actions/nested.rb, line 91
def origin_class
  origin_class_with_build_options[0]
end
origin_class_with_build_options() click to toggle source
# File lib/active_scaffold/actions/nested.rb, line 95
def origin_class_with_build_options
  if nested? && nested.association && !(nested.association[:type] == :many_to_one)
    if nested.association.returns_array?
      [nested.association.associated_class, {nested.association.reciprocal => nested.parent_scope}]
    elsif nested.child_association[:type] == :many_to_one
      [active_scaffold_config.model, {(nested.child_association[:key] || nested.child_association[:left_key]) => nested.parent_id}]
    end
  else
    [active_scaffold_config.model, {}]
  end
end
set_nested() click to toggle source
# File lib/active_scaffold/actions/nested.rb, line 26
def set_nested
  if params[:parent_scaffold] && params[:association]
    @nested = ActiveScaffold::DataStructures::NestedInfo.get(active_scaffold_config.model, params)
    unless @nested.nil?
      active_scaffold_constraints[:id] = params[:id] if @nested.belongs_to?
      register_constraints_with_action_columns(@nested.constrained_fields,  active_scaffold_config.list.hide_nested_column ? [] : [:list])
    end
  end
end

Private Instance Methods

nested_formats() click to toggle source
# File lib/active_scaffold/actions/nested.rb, line 128
def nested_formats
  (default_formats + active_scaffold_config.formats + active_scaffold_config.nested.formats).uniq
end