Skip to content

Commit

Permalink
create experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
timcowlishaw committed Jan 31, 2025
1 parent d2c289f commit 82f7760
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 6 deletions.
32 changes: 32 additions & 0 deletions app/controllers/ui/experiments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,38 @@ def update
end
end

def new
unless current_user
flash[:alert] = I18n.t(:create_experiment_forbidden)
redirect_to login_path
return
end
@title = I18n.t(:new_experiment_title)
add_breadcrumbs(
[I18n.t(:show_user_title, owner: helpers.possessive(current_user, current_user, capitalize: true)), ui_user_path(current_user)],
[@title, new_ui_experiment_path]
)
@experiment = Experiment.new(owner: current_user)
end

def create
unless current_user
flash[:alert] = I18n.t(:create_experiment_forbidden)
redirect_to login_path
return
end
@experiment = Experiment.new(experiment_params)
@experiment.owner = current_user
if @experiment.valid?
@experiment.save
flash[:success] = I18n.t(:new_experiment_success)
redirect_to ui_experiment_path(@experiment.id)
else
flash[:alert] = I18n.t(:new_experiment_failure)
render :new, status: :unprocessable_entity
end
end

private

def find_experiment!
Expand Down
4 changes: 4 additions & 0 deletions app/policies/user_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ def show_secrets?
def register_device?
user == record
end

def create_experiment?
user == record
end
end
5 changes: 5 additions & 0 deletions app/views/ui/experiments/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%= bootstrap_form_for @experiment, url: ui_experiments_path do |form| %> <%= render partial: "fields", locals: { experiment: @experiment, form: form, include_hardware_info: true } %>
<div class="mt-5">
<%= form.primary t(:new_experiment_submit), class: "btn btn-primary w-100 w-md-auto" %>
</div>
<% end %>
9 changes: 6 additions & 3 deletions app/views/ui/users/_actions.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<p class="mb-0">
<% if authorize? user, :update? %>
<%= link_to(t(:show_user_edit_cta, owner: possessive(user, current_user)), edit_ui_user_path(user), class: "btn btn-dark me-md-2 mb-3 mb-lg-0 w-100 w-md-auto") %>
<%= link_to(t(:show_user_edit_cta, owner: possessive(user, current_user)), edit_ui_user_path(user), class: "btn btn-dark me-md-2 mb-3 w-100 w-md-auto") %>
<% end %>
<% if authorize? user, :show_secrets? %>
<%= link_to(t(:show_user_secrets_cta, owner: possessive(user, current_user)), secrets_ui_user_path(user), class: "btn btn-dark me-md-2 mb-3 mb-lg-0 w-100 w-md-auto") %>
<%= link_to(t(:show_user_secrets_cta, owner: possessive(user, current_user)), secrets_ui_user_path(user), class: "btn btn-dark me-md-2 mb-3 w-100 w-md-auto") %>
<% end %>
<% if authorize? user, :register_device? %>
<%= link_to(t(:show_user_register_cta), register_ui_devices_path, class: "btn btn-dark me-md-2 mb-3 mb-lg-0 w-100 w-md-auto") %>
<%= link_to(t(:show_user_register_cta), register_ui_devices_path, class: "btn btn-dark me-md-2 mb-3 w-100 w-md-auto") %>
<% end %>
<% if authorize? user, :create_experiment? %>
<%= link_to(t(:show_user_new_experiment_cta), new_ui_experiment_path, class: "btn btn-dark me-md-2 mb-3 w-100 w-md-auto") %>
<% end %>
<% if current_user == user %>
<%= link_to(t(:show_user_log_out_cta), logout_path, class: "btn btn-dark w-100 mb-3 mb-lg-0 w-md-auto") %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/ui/users/_meta.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p class="mb-0 small">
<%= image_tag("user_details_icon_light.svg", class: "pe-1") %>
<span class="pe-1"><%= show_svg("user_details_icon_light.svg") %></span>
<%= user.role.capitalize %>
</p>
<% if user.city || user.country %>
Expand Down
6 changes: 5 additions & 1 deletion config/locales/controllers/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ en:
register_device_title: "Register a new kit"
new_device_title: "Register a new legacy kit"
new_device_success: "The kit has been registered!"
new_device_failure: "Some errors prevented us from registering your device. Please check below and try again!"
new_device_failure: "Some errors prevented us from registering your kit. Please check below and try again!"
upload_device_title: "Data upload"
upload_device_forbidden: "You are not allowed to upload data for that kit!"
upload_device_success: "Your data has been uploaded succesfully!"
Expand All @@ -52,5 +52,9 @@ en:
edit_experiment_forbidden: "You are not allowed to edit settings for that experiment!"
update_experiment_success: "The experiment's settings have been updated!"
update_experiment_failure: "Some errors prevented us from updating the experiment's settings. Please check below and try again!"
create_experiment_forbidden: "You need to be logged in in order to create an experiment!"
new_experiment_title: "Create an experiment"
new_experiment_success: "Your experiment has been created!"
new_experiment_failure: "Some errors prevented us from registering your experiment. Please check below and try again!"


