Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pipe chain should start with a raw value

I have the following action in my Phoenix app controller:

  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 run mix credo it returns 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've tried to refactor that but I didn't find solution that will makes credo happy. Any ideas how to solve this?

like image 739
Mateusz Urbański Avatar asked Jan 03 '23 19:01

Mateusz Urbański


2 Answers

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

queryable
|> Repo.all()
|> Repo.preload(:address)
like image 113
NoDisplayName Avatar answered May 14 '23 06:05

NoDisplayName


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
like image 21
Nicolas Garnil Avatar answered May 14 '23 08:05

Nicolas Garnil