Do not consider attributes using the :unless option as required

Remove some of the factories introduced in commit 66334b5 as now we do
not need them anymore.

Co-Authored-By: Javi Martín <35156+javierm@users.noreply.github.com>
This commit is contained in:
Senén Rodero Rodríguez
2021-11-05 11:53:46 +01:00
parent 21170d0f24
commit 30afb64bac
7 changed files with 30 additions and 13 deletions

View File

@@ -32,14 +32,19 @@ module Globalizable
private
def required_attribute?(attribute)
presence_validators = [ActiveModel::Validations::PresenceValidator,
ActiveRecord::Validations::PresenceValidator]
attribute_validators(attribute).any? { |validator| presence_validators.include? validator }
self.class.validators_on(attribute).any? do |validator|
validator.kind == :presence && !conditional_validator?(validator)
end
end
def attribute_validators(attribute)
self.class.validators_on(attribute).map(&:class)
def conditional_validator?(validator)
return false unless validator.options[:unless]
if validator.options[:unless].to_proc.arity.zero?
instance_exec(&validator.options[:unless])
else
validator.options[:unless].to_proc.call(self)
end
end
def check_translations_number

View File

@@ -86,8 +86,6 @@ FactoryBot.define do
evaluator.voters.each { |voter| create(:vote, votable: proposal, voter: voter) }
evaluator.followers.each { |follower| create(:follow, followable: proposal, user: follower) }
end
factory :retired_proposal, traits: [:retired]
end
factory :proposal_notification do

View File

@@ -13,9 +13,6 @@ shared_examples_for "globalizable" do |factory_name|
let(:attribute) { required_fields.sample || fields.sample }
before do
if factory_name == :budget || factory_name == :budget_phase
record.main_link_url = "https://consulproject.org"
end
record.update!(attribute => "In English")
I18n.with_locale(:es) do

View File

@@ -1,6 +1,7 @@
require "rails_helper"
describe Milestone do
it_behaves_like "globalizable", :milestone
it_behaves_like "globalizable", :milestone_with_description
describe "Validations" do

View File

@@ -3,6 +3,7 @@ require "rails_helper"
describe ProgressBar do
let(:progress_bar) { build(:progress_bar) }
it_behaves_like "globalizable", :progress_bar
it_behaves_like "globalizable", :secondary_progress_bar
it "is valid" do

View File

@@ -7,7 +7,7 @@ describe Proposal do
it_behaves_like "has_public_author"
it_behaves_like "notifiable"
it_behaves_like "map validations"
it_behaves_like "globalizable", :retired_proposal
it_behaves_like "globalizable", :proposal
it_behaves_like "sanitizable"
it_behaves_like "acts as paranoid", :proposal
end

View File

@@ -355,7 +355,9 @@ describe "Admin edit translatable records", :admin do
context "Remove all translations" do
let(:translatable) { create(:milestone) }
scenario "Shows an error message" do
scenario "Shows an error message when there's a mandatory translatable field" do
translatable.update!(status: nil)
visit admin_polymorphic_path(translatable, action: :edit)
click_link "Remove language"
@@ -365,6 +367,19 @@ describe "Admin edit translatable records", :admin do
expect(page).to have_content "Is mandatory to provide one translation at least"
end
scenario "Is successful when there isn't a mandatory translatable field" do
translatable.update!(status: Milestone::Status.first)
visit admin_polymorphic_path(translatable, action: :edit)
click_link "Remove language"
click_link "Remove language"
click_button "Update milestone"
expect(page).to have_content "Milestone updated successfully"
end
end
context "Remove a translation with invalid data" do