Skip to content

Commit

Permalink
Allow additional fields when attaching
Browse files Browse the repository at this point in the history
  • Loading branch information
binarygit committed Jul 24, 2024
1 parent cbc128f commit bc1d72f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
60 changes: 50 additions & 10 deletions app/controllers/avo/associations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,25 @@ def new
query = Avo::ExecutionContext.new(target: @field.attach_scope, query: query, parent: @record).handle
end

@extra = if @field.extra.present?
Avo::FieldsExecutionContext.new(target: @field.extra, parent: @record)
.detect_fields
.items_holder
.items
end

@options = query.all.map do |record|
[@attachment_resource.new(record: record).record_title, record.id]
end
end
end

def create
association_name = BaseResource.valid_association_name(@record, association_from_params)

if reflection_class == "HasManyReflection"
@record.send(association_name) << @attachment_record
else
@record.send(:"#{association_name}=", @attachment_record)
end

respond_to do |format|
if @record.save
if save_association
format.html { redirect_back fallback_location: resource_view_response_path, notice: t("avo.attachment_class_attached", attachment_class: @related_resource.name) }
else
format.html { render :new }
format.html { redirect_back fallback_location: resource_view_response_path, error: "Attachment unsuccessful" }
end
end
end
Expand Down Expand Up @@ -159,5 +158,46 @@ def set_related_authorization
def association_from_params
@field&.for_attribute || params[:related_name]
end

def save_association
association_name = BaseResource.valid_association_name(@record, association_from_params)

if reflection.instance_of?(ActiveRecord::Reflection::ThroughReflection) && additional_params?
join_record.save
elsif reflection_class == "HasManyReflection"
@record.send(association_name) << @attachment_record
else
@record.send(:"#{association_name}=", @attachment_record)
end
end

def additional_params?
additional_params.keys.count >= 1
end

def additional_params
params[:fields].except("related_id")
end

def reflection
@record.class.reflections.with_indifferent_access[association_from_params]
end

def source_foreign_key
reflection.source_reflection.foreign_key
end

def through_foreign_key
reflection.through_reflection.foreign_key
end

def join_record_params
field_names = Avo::FieldsExecutionContext.new(target: @field.extra, parent: @resource).detect_fields.items.map { |field| field.name.tr(" ", "_").downcase }
additional_params.permit(field_names).merge({source_foreign_key => @attachment_record.id, through_foreign_key => @record.id})
end

def join_record
reflection.through_reflection.klass.new(join_record_params)
end
end
end
3 changes: 3 additions & 0 deletions app/views/avo/associations/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
class: input_classes('w-full'),
}
%>
<% @extra&.each_with_index do |ex, index| %>
<%= render(Avo::Items::SwitcherComponent.new(resource: @related_resource, item: ex, index: index + 1, view: @view, form: form, field_component_extra_args: {stacked: true})) %>
<% end %>
</div>
<% end %>
</div>
Expand Down
2 changes: 2 additions & 0 deletions lib/avo/fields/has_base_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class HasBaseField < BaseField
attr_accessor :discreet_pagination
attr_accessor :hide_search_input
attr_reader :link_to_child_resource
attr_reader :extra

def initialize(id, **args, &block)
super(id, **args, &block)
Expand All @@ -27,6 +28,7 @@ def initialize(id, **args, &block)
@link_to_child_resource = args[:link_to_child_resource] || false
@reloadable = args[:reloadable].present? ? args[:reloadable] : false
@linkable = args[:linkable].present? ? args[:linkable] : false
@extra = args[:extra].present? ? args[:extra] : nil
end

def field_resource
Expand Down

0 comments on commit bc1d72f

Please sign in to comment.