Skip to content

Commit

Permalink
special unknown operator
Browse files Browse the repository at this point in the history
  • Loading branch information
tsubik committed Dec 5, 2023
1 parent 0607e31 commit 1e21df6
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 9 deletions.
8 changes: 5 additions & 3 deletions app/admin/operator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

config.clear_action_items!

action_item :toggle_active, only: [:show] do
action_item :toggle_active, only: [:show], if: -> { !resource.special_unknown? } do
if resource.is_active
link_to I18n.t("shared.deactivate"), deactivate_admin_producer_path(resource),
method: :put, data: {confirm: I18n.t("active_admin.operator_page.confirm_deactivate", name: resource.name)}
Expand All @@ -34,7 +34,7 @@
end
end

action_item :edit, only: [:show] do
action_item :edit, only: [:show], if: -> { !resource.special_unknown? } do
link_to I18n.t("active_admin.operator_page.edit"), edit_admin_producer_path(resource)
end

Expand Down Expand Up @@ -97,6 +97,8 @@
end
column(:created_at) { |o| o.created_at.to_date }
column(I18n.t("active_admin.shared.actions")) do |operator|
next if operator.special_unknown?

if operator.is_active
link_to I18n.t("shared.deactivate"), deactivate_admin_producer_path(operator),
method: :put, data: {confirm: I18n.t("active_admin.operator_page.confirm_deactivate", name: operator.name)}
Expand All @@ -108,7 +110,7 @@

actions defaults: false do |operator|
item I18n.t("active_admin.view"), admin_producer_path(operator)
item I18n.t("active_admin.edit"), edit_admin_producer_path(operator)
item I18n.t("active_admin.edit"), edit_admin_producer_path(operator) unless operator.special_unknown?
if operator.can_hard_delete?
item I18n.t("active_admin.delete"), admin_producer_path(operator), method: :delete, data: {confirm: I18n.t("active_admin.operator_page.confirm_delete")}
end
Expand Down
5 changes: 5 additions & 0 deletions app/models/observation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class Observation < ApplicationRecord

with_options if: :operator? do
validate :validate_governments_absences
validate :validate_known_operator, if: -> { operator? && (validation_status != "Created") }
end

with_options if: :government? do
Expand Down Expand Up @@ -288,6 +289,10 @@ def validate_governments_absences
errors.add(:governments, "Should have no governments with 'operator' type")
end

def validate_known_operator
errors.add(:operator, "can't be blank or unknown") if operator.blank? || operator.special_unknown?
end

# Validates the statuses transitions.
# This method makes sure the state machine of observations' statuses is enforced.
def status_changes
Expand Down
15 changes: 13 additions & 2 deletions app/models/operator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class Operator < ApplicationRecord
validates :name, uniqueness: {case_sensitive: false}
validates :website, url: true, if: lambda { |x| x.website.present? }
validates :operator_type, inclusion: {in: TYPES, message: "can't be %{value}. Valid values are: #{TYPES.join(", ")} "}
validates :country, presence: true, on: :create
validates :country, presence: true, on: :create, unless: :special_unknown?

scope :by_name_asc, -> { order(name: :asc) }

Expand Down Expand Up @@ -135,13 +135,24 @@ def can_hard_delete?
all_observations.with_deleted.none? &&
relevant_observations.with_deleted.none? &&
users.none? &&
operator_documents.with_deleted.none?
operator_documents.with_deleted.none? &&
!special_unknown?
end

def set_slug
self.slug = name.parameterize
end

def name
return I18n.t("filters.unknown") if special_unknown?

super
end

def special_unknown?
slug == "unknown"
end

private

# Saves the fmus of the operator to update the operator's name.
Expand Down
4 changes: 0 additions & 4 deletions app/resources/v1/operator_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ def set_active
@model.is_active = false
end

def name
I18n.with_locale(:en) { @model.name }
end

filter :certification, apply: ->(records, value, _options) {
values = value.select { |c| %w[fsc pefc olb pafc fsc_cw tlv ls].include? c }
return records unless values.any?
Expand Down
1 change: 1 addition & 0 deletions db/fixtures/06_operators.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Rake::Task["import:operators"].invoke unless Operator.any?
Operator.find_or_create_by!(name: "Unknown", operator_type: "Unknown", slug: "unknown")
9 changes: 9 additions & 0 deletions db/migrate/20231115113432_add_unknown_operator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddUnknownOperator < ActiveRecord::Migration[7.0]
def up
Operator.find_or_create_by!(name: "Unknown", operator_type: "Unknown", slug: "unknown")
end

def down
Operator.where(slug: "unknown").destroy_all
end
end
7 changes: 7 additions & 0 deletions spec/factories/operators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
operator_type { "Logging company" }
is_active { true }

factory :unknown_operator do
country { nil }
name { "Unknown" }
slug { "unknown" }
operator_type { "Unknown" }
end

trait :with_sawmills do
after(:create) do |op|
create_list(:sawmill, 2, operator: op)
Expand Down
24 changes: 24 additions & 0 deletions spec/models/observation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@
describe "#status_changes" do
let(:country) { create(:country) }
let(:status) { "Created" }
let(:operator) { create(:operator, country: country) }
let(:observation) {
build :observation,
country: country,
operator: operator,
validation_status: status
}
subject {
Expand Down Expand Up @@ -165,6 +167,28 @@
it { is_expected.to_not be_valid }
end
end

describe "for all" do
context "with unknown operator" do
let(:operator) { create(:unknown_operator) }
before { observation.save }
subject { observation }

context "when creating an observation with status `Created`" do
it { is_expected.to be_valid }
it { is_expected.to be_persisted }
end

context "when moving to 'Ready for QC'" do
it "should not be valid" do
observation.validation_status = "Ready for QC"
observation.operator = Operator.find_by(slug: "unknown")
expect(observation.save).to be false
expect(observation.errors[:operator]).to eql ["can't be blank or unknown"]
end
end
end
end
end

describe "#active_government" do
Expand Down

0 comments on commit 1e21df6

Please sign in to comment.