Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more options to lenght validator #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ We're assuming here you want a Post model with some multilang attributes, as out
or

class Post < ActiveRecord::Base
multilang :title, :description, :required => true, :length => 100
multilang :title, :description,
:required => true,
:length => { :maximum=>100, :minimum=>2 }
end

The multilang translations are stored in the same model attributes (eg. title):
Expand Down Expand Up @@ -116,7 +118,10 @@ The value from "any" method returns value for I18n.current_locale or, if value i

Multilang has some validation features:

multilang :title, :length => 100 #define maximal length validator
multilang :title, :length => 100 #define maximum length validator
multilang :title, :length => {:minimum=> 2,:maximum=>100} #define min/max length validator
multilang :title, :length => {:within=>2..100} #define min/max length validator
multilang :title, :length => {:is=> 10} #length is exactly 10
multilang :title, :required => true #define requirement validator for all available_locales
multilang :title, :required => 1 #define requirement validator for 1 locale
multilang :title, :required => [:en, :es] #define requirement validator for specific locales
Expand Down
3 changes: 2 additions & 1 deletion lib/multilang-hstore/active_record_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ def multilang(*args)
#attribute maximal length validator
if options[:length]
module_eval do
validates_length_of "#{attribute}_#{locale}", :maximum => options[:length]
validates_length_of "#{attribute}_#{locale}",
( options[:length].is_a?(Hash) ? options[:length] : {:maximum=>options[:length]} )
end
end

Expand Down