Use vanilla Rails validations for CustomField#name

Globalize should support regular Rails validations.

Additionally `validate_name` was no longer working as expected,
possibly due to changes in `CustomField::Translation` lifecycle.

* Do not test translation implementation details in
  CustomFieldController spec.

Signed-off-by: Alex Coles <alex@alexbcoles.com>
pull/3011/head
Alex Coles 10 years ago
parent 654a77d944
commit d060e654a5
  1. 29
      app/models/custom_field.rb
  2. 25
      spec/controllers/custom_fields_controller_spec.rb
  3. 3
      spec/models/custom_field_spec.rb

@ -32,9 +32,8 @@ class CustomField < ActiveRecord::Base
has_many :custom_values, dependent: :delete_all
acts_as_list scope: 'type = \'#{self.class}\''
translates :name,
:default_value,
:possible_values
translates :name, fallbacks_for_empty_translations: false
translates :default_value, :possible_values
accepts_nested_attributes_for :translations,
allow_destroy: true,
@ -57,6 +56,8 @@ class CustomField < ActiveRecord::Base
validates_presence_of :field_format
validates :name, presence: true, length: { maximum: 30 }
validate :uniqueness_of_name_with_scope
def uniqueness_of_name_with_scope
@ -73,8 +74,6 @@ class CustomField < ActiveRecord::Base
validate :validate_default_value_in_translations
validate :validate_name
validates :min_length, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates :max_length, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates :min_length, numericality: { less_than_or_equal_to: :max_length, message: :smaller_than_or_equal_to_max_length }, unless: Proc.new { |cf| cf.max_length.blank? }
@ -117,26 +116,6 @@ class CustomField < ActiveRecord::Base
self.is_required = required_field
end
# check presence of name and check the length of name value
def validate_name
if translations.empty?
errors.add(:name, :blank) if name.nil?
else
fallback_name = translations.find { |el| !el.name.nil? }
translations.each do | translation |
if translation.name.nil? && fallback_name.nil?
errors.add(:name, :blank)
else
if (translation.name.nil? && fallback_name.name.length > 30) || (!translation.name.nil? && translation.name.length > 30)
errors.add(:name, I18n.t('activerecord.errors.messages.wrong_length', count: 30))
end
translation.name = fallback_name.name if translation.name.nil?
end
end
end
end
def possible_values_options(obj = nil)
case field_format
when 'user', 'version'

@ -156,11 +156,28 @@ describe CustomFieldsController, type: :controller do
post :create, params
end
around do |example|
old_fallbacks = Globalize.fallbacks
Globalize.fallbacks = { de: [:de, :en], en: [] }
example.run
Globalize.fallbacks = old_fallbacks
end
it { expect(response.status).to eql(302) }
it { expect(assigns(:custom_field).translations.find { |elem| elem.locale == :de }[:name]).to eq(en_name) }
it { expect(assigns(:custom_field).translations.find { |elem| elem.locale == :en }[:name]).to eq(en_name) }
it { expect(assigns(:custom_field).translations.find { |elem| elem.locale == :en }[:default_value]).to be_nil }
it { expect(assigns(:custom_field).translations.find { |elem| elem.locale == :de }[:default_value]).to eq(de_default) }
it 'sets correct values for EN' do
I18n.with_locale(:en) do
expect(assigns(:custom_field).name).to eq(en_name)
expect(assigns(:custom_field).default_value).to be_nil
end
end
it 'sets correct values for DE' do
I18n.with_locale(:de) do
expect(assigns(:custom_field).name).to eq(en_name)
expect(assigns(:custom_field).default_value).to eq 'DE Default Value'
end
end
end
end
end

@ -35,6 +35,9 @@ describe CustomField, type: :model do
let(:field2) { FactoryGirl.build :custom_field }
describe '#name' do
it { should validate_presence_of(:name) }
it { should validate_length_of(:name).is_at_most(30) }
describe 'uniqueness' do
describe 'WHEN value, locale and type are identical' do

Loading…
Cancel
Save