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
Source
# File lib/active_scaffold/actions/nested.rb, line 6 def self.included(base) super base.module_eval do before_action :set_nested before_action :configure_nested include ActiveScaffold::Actions::Nested::ChildMethods if active_scaffold_config.columns.filter_map(&:association).any?(&:habtm?) end base.helper_method :nested base.helper_method :nested_parent_record end
Calls superclass method
Protected Instance Methods
Source
# File lib/active_scaffold/actions/nested.rb, line 64 def beginning_of_chain # only if nested is related to current controller, e.g. not when adding record in subform inside subform if nested? && nested.match_model?(active_scaffold_config.model) nested_chain_with_association elsif nested? && nested.scope nested_parent_record.send(nested.scope) else active_scaffold_config.model end end
Source
# File lib/active_scaffold/actions/nested.rb, line 35 def configure_nested return unless nested? register_constraints_with_action_columns(nested.constrained_fields) return unless active_scaffold_config.actions.include? :list active_scaffold_config.list.user.label = nested_label return if active_scaffold_config.nested.ignore_order_from_association chain = beginning_of_chain active_scaffold_config.list.user.nested_default_sorting = nested_default_sorting(chain) if nested.sorted?(chain) end
Source
# File lib/active_scaffold/actions/nested.rb, line 102 def create_association_with_parent(record, check_match: false) return unless create_association_with_parent?(check_match: check_match) if nested.child_association&.singular? record.send(:"#{nested.child_association.name}=", nested_parent_record) elsif nested.create_through_singular? through = nested_parent_record.send(nested.association.through_reflection.name) || nested_parent_record.send(:"build_#{nested.association.through_reflection.name}") if nested.source_reflection.reverse_association.collection? record.send(nested.source_reflection.reverse) << through else record.send(:"#{nested.source_reflection.reverse}=", through) end else record.send(nested.child_association.name) << nested_parent_record end end
Source
# File lib/active_scaffold/actions/nested.rb, line 94 def create_association_with_parent?(check_match: false) # has_many is done by beginning_of_chain and rails if direct association, not in through associations return false unless nested.create_with_parent? return false if check_match && !nested.match_model?(active_scaffold_config.model) nested_parent_record.present? end
Source
# File lib/active_scaffold/actions/nested.rb, line 19 def nested set_nested unless defined? @nested @nested end
Source
# File lib/active_scaffold/actions/nested.rb, line 24 def nested? !nested.nil? end
Source
# File lib/active_scaffold/actions/nested.rb, line 75 def nested_chain_with_association if nested.association.collection? nested_parent_record.send(nested.association.name) elsif nested.association.through? # has_one :through active_scaffold_config.model.where(active_scaffold_config.model.primary_key => nested_parent_record.send(nested.association.name)&.id) elsif nested.association.has_one? active_scaffold_config.model.where(nested.child_association.name => nested_parent_record) elsif nested.association.belongs_to? primary_key = active_scaffold_config.mongoid? ? '_id' : active_scaffold_config.model.primary_key active_scaffold_config.model.where(primary_key => nested_parent_record.send(nested.association.name)) else # never should get here raise 'missing condition for nested beginning_of_chain' end end
Source
# File lib/active_scaffold/actions/nested.rb, line 56 def nested_default_sorting(chain) {table_name: active_scaffold_config._table_name, default_sorting: nested.default_sorting(chain)} end
Source
# File lib/active_scaffold/actions/nested.rb, line 48 def nested_label if nested.belongs_to? as_(:nested_of_model, nested_model: active_scaffold_config.model.model_name.human, parent_model: ERB::Util.h(nested_parent_record.to_label)) else as_(:nested_for_model, nested_model: active_scaffold_config.list.label, parent_model: ERB::Util.h(nested_parent_record.to_label)) end end
Source
# File lib/active_scaffold/actions/nested.rb, line 90 def nested_parent_record(crud = :read) @nested_parent_record ||= find_if_allowed(nested.parent_id, crud, nested.parent_model) end
Source
# File lib/active_scaffold/actions/nested.rb, line 28 def set_nested @nested = nil return unless params[:parent_scaffold] && (params[:association] || params[:named_scope]) @nested = ActiveScaffold::DataStructures::NestedInfo.get(self.class.active_scaffold_config.model, params) end
Private Instance Methods
Source
# File lib/active_scaffold/actions/nested.rb, line 122 def nested_formats (default_formats + active_scaffold_config.formats + active_scaffold_config.nested.formats).uniq end