-
Notifications
You must be signed in to change notification settings - Fork 56
Refactor assessments context #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Pull Request Test Coverage Report for Build 943
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good overall, just some styling issues
lib/cadet/assessments/assessments.ex
Outdated
| Repo.all(Assessment) | ||
| end | ||
|
|
||
| def all_assessments(assessment_type) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should wait until my PR #101 is merged cos I added max_xp virtual field as well as query to fetch that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure lets do that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you wanna add the max_xp field here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tbh i'm thinking of removing these methods. nothing's using them right now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha yes. I don't think anyone would miss it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I'm thinking if it's possible to refactor Assessments context such that we can remove the assessments_test.exs altogether and getting coverage just from the controller tests. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can do away with a lot of them but we still need the functions that build questions/assessments
lib/cadet/assessments/assessments.ex
Outdated
| def all_assessments(assessment_type) do | ||
| Repo.all(from(a in Assessment, where: a.type == ^assessment_type)) | ||
| Assessment | ||
| |> where([a], a.type == ^assessment_type) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|> where(type: ^assessment_type)
lib/cadet/assessments/assessments.ex
Outdated
| # TODO: Refactor to be done on SQL instead of in-memory | ||
| Enum.filter(assessment_with_type, &(&1.is_published and Timex.before?(&1.open_at, now))) | ||
| Assessment | ||
| |> where([a], a.is_published) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same thing, use the shorter where wherever possible
lib/cadet/assessments/assessments.ex
Outdated
| Enum.filter(assessment_with_type, &(&1.is_published and Timex.before?(&1.open_at, now))) | ||
| Assessment | ||
| |> where([a], a.is_published) | ||
| |> where([a], a.open_at <= from_now(1, "second")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not 0 second lol
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bump ^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh just wanted to give a little buffer, although I guess it makes no difference either way
lib/cadet/assessments/assessments.ex
Outdated
| end | ||
|
|
||
| def create_question_for_assessment(params, assessment_id) | ||
| when is_binary(assessment_id) or is_number(assessment_id) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad, should be is_integer(assessment_id) instead of is_number
| assessment = | ||
| Assessment | ||
| |> where([assessment], assessment.id == ^assessment_id) | ||
| |> join( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preferable to use one letter bindings, i.e.
|> join(:left, [a], q in Question, q.assessment_id == a.id)
lib/cadet/assessments/assessments.ex
Outdated
| questions = assessment.questions | ||
| assessment = | ||
| Assessment | ||
| |> where([assessment], assessment.id == ^assessment_id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the short where
| result = Enum.map(Assessments.all_assessments(), & &1.id) | ||
| assert result == assessments | ||
| end | ||
| alias Cadet.Assessments.{Question, Assessment, AssessmentType} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm OCD but prefer alphabetical order {Assessment, AssessmentType, Question}
|
Made amendments as requested. Will wait on your controller before checking and merging again. |
|
Lel might wanna rebase/merge this on/with `master cos I'm pretty sure there'll be quite a number of conflicts @tuesmiddt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some refactor might be required.
Also wondering if we should do from_now(0, "second") instead of 1
lib/cadet/assessments/assessments.ex
Outdated
| Repo.all(Assessment) | ||
| end | ||
|
|
||
| def all_assessments(assessment_type) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you wanna add the max_xp field here?
lib/cadet/assessments/assessments.ex
Outdated
| assessment_with_type = Repo.all(from(a in Assessment, where: a.type == ^assessment_type)) | ||
| # TODO: Refactor to be done on SQL instead of in-memory | ||
| Enum.filter(assessment_with_type, &(&1.is_published and Timex.before?(&1.open_at, now))) | ||
| Assessment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same thing w.r.t. max_xp field
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm thinking of removing this method since assessments_controller is implemented and doesn't use it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I don't think anyone's gonna miss it.
lib/cadet/assessments/assessments.ex
Outdated
| Assessment | ||
| |> where(is_published: true) | ||
| |> where(type: ^assessment_type) | ||
| |> where([a], a.open_at <= from_now(1, "second")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not from_now(0, "second")? 😂
idem for the other from_now's
| |> preload([a, q], questions: q) | ||
| |> Repo.one() | ||
|
|
||
| params_with_assessment_id = Map.put_new(params, :assessment_id, assessment.id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No check if assessment == nil? Might be better to use with
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or refactor into 2 functions:
def create_question_for_assessment(params, assessment_id) when is_ecto_id(assessment_id)
def create_question_for_assessment(params, assessment = %Assessment{})There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some small changes needed
lib/cadet/assessments/assessments.ex
Outdated
| simple_update( | ||
| Assessment, | ||
| id, | ||
| using: &Assessment.changeset/2, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[_, q] lel
lib/cadet/assessments/assessments.ex
Outdated
| def publish_assessment(id) do | ||
| simple_update( | ||
| Assessment, | ||
| id, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|> join(:left, [a], q in assoc(a, :questions))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, will merge once Travis passes
* Refactor assessments context to execute single queries where possible * Refactor some assessments tests * Wups * Respond to comments * Remove some unused functions * Fix `create_question_for_assessment` * Minor fixes
Fixes #98
Fixes #58