Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions app/controllers/workshop_variations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ def index
end

def new
@workshop_variation = WorkshopVariation.new
if params[:workshop_variation_idea_id].present?
idea = WorkshopVariationIdea.find(params[:workshop_variation_idea_id])
@workshop_variation = WorkshopVariationFromIdeaService.new(idea, user: current_user).call
else
@workshop_variation = WorkshopVariation.new
end
authorize! @workshop_variation
set_form_variables
end
Expand Down Expand Up @@ -91,12 +96,15 @@ def set_form_variables
@workshops = workshops.order(:title)
@workshop = @workshop_variation.workshop || params[:workshop_id].present? &&
Workshop.where(id: params[:workshop_id]).last
@workshop_variation_idea = params[:workshop_variation_idea_id].present? &&
WorkshopVariationIdea.find_by(id: params[:workshop_variation_idea_id])
end

def workshop_variation_params
params.require(:workshop_variation).permit(
[ :name, :body, :published, :position,
:youtube_url, :created_by_id, :workshop_id
:youtube_url, :created_by_id, :workshop_id,
:workshop_variation_idea_id
]
)
end
Expand Down
11 changes: 11 additions & 0 deletions app/models/workshop_variation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,15 @@ def description
def title
name
end

def attach_assets_from_idea!
return unless workshop_variation_idea

workshop_variation_idea.assets.find_each do |asset|
new_asset = assets.build(type: asset.type)
new_asset.file.attach(asset.file.blob)
end

save!
end
end
2 changes: 1 addition & 1 deletion app/services/workshop_variation_from_idea_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def call

