class Wco::GalleriesController

def update

def update
  params[:gallery][:tag_ids]&.delete ''
  authorize! :update, @gallery
  old_shared_profile_ids = @gallery.shared_profiles.map(&:id)
  if params[:gallery][:shared_profiles].present?
    params[:gallery][:shared_profiles].delete('')
  end
  params[:gallery][:shared_profile_ids] = params[:gallery][:shared_profiles]
  params[:gallery].delete :shared_profiles
  flag = @gallery.update_attributes( params[:gallery].permit! )
  if flag
    if params[:gallery][:shared_profile_ids].present?
      new_shared_profiles = Wco::Profile.find( params[:gallery][:shared_profile_ids]
        ).select { |p| !old_shared_profile_ids.include?( p.id ) }
      Wco::GalleriesMailer.shared_galleries( new_shared_profiles, @gallery ).deliver
    end
    flash[:notice] = 'Success.'
    redirect_to edit_gallery_path(@gallery)
  else
    puts! @gallery.errors.messages, 'cannot save gallery'
    flash[:alert] = 'No Luck. ' + @gallery.errors.messages.to_s
    render :action => :edit
  end
end