Skip to content

Commit

Permalink
re-add required_attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Fuchs committed Jul 31, 2010
1 parent ba89640 commit a6e63a8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
12 changes: 6 additions & 6 deletions lib/globalize/active_record/class_methods.rb
Expand Up @@ -13,7 +13,7 @@ def with_translations(*locales)
end

def with_required_attributes
required_attributes.inject(scoped) do |scope, name|
required_translated_attributes.inject(scoped) do |scope, name|
scope.where("#{translated_column_name(name)} IS NOT NULL")
end
end
Expand All @@ -27,11 +27,11 @@ def with_translated_attribute(name, value, locales = nil)
end

def required_attributes
# TODO
# @required_attributes ||= reflect_on_all_validations.select do |validation|
# validation.macro == :validates_presence_of && translated_attribute_names.include?(validation.name)
# end.map(&:name)
[]
validators.map { |v| v.attributes if v.is_a?(ActiveModel::Validations::PresenceValidator) }.flatten
end

def required_translated_attributes
translated_attribute_names & required_attributes
end

def translation_class
Expand Down
18 changes: 7 additions & 11 deletions test/globalize3/translation_class_test.rb
Expand Up @@ -41,17 +41,13 @@ class Baz < ActiveRecord::Base
assert PostTranslation.new.respond_to?(:existing_method)
end

# TODO
#
# test "required_attributes don't include non-translated attributes" do
# validations = [
# stub(:name => :name, :macro => :validates_presence_of),
# stub(:name => :email, :macro => :validates_presence_of)
# ]
# User.expects(:reflect_on_all_validations => validations)
# assert_equal [:name], User.required_attributes
# end

test "required_attributes returns required attributes (i.e. validates_presence_of)" do
assert_equal [:name, :email], User.required_attributes
end

test "required_translated_attributes do not include non-translated attributes" do
assert_equal [:name], User.required_translated_attributes
end
end


0 comments on commit a6e63a8

Please sign in to comment.