• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

source-academy / backend / e0330f2cf38b2d8af12bffd20f4cac2158d607fc-PR-1236

31 Mar 2025 09:12AM UTC coverage: 19.982% (-73.6%) from 93.607%
e0330f2cf38b2d8af12bffd20f4cac2158d607fc-PR-1236

Pull #1236

github

RichDom2185
Redate migrations to maintain total ordering
Pull Request #1236: Added Exam mode

12 of 57 new or added lines in 8 files covered. (21.05%)

2430 existing lines in 97 files now uncovered.

671 of 3358 relevant lines covered (19.98%)

3.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/lib/cadet_web/helpers/view_helper.ex
1
defmodule CadetWeb.ViewHelper do
2
  @moduledoc """
3
  Helper functions shared throughout views
4
  """
5

6
  defp build_staff(course_reg) do
UNCOV
7
    transform_map_for_view(course_reg, %{name: fn st -> st.user.name end, id: :id})
×
8
  end
9

10
  def unsubmitted_by_builder(nil), do: nil
×
11

12
  def unsubmitted_by_builder(staff) do
13
    build_staff(staff)
×
14
  end
15

UNCOV
16
  def grader_builder(nil), do: nil
×
17

18
  def grader_builder(_) do
UNCOV
19
    fn %{grader: grader} -> build_staff(grader) end
×
20
  end
21

UNCOV
22
  def graded_at_builder(nil), do: nil
×
23

24
  def graded_at_builder(_) do
UNCOV
25
    fn %{updated_at: updated_at} -> format_datetime(updated_at) end
×
26
  end
27

UNCOV
28
  def format_datetime(nil), do: nil
×
29

30
  def format_datetime(datetime = %DateTime{}) do
31
    datetime
32
    |> DateTime.truncate(:millisecond)
UNCOV
33
    |> Timex.format!("{ISO:Extended}")
×
34
  end
35

36
  def format_datetime(datetime = %NaiveDateTime{}) do
37
    datetime
38
    |> Timex.to_datetime()
UNCOV
39
    |> format_datetime()
×
40
  end
41

42
  @doc """
43
  This function allows you to build a map for a view from a map of transformations or a list of fields.
44

45
  Given a `key_list`, it is the equivalent of `Map.take(source, key_list)`.
46

47
  Given a map of `%{view_field: source_field, ...}`, it is the equivalent of `%{view_field: Map.get(source, source_field), ...}`
48

49
  Given a map of `%{view_field: source_function, ...}`, it is the equivalent of `%{view_field: apply(source_function, source)}`
50

51
  Examples:
52
  ```
53
  source = %{
54
    foofoo: "ho",
55
    barbar: "ha",
56
    foobar: "hoha"
57
  }
58

59
  field_list = [:foofoo, :barbar]
60

61
  transform_map_for_view(source, field_list)
62
  > %{
63
    foofoo: "ho",
64
    barbar: "ha"
65
  }
66

67
  key_transformations = %{
68
    foo: :foofoo,
69
    bar: :barbar
70
  }
71

72
  transform_map_for_view(source, key_transformations)
73
  > %{
74
    foo: Map.get(source, :foofoo),
75
    bar: Map.get(source, :barbar)
76
  }
77

78
  function_transformations = %{
79
    foo: fn source -> source.foofoo <> "hoho",
80
    bar: fn source -> source.barbar <> "barbar"
81
  }
82

83
  transform_map_for_view(source, function_transformations)
84
  > %{
85
    foo: source.foofoo <> "hoho",
86
    bar: source.barbar <> "barbar"
87
  }
88
  ```
89
  """
90
  def transform_map_for_view(source, transformations) when is_map(transformations) do
UNCOV
91
    Enum.reduce(
×
92
      transformations,
93
      %{},
94
      fn {field_name, transformation}, acc ->
UNCOV
95
        Map.put(acc, field_name, get_value(transformation, source))
×
96
      end
97
    )
98
  end
99

100
  def transform_map_for_view(source, fields) when is_list(fields) do
UNCOV
101
    transform_map_for_view(
×
102
      source,
UNCOV
103
      Enum.reduce(fields, %{}, fn field, acc -> Map.put(acc, field, field) end)
×
104
    )
105
  end
106

107
  defp get_value(source_spec, value_store) when is_function(source_spec) do
UNCOV
108
    Kernel.apply(source_spec, [value_store])
×
109
  end
110

111
  defp get_value(source_spec, value_store) when is_binary(source_spec) or is_atom(source_spec) do
UNCOV
112
    Map.get(value_store, source_spec)
×
113
  end
114
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc