diff --git a/app/controllers/users/contributions_controller.rb b/app/controllers/users/contributions_controller.rb index 6c1d3574..bb02d63b 100644 --- a/app/controllers/users/contributions_controller.rb +++ b/app/controllers/users/contributions_controller.rb @@ -25,12 +25,18 @@ def new end def create - @osbl = Osbl.new(osbl_params) + osbl_params = contribution_params.delete(:osbl) + contribution = @user.contributions.build(contribution_params) + osbl = Osbl.new(osbl_params) - if @osbl.save + if osbl.valid? + osbl_data = OsblDataTransformer.new(osbl_params).transform + contribution.contributable = OsblCreation.new(osbl_data: osbl_data) + + contribution.save! redirect_to my_contributions_path, success: "Votre contribution a été enregistrée." else - redirect_to my_new_contribution_path, inertia: {errors: @osbl.errors} + redirect_to my_new_contribution_path, inertia: {errors: osbl.errors} end end @@ -40,62 +46,68 @@ def get_user @user = User.find(params[:user_id]) end - def osbl_params - params.permit( - :name, - :website, - :logo, - :description, - {osbls_causes_attributes: [:cause_id]}, - :tax_reduction, - {osbls_keywords_attributes: [:keyword_id]}, - :geographical_scale, - {osbls_intervention_areas_attributes: [:intervention_area_id]}, - :osbl_type, - :public_utility, - :creation_year, - {osbls_labels_attributes: [:label_id]}, - {annual_finances_attributes: [ - :year, - :certified, - :budget, - :treasury, - :employees_count, - {fund_sources_attributes: [ - :type, - :percent, - :amount - ]}, - {fund_allocations_attributes: [ - :type, - :percent, - :amount - ]} - ]}, - {document_attachments_attributes: [ - {document_attributes: [ - :type, - :file, + def contribution_params + @contribution_params ||= params.expect( + contribution: [ + :body, + files: [], + osbl: [ :name, - :year, - :description - ]} - ]}, - {locations_attributes: [ - :type, - :name, - :description, - :website, - {address_attributes: [ - :street_number, - :street_name, - :additional_info, - :postal_code, - :city, - :latitude, - :longitude - ]} - ]} + :website, + :logo, + :description, + :tax_reduction, + :geographical_scale, + :osbl_type, + :public_utility, + :creation_year, + osbls_causes_attributes: [[:cause_id]], + osbls_keywords_attributes: [[:keyword_id]], + osbls_intervention_areas_attributes: [[:intervention_area_id]], + osbls_labels_attributes: [[:label_id]], + annual_finances_attributes: [[ + :year, + :certified, + :budget, + :treasury, + :employees_count, + fund_sources_attributes: [[ + :type, + :percent, + :amount + ]], + fund_allocations_attributes: [[ + :type, + :percent, + :amount + ]] + ]], + document_attachments_attributes: [[ + document_attributes: [ + :type, + :file, + :name, + :year, + :description + ] + ]], + locations_attributes: [[ + :type, + :name, + :description, + :website, + address_attributes: [ + :street_number, + :street_name, + :additional_info, + :postal_code, + :city, + :latitude, + :longitude + ] + ]] + ] + ] ) end diff --git a/app/frontend/components/pages/contribution/new/OsblHeader.tsx b/app/frontend/components/pages/contribution/new/OsblHeader.tsx index 67a507fc..88c37fdd 100644 --- a/app/frontend/components/pages/contribution/new/OsblHeader.tsx +++ b/app/frontend/components/pages/contribution/new/OsblHeader.tsx @@ -39,7 +39,7 @@ export default function OsblHeader ({ data, setData, errors, clearErrors }: Omit