Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added calculator new specs; fixed controller; #958

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 7 additions & 2 deletions app/controllers/account/calculators_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

class Account::CalculatorsController < Account::BaseController
before_action :resource, only: [:edit, :update, :destroy]
load_and_authorize_resource
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should skip "load_and_authorize_resource", because we load resources and have only user aka Admin.


def index
Expand All @@ -23,6 +22,8 @@ def new
end

def edit
@calculator = resource

collect_fields_for_form
end

Expand All @@ -37,6 +38,8 @@ def create
end

def update
@calculator = resource

if updater
redirect_to edit_account_calculator_path(slug: @calculator), notice: t("notifications.calculator_updated")
else
Expand All @@ -47,6 +50,8 @@ def update
end

def destroy
@calculator = resource

@calculator.destroy

redirect_to account_calculators_path, notice: t("notifications.calculator_deleted"), status: :see_other
Expand All @@ -59,7 +64,7 @@ def collection
end

def resource
Calculator.find(params[:slug])
collection.friendly.find(params[:slug])
end

def collect_fields_for_form
Expand Down
3 changes: 1 addition & 2 deletions spec/factories/calculators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
FactoryBot.define do
factory :calculator do
en_name { "English Calculator" }
uk_name { "Український калькуялтор" }
slug { "calculator" }
uk_name { "Український калькулятор" }
end
end
2 changes: 1 addition & 1 deletion spec/factories/fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#
FactoryBot.define do
factory :field do
kind { 0 }
kind { "number" }
en_label { "Label" }
uk_label { "Label" }
var_name { "var" }
Expand Down
51 changes: 51 additions & 0 deletions spec/requests/account/calculators_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

let!(:calculator) { create(:calculator) }

let(:user) { create(:user) }
let(:locale) { "en" }
let(:new_path) { new_account_calculator_path(locale: locale) }

describe "DELETE #destroy" do
it "destroys the calculator and redirects" do
expect do
Expand Down Expand Up @@ -40,4 +44,51 @@
end
end
end

describe "GET #new" do
subject { get new_path }

context "when the user is authorized" do
it "initializes a new Calculator object with fields and formulas" do
subject

expect(response).to have_http_status(:ok)

calculator = assigns(:calculator)
expect(calculator).to be_a_new(Calculator)

expect(calculator.fields.size).to eq(1)
expect(calculator.formulas.size).to eq(1)

expect(calculator.fields.first).to be_a_new(Field)
expect(calculator.formulas.first).to be_a_new(Formula)
end

it "renders the new template" do
subject
expect(response).to render_template(:new)
end
end

context "when the locale is different" do
let(:locale) { "uk" }

it "handles the locale correctly" do
subject
expect(I18n.locale).to eq(:uk)
expect(response).to have_http_status(:ok)
end
end

context "when the user is not logged in" do
before do
sign_out(:user)
end

it "redirects to the login page" do
subject
expect(response).to redirect_to(new_user_session_path)
end
end
end
end
Loading