Created
August 29, 2016 15:40
-
-
Save johnmeehan/7fe9a4e236a0c3b7570db790a497180a to your computer and use it in GitHub Desktop.
Reform prepopulator and allow for deletion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
collection :side_panels, form: SidePanelForm, | |
prepopulator: ->(options) { | |
if side_panels.count == 0 | |
self.side_panels << SidePanel.new(sales_order_id: sales_order_id, collection: sales_order.collection) | |
end | |
}, | |
populator: ->(fragment:, index:, **) { | |
if fragment['panel_id'] == "" #'0' # or 'None' => nil then wouldn't need to make a None on the DB then. | |
deserialized_panel = side_panels.find { |p| p.id.to_s == fragment["id"] } | |
if deserialized_panel | |
deserialized_panel.mark_for_destruction | |
end | |
return skip! | |
end | |
return skip! if side_panels[index] | |
side_panels.insert(index, sales_order.order_items.build(type: 'SidePanel', width: light_width, height: lower_height, collection: sales_order.collection)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment