Class: AssignmentsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/assignments_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /assignments



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/assignments_controller.rb', line 34

def create
  resource_ids = assignment_params.values_at(:resource_ids, :resource_id).tap(&:compact!)
  resources = current_organization.resources.where(id: resource_ids)

  assignments = resources.map do |resource|
    Assignment.find_or_create_by!(resource: resource, user: assigned_user)
  end

  logger.info "Created '#{assignments}'"
  flash[:notice] = "Created #{assignments.count} #{'assignment'.pluralize(assignments.count)}"

  redirect_back fallback_location: [current_organization]
end

#destroyObject

DELETE /assignments/1



49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/assignments_controller.rb', line 49

def destroy
  if assignment.destroy
    logger.info "Deleted #{assignment}"
    redirect_to organization_assignments_url(current_organization), notice: 'Assignment was successfully destroyed.'
  else
    logger.warn "Unable to delete #{assignment}: '#{assignment.error_sentence}'"
    flash[:error] = "We were unable to delete the assignment"
    redirect_to :back
  end
end

#indexObject

GET /assignments



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/assignments_controller.rb', line 9

def index
  assignments = current_organization.assignments.to_a

  assignments.sort_by! do |a|
    [a.user_last_name, a.user_email].tap(&:compact!).first
  end

  memberships = current_organization.memberships.index_by(&:user_id)

  self.assigned_users = assignments.each_with_object(Hash.new(0)) do |assignment, hash|
    membership = memberships[assignment.user_id]
    hash[membership] = hash[membership] + 1 if membership.present?
    hash
  end
end

#newObject

GET /assignments/new



30
31
# File 'app/controllers/assignments_controller.rb', line 30

def new
end

#showObject

GET /assignments/1



26
27
# File 'app/controllers/assignments_controller.rb', line 26

def show
end