<commit_before>require_dependency "rhinoart/application_controller"

module Rhinoart
	class UsersController < BaseController
		before_action { authorize! :manage, :users }
		before_action :set_user, only: [:show, :edit, :update, :destroy]        

		def index
			order_str = params[:sort].present? ? "#{params[:sort]} #{params[:dir]}" : 'name, email'

			if params[:role].present?
				case params[:role]
				when 'new'
					@users = User.where(approved: false).paginate(page: params[:page], per_page: 30).order(order_str)
				
				when 'admin'
					@users = User.where("approved = 1 and (admin_role is not null or admin_role != '')").paginate(page: params[:page], per_page: 30).order(order_str)
				
				else
					@users = User.all.paginate(page: params[:page], per_page: 30).order(order_str)
				end
			else
				if params[:q].present?
					@users = User.where('email LIKE ? or info LIKE ?', "%#{params[:q]}%", "%#{params[:q]}%").paginate(page: params[:page], per_page: 30).order(order_str)
				else
					@users = User.paginate(page: params[:page], per_page: 30).order(order_str)
				end
			end 
		end

		def show
			@works = PaperTrail::Version.where(whodunnit: @user).order(created_at: :desc).paginate(:page => params[:page], :per_page => 40)
		end	

		def new
			@user = User.new
		end

		def create
			@user = User.new(user_attributes)
			if @user.save
				flash[:success] = t("_WELCOME")
				redirect_to users_path
			else
				render 'new'
			end
		end

		def edit  
		end

		def update
			new_attributes = user_attributes.to_hash.symbolize_keys
			new_attributes.delete(:password) if new_attributes[:password].blank?

			if new_attributes[:approved] && !new_attributes[:email].present?
			else
			  new_attributes[:admin_role] = nil if !new_attributes[:admin_role].present? && can?(:manage, :all)
			  begin
			  		new_attributes[:api_role] = nil if !new_attributes[:api_role].present? && can?(:manage, :all)
			  rescue			  	
			  end			  
			end 

			if @user.update_attributes(new_attributes)
			  redirect_to (params[:redirect_to] || :users), success: "User created"
			else
			  render :edit
			end
		end  

		def destroy
			if params[:hard_delete]
				@user.destroy
			else
				@user.update(admin_role: nil, frontend_role: nil, approved: false)
			end

			redirect_to (params[:redirect_to] || :users), success: "User soft deleted"
		end

		private
			# Use callbacks to share common setup or constraints between actions.
			def set_user<commit_msg>Fix user update<commit_after>require_dependency "rhinoart/application_controller"

module Rhinoart
	class UsersController < BaseController
		before_action { authorize! :manage, :users }
		before_action :set_user, only: [:show, :edit, :update, :destroy]        

		def index
			order_str = params[:sort].present? ? "#{params[:sort]} #{params[:dir]}" : 'name, email'

			if params[:role].present?
				case params[:role]
				when 'new'
					@users = User.where(approved: false).paginate(page: params[:page], per_page: 30).order(order_str)
				
				when 'admin'
					@users = User.where("approved = 1 and (admin_role is not null or admin_role != '')").paginate(page: params[:page], per_page: 30).order(order_str)
				
				else
					@users = User.all.paginate(page: params[:page], per_page: 30).order(order_str)
				end
			else
				if params[:q].present?
					@users = User.where('email LIKE ? or info LIKE ?', "%#{params[:q]}%", "%#{params[:q]}%").paginate(page: params[:page], per_page: 30).order(order_str)
				else
					@users = User.paginate(page: params[:page], per_page: 30).order(order_str)
				end
			end 
		end

		def show
			@works = PaperTrail::Version.where(whodunnit: @user).order(created_at: :desc).paginate(:page => params[:page], :per_page => 40)
		end	

		def new
			@user = User.new
		end

		def create
			@user = User.new(user_attributes)
			if @user.save
				flash[:success] = t("_WELCOME")
				redirect_to users_path
			else
				render 'new'
			end
		end

		def edit  
		end

		def update
			new_attributes = user_attributes.to_hash.symbolize_keys
			new_attributes.delete(:password) if new_attributes[:password].blank?

			if new_attributes[:approved] && !new_attributes[:email].present?
			else
			  new_attributes[:admin_role] = nil if !new_attributes[:admin_role].present? && can?(:manage, :all)
			  begin
			  		@user.api_role
			  		new_attributes[:api_role] = nil if !new_attributes[:api_role].present? && can?(:manage, :all)
			  rescue			  	
			  end			  
			end 

			if @user.update_attributes(new_attributes)
			  redirect_to (params[:redirect_to] || :users), success: "User created"
			else
			  render :edit
			end
		end  

		def destroy
			if params[:hard_delete]
				@user.destroy
			else
				@user.update(admin_role: nil, frontend_role: nil, approved: false)
			end

			redirect_to (params[:redirect_to] || :users), success: "User soft deleted"
		end

		private
			# Use callbacks to share common setup or constraints between actions.
			def set_user
{'fraction_non_alphanumeric': 0.13551779935275082, 'fraction_numerical': 0.0052588996763754045, 'mean_word_length': 3.6563088512241055, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3390424', 'n_tokens_mistral': 2004, 'n_tokens_neox': 1620, 'n_words': 384}