Class: ActiveScaffold::DataStructures::NestedInfoAssociation
- Inherits:
-
NestedInfo
- Object
- NestedInfo
- ActiveScaffold::DataStructures::NestedInfoAssociation
- 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
- #create_through_singular? ⇒ Boolean
- #create_with_parent? ⇒ Boolean
- #default_sorting(chain) ⇒ Object
-
#initialize(model, params) ⇒ NestedInfoAssociation
constructor
A new instance of NestedInfoAssociation.
- #match_model?(model) ⇒ Boolean
-
#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: 1.
- #sorted?(chain) ⇒ Boolean
- #through_association? ⇒ Boolean
- #to_params ⇒ Object
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.
82 83 84 85 86 87 88 89 90 |
# File 'lib/active_scaffold/data_structures/nested_info.rb', line 82 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 |
Instance Method Details
#create_through_singular? ⇒ Boolean
116 117 118 |
# File 'lib/active_scaffold/data_structures/nested_info.rb', line 116 def create_through_singular? association.through_singular? && association.source_reflection.reverse end |
#create_with_parent? ⇒ Boolean
120 121 122 123 124 125 126 |
# File 'lib/active_scaffold/data_structures/nested_info.rb', line 120 def create_with_parent? if has_many? && !association.through? false elsif child_association || create_through_singular? true end end |
#default_sorting(chain) ⇒ Object
144 145 146 147 148 149 150 151 |
# File 'lib/active_scaffold/data_structures/nested_info.rb', line 144 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) ⇒ Boolean
132 133 134 135 136 137 138 |
# File 'lib/active_scaffold/data_structures/nested_info.rb', line 132 def match_model?(model) if association.polymorphic? child_association&.inverse_klass == model else association.klass == model 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:
-
is through singular and source association has reverse, e.g.: Employee belongs to vendor, Vendor has many rates, Rate belongs to vendor, Employee has many rates through vendor Rates association through singular association vendor, source association in Vendor (rates) has reverse (vendor in Rate) AS will assign the vendor of the employee to the new Rate
-
source association is singular, e.g.: Customer has many networks, Network has one (or belongs to) firewall, Customer has many firewalls through networks
-
create columns include through association of reverse association, e.g.: Vendor has many employees, Employee has many rates, Vendor has many rates through employees, Rate has one vendor through employee RatesController has employee in create action columns (reverse is vendor, and through association employee is in create form).
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/active_scaffold/data_structures/nested_info.rb', line 105 def readonly_through_association?(columns) return false unless through_association? return true if association.through_reflection.through? # create not possible, too many levels return true if association.source_reflection.through? # create not possible, too many levels return false if create_through_singular? # create allowed, AS has code for this return false unless association.source_reflection.collection? # create allowed if source is singular, rails creates joint model # create allowed only if through reflection in record to be created is included in create columns !child_association || columns.exclude?(child_association.through_reflection.name) end |
#sorted?(chain) ⇒ Boolean
140 141 142 |
# File 'lib/active_scaffold/data_structures/nested_info.rb', line 140 def sorted?(chain) default_sorting(chain).present? end |
#through_association? ⇒ Boolean
128 129 130 |
# File 'lib/active_scaffold/data_structures/nested_info.rb', line 128 def through_association? association.through? end |
#to_params ⇒ Object
153 154 155 |
# File 'lib/active_scaffold/data_structures/nested_info.rb', line 153 def to_params super.merge(association: @association.name, @param_name => parent_id) end |