Skip to content

Commit

Permalink
fix adding new rows to the subform on existing tabs, fix assigning ta…
Browse files Browse the repository at this point in the history
…bbed_record when adding new rows and tabbed_by association
  • Loading branch information
scambra committed Oct 22, 2024
1 parent 4fff0c6 commit 9f26b9d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

# render footer before rendering associated records, fixes create new on self-associations
# so generated_id for blank associated record is not used in create new button
footer = render('form_association_footer', parent_record: parent_record, column: column, associated: associated, scope: scope, subform_div_id: subform_div_id)
footer = render('form_association_footer', parent_record: parent_record, column: column, associated: associated, scope: scope, subform_div_id: subform_div_id, tabbed_by: tabbed_by, tab_value: tab_value)
-%>
<h5>
<%= column.label -%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ controller_path = active_scaffold_controller_for(parent_record.class).controller
parent_controller = (main_form_controller || controller).controller_path
url_options = {:controller => controller_path, :action => 'edit_associated', :child_association => column.name, :scope => scope, :id => parent_record.to_param, :generated_id => temporary_id, :parent_controller => parent_controller}
edit_associated_url = params_for(url_options.merge(:associated_id => '--ID--')) if show_add_existing
add_new_url = params_for(url_options) if show_add_new
if show_add_new
if local_assigns[:tabbed_by]
url_options.merge! tabbed_by: tabbed_by, value: tab_value
if (tabbed_association = tabbed_by_association(column, tabbed_by))
url_options[:value] = tab_value.id
url_options[:value_type] = tab_value.class.base_class.sti_name if tabbed_association.polymorphic?
end
end
add_new_url = params_for(url_options)
end

-%>
<div class="footer-wrapper">
Expand Down
11 changes: 10 additions & 1 deletion lib/active_scaffold/actions/subform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,19 @@ def do_edit_associated

@record = find_associated_record if params[:associated_id]
@record ||= build_associated(@column.association, @parent_record) do |blank_record|
blank_record.send("#{params[:tabbed_by]}=", params[:value]) if params[:tabbed_by] && params[:value]
if params[:tabbed_by] && params[:value]
assign_tabbed_by(blank_record, @column, params[:tabbed_by], params[:value], params[:value_type])
end
end
end

def assign_tabbed_by(record, parent_column, tabbed_by, value, value_type)
if (association = tabbed_by_association(parent_column, tabbed_by))
klass = value_type&.constantize || association.klass
end
record.send "#{tabbed_by}=", klass&.find(value) || value
end

def find_associated_record
@column.association.klass.find(params[:associated_id]).tap do |record|
save_record_to_association(record, @column.association.reverse_association, @parent_record, @column.association)
Expand Down
6 changes: 5 additions & 1 deletion lib/active_scaffold/helpers/controller_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def self.included(controller)
:render_parent_action, :nested_singular_association?,
:main_form_controller, :build_associated,
:generate_temporary_id, :generated_id,
:active_scaffold_config_for
:active_scaffold_config_for, :tabbed_by_association
end
end

Expand Down Expand Up @@ -160,6 +160,10 @@ def render_parent_action
@parent_action
end

def tabbed_by_association(assoc_column, tabbed_by)
assoc_column.association.klass.reflect_on_association(tabbed_by)
end

# build an associated record for association
def build_associated(association, parent_record)
if association.through? && association.through_reflection.collection?
Expand Down
8 changes: 2 additions & 6 deletions lib/active_scaffold/helpers/tabs_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def active_scaffold_current_tabs(column, record, tab_options)
column.each_column do |col|
tabbed_by = col.options[:tabbed_by] || column.tabbed_by
tab_values = record.send(col.name).map(&tabbed_by).compact
if tabbed_by_association?(col, tabbed_by)
if tabbed_by_association(col, tabbed_by)
tab_values.map! { |value| [value, value.id.to_s] }
else
tab_values.map! { |value| [tab_options.find { |_, tab_value, _| value == tab_value }&.first || value, value] }
Expand All @@ -38,15 +38,11 @@ def active_scaffold_current_tabs(column, record, tab_options)
used_choices
end

def tabbed_by_association?(assoc_column, tabbed_by)
assoc_column.association.klass.reflect_on_association(tabbed_by).present?
end

def active_scaffold_tab_options(column, record)
subform_column = column.each_column { |col| break col }
if subform_column
tabbed_by = subform_column.options[:tabbed_by] || column.tabbed_by
if tabbed_by_association?(subform_column, tabbed_by)
if tabbed_by_association(subform_column, tabbed_by)
subform_record = record.send(subform_column.name).first_or_initialize
tab_column = active_scaffold_config_for(subform_column.association.klass).columns[tabbed_by]
end
Expand Down

0 comments on commit 9f26b9d

Please sign in to comment.