Skip to content

Instantly share code, notes, and snippets.

{
assignment: {
id: 1,
title: 'Read Programming Phoenix',
description: 'Learn about Elixir and create a Phoenix app in the process!',
started_at: null,
due_at: null,
creator_id: 1,
created_at: '2018–02–13T20:32:29.178–05:00',
updated_at: '2018–02–13T20:32:29.178–05:00',
// defined in actions.js
export const modifyActiveAssignment = (attribute, value) => ({
type: MODIFY_ACTIVE_ASSIGNMENT,
payload: {
attribute,
value
}
})
// defined in the React component
// editorReducer.js
case MODIFY_ACTIVE_ASSIGNMENT:
const { attribute, value } = payload
return { …state, active: { …active, [attribute]: value } }
case ADD_TASK:
case SHOW_TASK:
case UPDATE_TASK:
case REMOVE_TASK:
const tasks = tasksReducer(active.tasks, action)
return { …state, active: { …active, tasks } }
module Assignments
# This code lives in a module that we use to namespace our application as part of domain-driven design. You might call this an aggregate root.
def self.update_assignment(assignment_id, title:, description:, collaborator_ids:, assignee_ids:, tasks:, due_in:, due_in_units:)
assignment = Assignment
.includes(tasks: :rubric_items)
.find(assignment_id)
assignment.update(
title: title,
description: description,
class Assignment < ActiveRecord:Base
# code omitted
def set_tasks_from_list(task_attributes)
self.tasks = self.tasks.new(task_attributes)
end
def set_collaborators_from_list(collaborator_ids)
collaborators = collaborator_ids.map { |id| { collaborator_id: id } }
self.collaborations = self.collaborations.new(collaborators)
class PublishableAssignmentValidation
attr_reader :assignment
def initialize(assignment)
@assignment = assignment
end
def valid?
validate
assignment.errors.none?

Chapter 5 Separating Responsibilities

Code: https://github.com/talum/99bottles/commit/3120ccc3aa7d7227bc6fb1513bc770e4652aeb5d

Summary: "This chapter explores what it means to model abstractions and rely on messages; it considers the consequences of mutation and the perils of premature performance optimization." Sandi Metz, Katrina Owen. 99 Bottles of OOP (Kindle Locations 5107-5108).

On refactoring and chapter 4 changes: "The truth about refactoring is that it sometimes makes things worse, in which case your efforts serve gallantly to disprove an idea." Sandi Metz, Katrina Owen. 99 Bottles of OOP (Kindle Locations 5096-5097).

5.1 Code Smell

@talum
talum / sti_1a.rb
Last active November 25, 2018 19:07
STI examples
def completed_labs_count(student, canonical_repo_ids)
if $rollout.active?(:remove_content)
student.canonical_progresses.complete.
joins(github_repository: :canonical_material).
where(github_repository_id: canonical_repo_ids).
where(kind: CourseWork::CanonicalMaterial::LAB_KINDS).
uniq(:id).
count
else
student.canonical_progresses.complete.
@talum
talum / sti_1b.rb
Last active November 25, 2018 19:09
# some other caller
def set_projects_progress_status!
project_repo_ids = AssignedRepo.team_projects_for(batch_id: batch.id, track_id: track.id).pluck(:canonical_id)
end
def set_projects_progress_status!
project_repo_ids = AssignedRepo.team_projects_for(batch_id: batch.id, track_id: track.id).canonical_github_repository_ids
end
# Assigned Repo
class Api::V1::SearchesController < ApiController
def new
gh_id_mappings = if $rollout.active?(:remove_content)
GithubRepository.track_data_for_github_repos(repos.pluck(:id), current_user.active_track_id)
else
GithubRepository.github_id_mappings_for_global_search(current_user)
end
# omitted
end