Using Services to vailidar + ruby on rails

1

I'm trying to create a service that will check if a user exists

class UserService
  def create_user(user_username, user_email)
    if User.find(user_username)
      false
    end
    if User.find(user_email)
      false
    end
    true
  end
end

And I call it on the chalkboard of my controler

def create
  if UserService.create_user(params[:username], params[:email])
    @user = User.new(user_params)
    if @user.save
      render json: @user, status: :created, location: api_user_path(@user)
    end

    render json: "exist", status: :unprocessable_entity

  end

  render json: @user.errors, status: :unprocessable_entity
end

But this is giving error on the line where I call

undefined method 'create_user' for UserService:Class

claim can you help me? I'm new with ruby on rails and actually I even asked another question about how services work, but there I found some things and tried to start on my own, but I have no idea if what I'm doing is right.

Just to make it clear I know it would work if I put it right in the controler, but I'm trying to learn how to use services in ruby, so I'm doing it that way.

    
asked by anonymous 13.01.2016 / 21:55

3 answers

0

I was able to do it after reading several articles and asking a lot of people. Well I had some error on the way it was pretty wrong what I was doing.

Then to make the service call, it would have two ways or I should hold an object or say that my method is static and that was the solution I approached. Just put a self.<nome do metodo> that ready it is static.

Another mistake was in the way he was doing the searches. In the end it looks like this:

My method crete no controler:

def create
    if UserService.create_user(params[:username], params[:email])
      @user = User.new(user_params)
      if @user.save
        render json: @user, status: :created, location: api_user_path(@user)
        return
      end
      render json: @user.errors, status: :unprocessable_entity
      return
    end

    render json: "exit", status: :unprocessable_entity
  end

My service:

class UserService
  def self.create_user(user_username, user_email)
    if User.find_by_username(user_username)
      return false
    end
    if User.find_by_email(user_email)
      return false
    end
    return true
  end
end
    
14.01.2016 / 17:38
2

You can give a refactor in this service, using 1 query instead of 2 like this:

class UserService
  def self.create_user(user_username, user_email)
    return false if User.where("username =? or email = ?", user_username, email).any?
    true
  end
end
    
18.01.2016 / 15:06
1

Is it possible to put your code in github to see it completely? You do not need to use a service for that you would just put in your users_controller.rb

def create
  @user = User.new(user_params)
  if @user.save
    render json: @user, status: :created, location: api_user_path(@user)
  else
    render json: @user.errors, status: :unprocessable_entity
  end
end

ae in your User model you put:

class User < ActiveRecord::Base
  validates :email, uniqueness: true
  validates :user_name, uniqueness: true

It is important to remember that uniqueness conditions must be added to your database, not only in the application. I hope I have helped.

EDIT

Here is a suggested refactor after seeing your comment.

class UserService
  def self.create_user?(user_username, user_email)
    !User.where('email = ? OR username = ?', user_email, user_username).exists?
  end
end
    
14.01.2016 / 15:12