app/controllers/binda/field_groups_controller.rb in binda-0.0.6 vs app/controllers/binda/field_groups_controller.rb in binda-0.0.7

- old
+ new

@@ -30,31 +30,14 @@ redirect_to new_structure_field_group_path( @structure.slug ), flash: { alert: @field_group.errors } end end def update - # Create new fields if any - new_params[:new_field_settings].each do |field_setting| - unless field_setting[:name].blank? - new_field_setting = @field_group.field_settings.create( field_setting ) - unless new_field_setting - return redirect_to edit_structure_field_group_path( @structure.slug, @field_group.slug ), flash: { error: new_field_setting.errors } - end - end - end - # Create new fields if any - unless new_params[:new_choices].nil? - new_params[:new_choices].each do |choice| - unless choice[:label].blank? || choice[:value].blank? - new_choice = Choice.create( choice ) - unless new_choice - return redirect_to edit_structure_field_group_path( @structure.slug, @field_group.slug ), flash: { error: new_choice.errors } - end - end - end - end + # Add nested classes + add_new_field_settings + add_new_choices # Update the other ones if @field_group.update(field_group_params) reset_field_settings_cache redirect_to structure_field_group_path( @structure.slug, @field_group.slug ), notice: 'Field group was successfully updated.' @@ -98,26 +81,28 @@ :field_type, :position, :required, :default_text, :ancestry, - :default_choice_id_id, + :default_choice_id, :allow_null, choices: [], choices_attributes: - [ :field_setting_id, + [ :id, + :field_setting_id, :label, :value ] ] ) end def new_params params.require(:field_group).permit( new_field_settings: - [ :field_group_id, + [ :id, + :field_group_id, :field_setting_id, :name, :slug, :description, :field_type, @@ -127,18 +112,45 @@ :default_choice_id, :allow_null, choices: [] ], new_choices: - [ :field_setting_id, + [ :id, + :field_setting_id, :label, :value ] ) end def reset_field_settings_cache FieldSetting.reset_field_settings_array + end + + def add_new_field_settings + # Create new fields if any + new_params[:new_field_settings].each do |field_setting| + unless field_setting[:name].blank? + new_field_setting = @field_group.field_settings.create( field_setting ) + unless new_field_setting + return redirect_to edit_structure_field_group_path( @structure.slug, @field_group.slug ), flash: { error: new_field_setting.errors } + end + end + end + end + + def add_new_choices + # Create new fields if any + unless new_params[:new_choices].nil? + new_params[:new_choices].each do |choice| + unless choice[:label].blank? || choice[:value].blank? + new_choice = Choice.create( choice ) + unless new_choice + return redirect_to edit_structure_field_group_path( @structure.slug, @field_group.slug ), flash: { error: new_choice.errors } + end + end + end + end end end end