The pipe chain must start with a raw value

My Phoenix App Controller has the following action:

  def index(conn, params) do
    studios =
      if params["search"] do
        Studio.search(Studio, params["search"])
      else
        Studio
      end
      |> Repo.all
      |> Repo.preload(:address)

    render conn, studios: studios
  end

      

When my startup mix credo

returns the following warning:

┃ [F] → Pipe chain should start with a raw value.
┃       lib/tattoo_backend/web/controllers/api/v1/studio_controller.ex:21 #(TattooBackend.Web.API.V1.StudioController.index)

      

I tried to refactor this, but I haven't found a solution that makes the creed happy. Any ideas how to solve this?

+3


source to share


3 answers


queryable = 
  if params["search"] do
    Studio.search(Studio, params["search"])
  else
    Studio
  end

queryable
|> Repo.all()
|> Repo.preload(:address)

      



+3


source


Using pattern matching:



def index(conn, params) do
  params
  |> search
  |> Repo.all()
  |> Repo.preload(:address)
end

defp search(%{"search" => search}) do
  Studio.search(Studio, search)
end
defp search(_), do: Studio

      

+3


source


if foo do
  bar
else
  baz
end

      

is equivalent if(foo, do: bar, else: baz)

. Once you know this, you can understand what the error message means: params["search"]

should be passed to if

. This should fix the warning:

def index(conn, params) do
  studios =
    params["search"]
    |> if do
      Studio.search(Studio, params["search"])
    else
      Studio
    end
    |> Repo.all
    |> Repo.preload(:address)

  render conn, studios: studios
end

      

+1


source







All Articles