Skip to content

Instantly share code, notes, and snippets.

@scrogson
Forked from paulcsmith/comment_view.ex
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save scrogson/e31216e71b1bfbdc871f to your computer and use it in GitHub Desktop.

Select an option

Save scrogson/e31216e71b1bfbdc871f to your computer and use it in GitHub Desktop.

Revisions

  1. Paul Smith revised this gist Mar 18, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion web.ex
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,9 @@
    defmodule MyApp.Web do
    def view do
    quote do
    # Default code
    # Default code.

    # You would probably extract these functions to a module.
    def put_assoc(map, :user, user) do
    Map.put(map, :user, UserView.render("show.json", %{data: user})
    end
  2. Paul Smith revised this gist Mar 18, 2015. 2 changed files with 3 additions and 2 deletions.
    3 changes: 2 additions & 1 deletion comment_view.ex
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,11 @@
    defmodule MyApp.CommentView do
    defmodule MyApp.CommentView do
    use MyApp.Web, :view
    @attributes ~W(id name inserted_at)

    def render("index.json", %{data: comments}) do
    for comment <- comments, do: render("show.json", %{data: comment})
    end

    def render("show.json", %{data: comment}) do
    comment
    |> Map.take(@attributes)
    2 changes: 1 addition & 1 deletion user_view.ex
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    defmodule MyApp.UserView do
    defmodule MyApp.UserView do
    use MyApp.Web, :view
    @attributes ~W(id name inserted_at)

  3. Paul Smith revised this gist Mar 18, 2015. 3 changed files with 26 additions and 4 deletions.
    2 changes: 1 addition & 1 deletion comment_view.ex
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,6 @@ defmodule MyApp.CommentView do
    def render("show.json", %{data: comment}) do
    comment
    |> Map.take(@attributes)
    |> Map.put(:user, UserView.render('show_lite.json', %{data: data.user})
    |> put_assoc(:user, comment.user)
    end
    end
    8 changes: 5 additions & 3 deletions mymodel_view.ex
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,14 @@
    defmodule MyApp.MyModelView do
    defmodule MyApp.MyModelView do
    use MyApp.Web, :view
    @attributes ~W(id body title inserted_at)

    def render("show.json", %{data: my_model})
    my_model
    |> Map.take(@attributes)
    |> Map.update!(:title, &String.capitalize)
    |> Map.put(:user, UserView.render("show.json", %{data: my_model.user})
    |> Map.put(:comments, CommentView.render("index.json", %{data: my_model.comments})
    |> put_assoc(:user, my_model.user)
    # Or you could do
    # |> put_assoc(:user, my_model.user, :lite)
    |> put_assoc(:comments, my_model.comments)
    end
    end
    20 changes: 20 additions & 0 deletions web.ex
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    defmodule MyApp.Web do
    def view do
    quote do
    # Default code

    def put_assoc(map, :user, user) do
    Map.put(map, :user, UserView.render("show.json", %{data: user})
    end

    # Need to customize more?
    def put_assoc(map, :user, user, :lite) do
    Map.put(map, :user, UserView.render("show_lite.json", %{data: user})
    end

    def put_assoc(map, :comments, comments) do
    Map.put(map, :comments, CommentView.render("index.json", %{data: comments})
    end
    end
    end
    end
  4. Paul Smith revised this gist Mar 18, 2015. 3 changed files with 4 additions and 4 deletions.
    4 changes: 2 additions & 2 deletions comment_view.ex
    Original file line number Diff line number Diff line change
    @@ -2,12 +2,12 @@ defmodule MyApp.CommentView do
    use MyApp.Web, :view
    @attributes ~W(id name inserted_at)

    def render("show.json", %{data: comments}) when is_list(comments) do
    def render("index.json", %{data: comments}) do
    for comment <- comments, do: render("show.json", %{data: comment})
    end
    def render("show.json", %{data: comment}) do
    comment
    |> Map.take(@attributes)
    |> Map.put(:user, UserView.render('show_lite.json', data: data.user)
    |> Map.put(:user, UserView.render('show_lite.json', %{data: data.user})
    end
    end
    2 changes: 1 addition & 1 deletion mymodel_view.ex
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,6 @@ defmodule MyApp.MyModelView do
    |> Map.take(@attributes)
    |> Map.update!(:title, &String.capitalize)
    |> Map.put(:user, UserView.render("show.json", %{data: my_model.user})
    |> Map.put(:comments, CommentView.render("show.json", %{data: my_model.comments})
    |> Map.put(:comments, CommentView.render("index.json", %{data: my_model.comments})
    end
    end
    2 changes: 1 addition & 1 deletion user_view.ex
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ defmodule MyApp.UserView do

    def render("show.json", %{data: user})
    render("show_lite.json", %{data: user})
    |> Map.put(:image, ImageView.render("show.json", data: user.image)
    |> Map.put(:image, ImageView.render("show.json", %{data: user.image})
    end

    def render("show_lite.json", %{data: user})
  5. Paul Smith revised this gist Mar 18, 2015. 3 changed files with 5 additions and 7 deletions.
    4 changes: 1 addition & 3 deletions comment_view.ex
    Original file line number Diff line number Diff line change
    @@ -3,9 +3,7 @@ defmodule MyApp.CommentView do
    @attributes ~W(id name inserted_at)

    def render("show.json", %{data: comments}) when is_list(comments) do
    for comment <- comments do
    render("show.json", data: comment)
    end
    for comment <- comments, do: render("show.json", %{data: comment})
    end
    def render("show.json", %{data: comment}) do
    comment
    4 changes: 2 additions & 2 deletions mymodel_view.ex
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ defmodule MyApp.MyModelView do
    my_model
    |> Map.take(@attributes)
    |> Map.update!(:title, &String.capitalize)
    |> Map.put(:user, UserView.render("show.json", data: my_model.user)
    |> Map.put(:comments, CommentView.render("show.json", data: my_model.comments)
    |> Map.put(:user, UserView.render("show.json", %{data: my_model.user})
    |> Map.put(:comments, CommentView.render("show.json", %{data: my_model.comments})
    end
    end
    4 changes: 2 additions & 2 deletions user_view.ex
    Original file line number Diff line number Diff line change
    @@ -3,11 +3,11 @@ defmodule MyApp.UserView do
    @attributes ~W(id name inserted_at)

    def render("show.json", %{data: user})
    render("show.json", %{data: user}, :lite)
    render("show_lite.json", %{data: user})
    |> Map.put(:image, ImageView.render("show.json", data: user.image)
    end

    def render("show.json", %{data: user}, :lite)
    def render("show_lite.json", %{data: user})
    user
    |> Map.take(@attributes)
    end
  6. Paul Smith revised this gist Mar 18, 2015. 3 changed files with 6 additions and 7 deletions.
    5 changes: 2 additions & 3 deletions comment_view.ex
    Original file line number Diff line number Diff line change
    @@ -2,13 +2,12 @@ defmodule MyApp.CommentView do
    use MyApp.Web, :view
    @attributes ~W(id name inserted_at)

    def render("show.json", data: comments) when is_list(comments) do
    def render("show.json", %{data: comments}) when is_list(comments) do
    for comment <- comments do
    render("show.json", data: comment)
    end
    end

    def render("show.json", data: comment) do
    def render("show.json", %{data: comment}) do
    comment
    |> Map.take(@attributes)
    |> Map.put(:user, UserView.render('show_lite.json', data: data.user)
    2 changes: 1 addition & 1 deletion mymodel_view.ex
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ defmodule MyApp.MyModelView do
    use MyApp.Web, :view
    @attributes ~W(id body title inserted_at)

    def render("show.json", data: my_model)
    def render("show.json", %{data: my_model})
    my_model
    |> Map.take(@attributes)
    |> Map.update!(:title, &String.capitalize)
    6 changes: 3 additions & 3 deletions user_view.ex
    Original file line number Diff line number Diff line change
    @@ -2,12 +2,12 @@ defmodule MyApp.UserView do
    use MyApp.Web, :view
    @attributes ~W(id name inserted_at)

    def render("show.json", data: user)
    render("show_lite.json", data: user)
    def render("show.json", %{data: user})
    render("show.json", %{data: user}, :lite)
    |> Map.put(:image, ImageView.render("show.json", data: user.image)
    end

    def render("show_lite.json", data: user)
    def render("show.json", %{data: user}, :lite)
    user
    |> Map.take(@attributes)
    end
  7. Paul Smith revised this gist Mar 18, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions comment_view.ex
    Original file line number Diff line number Diff line change
    @@ -3,8 +3,8 @@ defmodule MyApp.CommentView do
    @attributes ~W(id name inserted_at)

    def render("show.json", data: comments) when is_list(comments) do
    for c <- comments do
    render("show.json", data: c)
    for comment <- comments do
    render("show.json", data: comment)
    end
    end

  8. Paul Smith revised this gist Mar 18, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions comment_view.ex
    Original file line number Diff line number Diff line change
    @@ -2,9 +2,9 @@ defmodule MyApp.CommentView do
    use MyApp.Web, :view
    @attributes ~W(id name inserted_at)

    def render("show.json", data: comment) when is_list(comment) do
    for c <- data do
    render("show.json", data: data)
    def render("show.json", data: comments) when is_list(comments) do
    for c <- comments do
    render("show.json", data: c)
    end
    end

  9. Paul Smith revised this gist Mar 18, 2015. 3 changed files with 12 additions and 12 deletions.
    6 changes: 3 additions & 3 deletions comment_view.ex
    Original file line number Diff line number Diff line change
    @@ -2,14 +2,14 @@ defmodule MyApp.CommentView do
    use MyApp.Web, :view
    @attributes ~W(id name inserted_at)

    def render("show.json", data: data) when is_list(data) do
    def render("show.json", data: comment) when is_list(comment) do
    for c <- data do
    render("show.json", data: data)
    end
    end

    def render("show.json", data: data) do
    data
    def render("show.json", data: comment) do
    comment
    |> Map.take(@attributes)
    |> Map.put(:user, UserView.render('show_lite.json', data: data.user)
    end
    8 changes: 4 additions & 4 deletions mymodel_view.ex
    Original file line number Diff line number Diff line change
    @@ -2,11 +2,11 @@ defmodule MyApp.MyModelView do
    use MyApp.Web, :view
    @attributes ~W(id body title inserted_at)

    def render("show.json", data: data)
    data
    def render("show.json", data: my_model)
    my_model
    |> Map.take(@attributes)
    |> Map.update!(:title, &String.capitalize)
    |> Map.put(:user, UserView.render("show.json", data: data.user)
    |> Map.put(:comments, CommentView.render("show.json", data: data.comments)
    |> Map.put(:user, UserView.render("show.json", data: my_model.user)
    |> Map.put(:comments, CommentView.render("show.json", data: my_model.comments)
    end
    end
    10 changes: 5 additions & 5 deletions user_view.ex
    Original file line number Diff line number Diff line change
    @@ -2,13 +2,13 @@ defmodule MyApp.UserView do
    use MyApp.Web, :view
    @attributes ~W(id name inserted_at)

    def render("show.json", data: data)
    render("show_lite.json", data: data)
    |> Map.put(:image, ImageView.render("show.json", data: data.image)
    def render("show.json", data: user)
    render("show_lite.json", data: user)
    |> Map.put(:image, ImageView.render("show.json", data: user.image)
    end

    def render("show_lite.json", data: data)
    data
    def render("show_lite.json", data: user)
    user
    |> Map.take(@attributes)
    end
    end
  10. Jason S. revised this gist Mar 18, 2015. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions user_view.ex
    Original file line number Diff line number Diff line change
    @@ -11,6 +11,4 @@ defmodule MyApp.UserView do
    data
    |> Map.take(@attributes)
    end


    end
  11. Jason S. revised this gist Mar 18, 2015. 3 changed files with 13 additions and 18 deletions.
    10 changes: 4 additions & 6 deletions comment_view.ex
    Original file line number Diff line number Diff line change
    @@ -2,17 +2,15 @@ defmodule MyApp.CommentView do
    use MyApp.Web, :view
    @attributes ~W(id name inserted_at)

    def render("show.json", data: data), do: encode(data)

    def encode(data) when is_list(data) do
    def render("show.json", data: data) when is_list(data) do
    for c <- data do
    encode(data)
    render("show.json", data: data)
    end
    end

    def encode(data) do
    def render("show.json", data: data) do
    data
    |> Map.take(@attributes)
    |> Map.put(:user, UserView.encode_lite(data.user)
    |> Map.put(:user, UserView.render('show_lite.json', data: data.user)
    end
    end
    8 changes: 3 additions & 5 deletions mymodel_view.ex
    Original file line number Diff line number Diff line change
    @@ -2,13 +2,11 @@ defmodule MyApp.MyModelView do
    use MyApp.Web, :view
    @attributes ~W(id body title inserted_at)

    def render("show.json", data: data), do: encode(data)

    def encode(data) do
    def render("show.json", data: data)
    data
    |> Map.take(@attributes)
    |> Map.update!(:title, &String.capitalize)
    |> Map.put(:user, UserView.encode(data.user)
    |> Map.put(:comments, CommentView.encode(data.comments)
    |> Map.put(:user, UserView.render("show.json", data: data.user)
    |> Map.put(:comments, CommentView.render("show.json", data: data.comments)
    end
    end
    13 changes: 6 additions & 7 deletions user_view.ex
    Original file line number Diff line number Diff line change
    @@ -2,16 +2,15 @@ defmodule MyApp.UserView do
    use MyApp.Web, :view
    @attributes ~W(id name inserted_at)

    def render("show.json", data: data), do: encode(data)
    def render("show.json", data: data)
    render("show_lite.json", data: data)
    |> Map.put(:image, ImageView.render("show.json", data: data.image)
    end

    def encode_lite(data) do
    def render("show_lite.json", data: data)
    data
    |> Map.take(@attributes)
    end

    def encode(data) do
    data
    |> encode_lite()
    |> Map.put(:image, ImageView.encode(data.image)
    end

    end
  12. Jason S. revised this gist Mar 17, 2015. 3 changed files with 4 additions and 4 deletions.
    2 changes: 1 addition & 1 deletion comment_view.ex
    Original file line number Diff line number Diff line change
    @@ -13,6 +13,6 @@ defmodule MyApp.CommentView do
    def encode(data) do
    data
    |> Map.take(@attributes)
    |> Map.put(:user, MyApp.UserView.encode_lite(data.user)
    |> Map.put(:user, UserView.encode_lite(data.user)
    end
    end
    4 changes: 2 additions & 2 deletions mymodel_view.ex
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ defmodule MyApp.MyModelView do
    data
    |> Map.take(@attributes)
    |> Map.update!(:title, &String.capitalize)
    |> Map.put(:user, MyApp.UserView.encode(data.user)
    |> Map.put(:comments, MyApp.CommentView.encode(data.comments)
    |> Map.put(:user, UserView.encode(data.user)
    |> Map.put(:comments, CommentView.encode(data.comments)
    end
    end
    2 changes: 1 addition & 1 deletion user_view.ex
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,6 @@ defmodule MyApp.UserView do
    def encode(data) do
    data
    |> encode_lite()
    |> Map.put(:image, MyApp.ImageView.encode(data.image)
    |> Map.put(:image, ImageView.encode(data.image)
    end
    end
  13. Jason S. revised this gist Mar 17, 2015. 3 changed files with 11 additions and 2 deletions.
    1 change: 1 addition & 0 deletions comment_view.ex
    Original file line number Diff line number Diff line change
    @@ -13,5 +13,6 @@ defmodule MyApp.CommentView do
    def encode(data) do
    data
    |> Map.take(@attributes)
    |> Map.put(:user, MyApp.UserView.encode_lite(data.user)
    end
    end
    4 changes: 3 additions & 1 deletion mymodel_view.ex
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,14 @@
    defmodule MyApp.MyModelView do
    use MyApp.Web, :view
    @attributes ~W(id body title user comments inserted_at)
    @attributes ~W(id body title inserted_at)

    def render("show.json", data: data), do: encode(data)

    def encode(data) do
    data
    |> Map.take(@attributes)
    |> Map.update!(:title, &String.capitalize)
    |> Map.put(:user, MyApp.UserView.encode(data.user)
    |> Map.put(:comments, MyApp.CommentView.encode(data.comments)
    end
    end
    8 changes: 7 additions & 1 deletion user_view.ex
    Original file line number Diff line number Diff line change
    @@ -4,8 +4,14 @@ defmodule MyApp.UserView do

    def render("show.json", data: data), do: encode(data)

    def encode(data) do
    def encode_lite(data) do
    data
    |> Map.take(@attributes)
    end

    def encode(data) do
    data
    |> encode_lite()
    |> Map.put(:image, MyApp.ImageView.encode(data.image)
    end
    end
  14. Jason S. revised this gist Mar 17, 2015. 1 changed file with 17 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions comment_view.ex
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    defmodule MyApp.CommentView do
    use MyApp.Web, :view
    @attributes ~W(id name inserted_at)

    def render("show.json", data: data), do: encode(data)

    def encode(data) when is_list(data) do
    for c <- data do
    encode(data)
    end
    end

    def encode(data) do
    data
    |> Map.take(@attributes)
    end
    end
  15. Jason S. revised this gist Mar 17, 2015. 2 changed files with 14 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion mymodel_view.ex
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,9 @@ defmodule MyApp.MyModelView do
    use MyApp.Web, :view
    @attributes ~W(id body title user comments inserted_at)

    def render("show.json", data: data) do
    def render("show.json", data: data), do: encode(data)

    def encode(data) do
    data
    |> Map.take(@attributes)
    |> Map.update!(:title, &String.capitalize)
    11 changes: 11 additions & 0 deletions user_view.ex
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    defmodule MyApp.UserView do
    use MyApp.Web, :view
    @attributes ~W(id name inserted_at)

    def render("show.json", data: data), do: encode(data)

    def encode(data) do
    data
    |> Map.take(@attributes)
    end
    end
  16. Jason S. revised this gist Mar 17, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mymodel_view.ex
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ defmodule MyApp.MyModelView do
    @attributes ~W(id body title user comments inserted_at)

    def render("show.json", data: data) do
    comment
    data
    |> Map.take(@attributes)
    |> Map.update!(:title, &String.capitalize)
    end
  17. Jason S. created this gist Mar 17, 2015.
    10 changes: 10 additions & 0 deletions mymodel_view.ex
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    defmodule MyApp.MyModelView do
    use MyApp.Web, :view
    @attributes ~W(id body title user comments inserted_at)

    def render("show.json", data: data) do
    comment
    |> Map.take(@attributes)
    |> Map.update!(:title, &String.capitalize)
    end
    end