Skip to content

Commit

Permalink
Merge pull request #1099 from concord-consortium/misc-project-admin-c…
Browse files Browse the repository at this point in the history
…leanup

fix: Misc updates to project admin code
  • Loading branch information
dougmartin authored Nov 21, 2023
2 parents 26cbd53 + 8d4623c commit ac3b8e4
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 22 deletions.
6 changes: 2 additions & 4 deletions app/assets/javascripts/lara-typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -124689,7 +124689,7 @@ var ProjectSettingsForm = function (_a) {
delete data.project.created_at;
delete data.project.updated_at;
setProject((0, convert_keys_1.snakeToCamelCaseKeys)(data.project));
setAdmins(data.admins || []);
setAdmins(data.admins);
setProjectLoaded(true);
setPageTitle("Edit " + data.project.title);
return [2
Expand Down Expand Up @@ -124763,9 +124763,7 @@ var ProjectSettingsForm = function (_a) {
case 0:
apiUrl = id ? "/api/v1/projects/" + id : "/api/v1/projects";
projectData = (0, convert_keys_1.camelToSnakeCaseKeys)(project);
projectData.admin_ids = admins.map(function (a) {
return a.id;
});
projectData.admins = admins;
return [4
/*yield*/
, fetch(apiUrl, {
Expand Down
3 changes: 3 additions & 0 deletions app/assets/stylesheets/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,9 @@ body.admin.edit, body.admin.new {
padding: 7px;
}
}
div.bottom-links {
margin-top: 20px;
}
}

.settings-table {
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def show
# GET /admin/users/new.json
def new
@user = User.new
@projects = Project.order(:title)
@projects = Project.all
authorize! :create, User

respond_to do |format|
Expand All @@ -44,7 +44,7 @@ def new
# GET /admin/users/1/edit
def edit
@user = User.find(params[:id])
@projects = Project.order(:title)
@projects = Project.all
authorize! :update, @user
end

Expand Down
10 changes: 7 additions & 3 deletions app/controllers/api/v1/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Api::V1::ProjectsController < API::APIController

# GET /api/v1/projects
def index
@projects = Project.order(:title)
@projects = Project.all
authorize! :manage, @projects
render json: {projects: @projects}
end
Expand Down Expand Up @@ -37,8 +37,12 @@ def update
@project = Project.find(@updated_project_hash[:id]);
authorize! :update, @project

# remove any extra admin ids in the update that are not in the original, to ensure we can only remove and not add project admins
@updated_project_hash[:admin_ids] = (@updated_project_hash[:admin_ids] || []).select { |id| @project.admin_ids.include?(id.to_i) }
# extract the ids from the passed admin objects and remove any extra admins in the update that
# are not in the original, to ensure we can only remove and not add project admins
@updated_project_hash[:admin_ids] = (@updated_project_hash[:admins] || [])
.select { |admin| @project.admin_ids.include?(admin[:id].to_i) }
.map { |admin| admin[:id] }
@updated_project_hash.delete(:admins)

if @project.update_attributes(@updated_project_hash)
render json: {success: true, project: @project, admins: admin_json(@project)}, status: :ok
Expand Down
2 changes: 2 additions & 0 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Project < ActiveRecord::Base
has_many :project_admins
has_many :admins, through: :project_admins, :source => :user

default_scope order('title')

protected
def self.create_default
self.create(:title => DefaultName, :logo_lara => '', :url => 'https://concord.org/', :project_key => DefaultKey)
Expand Down
2 changes: 1 addition & 1 deletion app/models/project_admin.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class ProjectAdmin < ActiveRecord::Base
attr_accessible :user, :project, :user_id, :project_id
attr_accessible :user, :project

belongs_to :user
belongs_to :project
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/users/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
<%= render :partial => 'form', :locals => { :f => f } %>
<%- end -%>

<div style="margin-top: 20px;">
<div class='bottom-links'>
<%= link_to '‹ Back to Users List', admin_users_path %>
</div>
2 changes: 1 addition & 1 deletion app/views/admin/users/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
<%= render :partial => 'form', :locals => { :f => f } %>
<%- end -%>

<div style="margin-top: 20px;">
<div class='bottom-links'>
<%= link_to '‹ Back to Users List', admin_users_path %>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ describe("ProjectSettingsForm", () => {
const titleInput = container.querySelector("#project-title") as HTMLInputElement;
const urlInput = container.querySelector("#project-url") as HTMLInputElement;
const saveButton = container.querySelector(".save-button") as HTMLButtonElement;
const updatedProject = {...project, title: "Test Project A", url: "https://concord.org/new-path", admins: []};
fetch.mockResponse(JSON.stringify({project: updatedProject, success: true}));
const updatedProject = {...project, title: "Test Project A", url: "https://concord.org/new-path"};
fetch.mockResponse(JSON.stringify({project: updatedProject, admins: [], success: true}));
await act(async () => {
fireEvent.change(titleInput, { target: { value: "Test Project A"} });
fireEvent.change(urlInput, { target: { value: "https://concord.org/new-path"} });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const ProjectSettingsForm: React.FC<IProjectSettingsFormProps> = ({id}: I
delete data.project.created_at;
delete data.project.updated_at;
setProject(snakeToCamelCaseKeys(data.project));
setAdmins(data.admins || []);
setAdmins(data.admins);
setProjectLoaded(true);
setPageTitle(`Edit ${data.project.title}`);
};
Expand Down Expand Up @@ -124,7 +124,7 @@ export const ProjectSettingsForm: React.FC<IProjectSettingsFormProps> = ({id}: I
const handleSaveProject = async () => {
const apiUrl = id ? `/api/v1/projects/${id}` : `/api/v1/projects`;
const projectData = camelToSnakeCaseKeys(project);
projectData.admin_ids = admins.map(a => a.id);
projectData.admins = admins;

const data = await fetch(apiUrl, {
method: "POST",
Expand Down
12 changes: 6 additions & 6 deletions spec/controllers/api/v1/projects_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
describe "#update" do
it "returns a success message and a JSON string when a project is updated" do
prev_updated_at = project1.updated_at
admin_ids = project1.admins.map {|a| a.id}
xhr :post, "update", {project: {id: project1.id, title: "New Project Title", admin_ids: admin_ids}}
admins = project1.admins.map {|a| {id: a.id, email: a.email} }
xhr :post, "update", {project: {id: project1.id, title: "New Project Title", admins: admins}}
expect(response.status).to eq(200)
expect(response.content_type).to eq("application/json")
response_body = JSON.parse(response.body, symbolize_names: true)
Expand All @@ -71,7 +71,7 @@

it "allows removing project admins" do
expect(project1.admins.length).to eq(1)
xhr :post, "update", {project: {id: project1.id, title: "New Project Title", admin_ids: []}}
xhr :post, "update", {project: {id: project1.id, title: "New Project Title", admins: []}}
expect(response.status).to eq(200)
expect(response.content_type).to eq("application/json")
response_body = JSON.parse(response.body, symbolize_names: true)
Expand All @@ -80,9 +80,9 @@
end

it "filters out admins that are not already set" do
admin_ids = project1.admins.map {|a| a.id}
admin_ids.push(user2.id)
xhr :post, "update", {project: {id: project1.id, title: "New Project Title", admin_ids: admin_ids}}
admins = project1.admins.map {|a| {id: a.id, email: a.email} }
admins.push({id: user2.id, email: user2.email})
xhr :post, "update", {project: {id: project1.id, title: "New Project Title", admins: admins}}
expect(response.status).to eq(200)
expect(response.content_type).to eq("application/json")
response_body = JSON.parse(response.body, symbolize_names: true)
Expand Down

0 comments on commit ac3b8e4

Please sign in to comment.