def attributes_from_idea
workshop_variation_idea.attributes.slice(
"name", "description", "youtube_url",
"name", "body", "youtube_url",
"position", "workshop_id"
).merge(
created_by_id: user.id,
Expand Down
8 changes: 5 additions & 3 deletions spec/factories/workshop_variation_ideas.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
FactoryBot.define do
factory :workshop_variation_idea do
name { "Workshop Variation Idea" }
description { "This is a variation idea description" }
body { "This is a variation idea description" }
youtube_url { "https://www.youtube.com/watch?v=example" }
inactive { true }
position { 1 }
permission_given { true }
publish_preferences { "public" }
association :workshop
association :organization
association :windows_type
association :created_by, factory: :user
association :updated_by, factory: :user
end
Expand Down
271 changes: 271 additions & 0 deletions spec/requests/workshop_variation_ideas_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
require "rails_helper"

RSpec.describe "/workshop_variation_ideas", type: :request do
let(:regular_user) { create(:user) }
let(:admin) { create(:user, super_user: true) }
let(:workshop) { create(:workshop) }
let(:organization) { create(:organization) }
let(:windows_type) { create(:windows_type) }

let(:valid_attributes) do
{
name: "Mindful Art Variation",
body: "A variation focusing on mindfulness and relaxation.",
youtube_url: "https://www.youtube.com/watch?v=example",
permission_given: true,
publish_preferences: "public",
workshop_id: workshop.id,
organization_id: organization.id,
windows_type_id: windows_type.id,
created_by_id: regular_user.id,
updated_by_id: regular_user.id
}
end

let(:invalid_attributes) do
{
name: "",
workshop_id: nil,
created_by_id: nil
}
end

before do
allow(NotificationServices::CreateNotification).to receive(:call)
end

# ============================================================
# ADMIN
# ============================================================

context "as an admin" do
before { sign_in admin }

describe "GET /index" do
it "renders successfully" do
create(:workshop_variation_idea, valid_attributes)
get workshop_variation_ideas_path
expect(response).to have_http_status(:ok)
end
end

describe "GET /show" do
it "renders successfully" do
idea = create(:workshop_variation_idea, valid_attributes)
get workshop_variation_idea_path(idea)
expect(response).to have_http_status(:ok)
end
end

describe "GET /new" do
it "renders successfully" do
get new_workshop_variation_idea_path
expect(response).to have_http_status(:ok)
end
end

describe "GET /edit" do
it "renders successfully" do
idea = create(:workshop_variation_idea, valid_attributes)
get edit_workshop_variation_idea_path(idea)
expect(response).to have_http_status(:ok)
end
end

describe "POST /create" do
context "with valid params" do
it "creates a WorkshopVariationIdea" do
expect {
post workshop_variation_ideas_path, params: { workshop_variation_idea: valid_attributes }
}.to change(WorkshopVariationIdea, :count).by(1)

expect(response).to redirect_to(workshop_variation_ideas_path)
end

it "sends a notification" do
expect(NotificationServices::CreateNotification).to receive(:call).with(
hash_including(
kind: :idea_submitted_fyi,
recipient_role: :admin
)
)

post workshop_variation_ideas_path, params: { workshop_variation_idea: valid_attributes }
end
end

context "with invalid params" do
it "does not create and returns 422" do
expect {
post workshop_variation_ideas_path, params: { workshop_variation_idea: invalid_attributes }
}.not_to change(WorkshopVariationIdea, :count)

expect(response).to have_http_status(:unprocessable_content)
end
end
end

describe "PATCH /update" do
it "updates the workshop variation idea" do
idea = create(:workshop_variation_idea, valid_attributes)

patch workshop_variation_idea_path(idea),
params: { workshop_variation_idea: { name: "Updated Name" } }

expect(idea.reload.name).to eq("Updated Name")
expect(response).to redirect_to(workshop_variation_ideas_path)
end
end

describe "DELETE /destroy" do
it "destroys the workshop variation idea" do
idea = create(:workshop_variation_idea, valid_attributes)

expect {
delete workshop_variation_idea_path(idea)
}.to change(WorkshopVariationIdea, :count).by(-1)

expect(response).to redirect_to(workshop_variation_ideas_path)
end
end
end

# ============================================================
# REGULAR USER
# ============================================================

context "as a regular user" do
before { sign_in regular_user }

describe "GET /index" do
it "redirects to root" do
get workshop_variation_ideas_path
expect(response).to redirect_to(root_path)
end
end

describe "GET /show" do
it "renders owned workshop_variation_idea successfully" do
idea = create(:workshop_variation_idea, valid_attributes) # owner == regular_user
get workshop_variation_idea_path(idea)
expect(response).to have_http_status(:ok)
end

it "redirects from another's workshop_variation_idea to root" do
idea = create(:workshop_variation_idea, valid_attributes.merge(created_by_id: admin.id))
get workshop_variation_idea_path(idea)
expect(response).to redirect_to(root_path)
end
end

describe "GET /new" do
it "renders successfully" do
get new_workshop_variation_idea_path
expect(response).to have_http_status(:ok)
end
end

describe "POST /create" do
context "with valid params" do
it "creates a WorkshopVariationIdea" do
expect {
post workshop_variation_ideas_path,
params: { workshop_variation_idea: valid_attributes }
}.to change(WorkshopVariationIdea, :count).by(1)

expect(response).to redirect_to(root_path)
end
end

context "with invalid params" do
it "does not create and returns 422" do
expect {
post workshop_variation_ideas_path,
params: { workshop_variation_idea: invalid_attributes }
}.not_to change(WorkshopVariationIdea, :count)

expect(response).to have_http_status(:unprocessable_content)
end
end
end

describe "PATCH /update" do
it "does not update with valid params" do
idea = create(:workshop_variation_idea, valid_attributes)
original_name = idea.name

patch workshop_variation_idea_path(idea),
params: { workshop_variation_idea: { name: "Updated Name" } }

expect(idea.reload.name).to eq(original_name)
expect(response).to redirect_to(root_path)
end
end

describe "DELETE /destroy" do
it "does not destroy the idea" do
idea = create(:workshop_variation_idea, valid_attributes)

expect {
delete workshop_variation_idea_path(idea)
}.not_to change(WorkshopVariationIdea, :count)

expect(response).to redirect_to(root_path)
end
end
end

# ============================================================
# GUEST
# ============================================================

context "as a guest" do
describe "GET /index" do
it "redirects to root" do
get workshop_variation_ideas_path
expect(response).to redirect_to(root_path)
end
end

describe "GET /new" do
it "redirects to root" do
get new_workshop_variation_idea_path
expect(response).to redirect_to(root_path)
end
end

describe "POST /create" do
it "does not create and redirects to root" do
expect {
post workshop_variation_ideas_path,
params: { workshop_variation_idea: valid_attributes }
}.not_to change(WorkshopVariationIdea, :count)

expect(response).to redirect_to(root_path)
end
end

describe "PATCH /update" do
it "redirects to root" do
idea = create(:workshop_variation_idea, valid_attributes)

patch workshop_variation_idea_path(idea),
params: { workshop_variation_idea: { name: "Updated" } }

expect(response).to redirect_to(root_path)
end
end

describe "DELETE /destroy" do
it "does not delete and redirects to root" do
idea = create(:workshop_variation_idea, valid_attributes)

expect {
delete workshop_variation_idea_path(idea)
}.not_to change(WorkshopVariationIdea, :count)

expect(response).to redirect_to(root_path)
end
end
end
end
Loading
Loading