levent/agileista

View on GitHub
app/controllers/projects_controller.rb

Summary

Maintainability
A
0 mins
Test Coverage
class ProjectsController < AbstractSecurityController
  before_filter :set_project_override, only: [:edit, :destroy, :update]
  before_filter :must_be_account_holder, only: [:edit, :destroy, :update]

  def index
    setup_projects_from_invitations
    @projects = current_person.projects
  end

  def new
    @project = Project.new
  end

  def create
    @project = current_person.projects.new(project_params)
    if @project.save
      @project.scrum_master = current_person
      flash[:notice] = "Project created"
      redirect_to project_backlog_index_path(@project)
    else
      flash.now[:error] = "Project could not be created"
      render 'new'
    end
  end

  def edit
    @hip_chat_integration = @project.hip_chat_integration || HipChatIntegration.new
    @slack_integration = @project.slack_integration || SlackIntegration.new
  end

  def update
    if @project.update_attributes(project_params)
      flash[:notice] = "Project settings saved"
      redirect_to :back
    else
      @hip_chat_integration = @project.hip_chat_integration || HipChatIntegration.new
      @slack_integration = @project.slack_integration || SlackIntegration.new
      flash.now[:error] = "Project settings couldn't be saved"
      render 'edit'
    end
  end

  def destroy
    if @project.destroy
      flash[:notice] = "Project removed"
      redirect_to projects_path
    else
      flash.now[:error] = "Project could not be removed"
      render 'edit'
    end
  end

  private

  def set_project_override
    @project = current_person.projects.find(params[:id])
  end

  def setup_projects_from_invitations
    invitations = Invitation.where(email: current_person.email)
    flash[:notice] = "You have been invited to participate in additional projects" if invitations.any?
    invitations.each do |invite|
      team_member = TeamMember.where(project_id: invite.project_id, person_id: current_person.id).first_or_create!
      invite.destroy if team_member
    end
  end

  def project_params
    params[:project].permit(:name, :iteration_length)
  end
end