1 change: 1 addition & 0 deletions config/locales/views/experiments/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ en:
experiment_form_devices_subhead: "Kits"
experiment_form_search_devices_label: "Search for and add a kit"
experiment_form_is_test_label: "Mark as a test experiment"
new_experiment_submit: "Create"
1 change: 1 addition & 0 deletions config/locales/views/users/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ en:
show_user_edit_cta: "Edit %{owner} profile"
show_user_secrets_cta: "Show %{owner} API keys"
show_user_register_cta: "Register a new kit"
show_user_new_experiment_cta: "Create an experiment"
show_user_log_out_cta: "Sign out"
show_user_devices_heading: "Kits"
show_user_no_devices_message: "%{username} has no kits yet!"
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/ui/devices_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@
end

context "when a user is logged in" do
it "displays the register device page" do
it "displays the new device page" do
get :new, session: { user_id: user.try(:id) }
expect(response).to have_http_status(:success)
expect(assigns[:device]).to be_present
Expand Down
73 changes: 73 additions & 0 deletions spec/controllers/ui/experiments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,77 @@
end
end
end

describe "new" do
context "when no user is logged in" do
let(:user) { nil }

it "redirects to the login page" do
get :new, session: { user_id: user.try(:id) }
expect(response).to redirect_to(login_path)
expect(flash[:alert]).to be_present
end
end

context "when a user is logged in" do
it "displays the new experiment page" do
get :new, session: { user_id: user.try(:id) }
expect(response).to have_http_status(:success)
expect(assigns[:experiment]).to be_present
expect(response).to render_template(:new)
end
end
end

describe "create" do

let(:device) {
create(:device)
}

let(:experiment_params) {
{ name: "A experiment", description: "A experiment description", device_ids: [device.id] }
}

context "when no user is logged in" do
let(:user) { nil }

it "redirects to the login page" do
post :create, params: { experiment: experiment_params }, session: { user_id: user.try(:id) }
expect(response).to redirect_to(login_path)
expect(flash[:alert]).to be_present
end
end

context "when a user is logged in" do
let(:user) { create(:user) }

context "when the parameters provided are valid" do
it "creates a experiment and redirects to the experiment page" do
expect_any_instance_of(Experiment).to receive(:save).and_call_original
post :create, params: { experiment: experiment_params }, session: { user_id: user.try(:id) }
expect(response).to redirect_to(ui_experiment_path(Experiment.last.id))
expect(flash[:success]).to be_present
expect(Experiment.last.name).to eq(experiment_params[:name])
expect(Experiment.last.description).to eq(experiment_params[:description])
expect(Experiment.last.owner).to eq(user)
expect(Experiment.last.devices).to include(device)
end
end

context "when the parameters provided are invalid" do
let(:experiment_params) {
{ name: nil, description: "A experiment description" }
}

it "does not create a experiment, and rerenders the new experiment page" do
expect_any_instance_of(Experiment).not_to receive(:save)
post :create, params: { experiment: experiment_params }, session: { user_id: user.try(:id) }
expect(response).to have_http_status(:unprocessable_entity)
expect(response).to render_template(:new)
expect(flash[:alert]).to be_present
end
end
end
end
end
17 changes: 17 additions & 0 deletions spec/features/experiment_management_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,21 @@
expect(page).to have_current_path(ui_experiment_path(experiment.id))
expect(page).to have_content("new experiment name")
end

scenario "User creates an experiment" do
password = "password123"
username = "username"
user = create(:user, username: username, password: password, password_confirmation: password)
visit "/login"
fill_in "Username or email", with: user.email
fill_in "Password", with: password
click_on "Sign into your account"
expect(page).to have_current_path(ui_user_path(user.username))
click_on "Create an experiment"
expect(page).to have_current_path(new_ui_experiment_path)
fill_in "Name", with: "new experiment name"
click_on "Create"
expect(page).to have_current_path(ui_experiment_path(Experiment.last.id))
expect(page).to have_content("new experiment name")
end
end

0 comments on commit 82f7760

Please sign in to comment.