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:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user