Skip to content
Browse files

filtering attributes that should be validated

  • Loading branch information...
1 parent af7be21 commit 151e16220959e3052639a9998207f49e6f61ece7 @wilkerlucio committed Aug 22, 2010
Showing with 34 additions and 2 deletions.
  1. +0 −1 README.textile
  2. +10 −1 lib/jcheck_rails.rb
  3. +24 −0 spec/jcheck_rails_spec.rb
View
1 README.textile
@@ -30,7 +30,6 @@ overview on @jCheck@.
h2. TODO
-* Option to filter fields that should have exposed validations
* Get messages from i18n and append automatically
h2. Change Log
View
11 lib/jcheck_rails.rb
@@ -29,6 +29,8 @@ class Engine < Rails::Engine
# * <tt>:form_id</tt> - The id of form in html to be used in jQuery selector (default is same behaviour as +form_for+ do to generate form id)
# * <tt>:field_prefix</tt> - Field prefix to be used into jCheck, send nil to avoid field_prefix (default is same prefix as form_for will do)
# * <tt>:generate_field_names</tt> - Define if it should generate field custom_label for jCheck (default is true)
+ # * <tt>:only_attributes</tt> - Filter the attributes that should be reflected (default nil)
+ # * <tt>:exclude_attributes</tt> - Filter the attributes that should not be reflected (default nil)
#
# Also, any other configuration option will be sent to jCheck() initializer.
#
@@ -43,9 +45,13 @@ def jcheck_for(*args)
:variable => "validator",
:form_id => ActionController::RecordIdentifier.dom_id(object, (object.respond_to?(:persisted?) && object.persisted? ? :edit : nil)),
:field_prefix => ActiveModel::Naming.singular(object),
- :generate_field_names => true
+ :generate_field_names => true,
+ :only_attributes => nil,
+ :exclude_attributes => nil
)
+ only_attributes = options.delete :only_attributes
+ exclude_attributes = options.delete :exclude_attributes
variable = options.delete :variable
form_id = options.delete :form_id
generate_field_names = options.delete :generate_field_names
@@ -54,6 +60,9 @@ def jcheck_for(*args)
field_names = []
object.class._validators.each do |attribute, validators|
+ next if only_attributes and !(only_attributes.include?(attribute))
+ next if exclude_attributes and exclude_attributes.include?(attribute)
+
attr_validations = jcheck_for_object_attribute(object, attribute)
field_names << "#{variable}.field(#{Encoder.convert_to_javascript attribute}).custom_label = #{Encoder.convert_to_javascript jcheck_attribute_name(object, attribute)};" if generate_field_names
View
24 spec/jcheck_rails_spec.rb
@@ -255,6 +255,30 @@ def self.human_attribute_name(attribute)
jcheck(@m).should == "<script type=\"text/javascript\"> jQuery(function() { var validator = jQuery('#new_sample_model').jcheck({'field_prefix': 'sample_model'}); validator.validates('name', {'presence': true}); validator.field('name').custom_label = 'Name'; }); </script>"
end
+ context "filtering attributes" do
+ before :all do
+ @md = mock_model do
+ attr_accessor :name, :email, :address
+
+ validates_presence_of :name, :email, :address
+ end
+
+ FilterTest = @md.class
+ end
+
+ it "should get only some attributes if using only_attributes option" do
+ output = jcheck(@md, :only_attributes => [:name, :email])
+ output.should include("validates('name'", "validates('email'")
+ output.should_not include("validates('address'")
+ end
+
+ it "should exclude some attributes if it's sent" do
+ output = jcheck(@md, :exclude_attributes => [:name, :address])
+ output.should include("validates('email'")
+ output.should_not include("validates('name'", "validates('address'")
+ end
+ end
+
it "should be able to customize form id" do
jcheck(@m, :form_id => "custom_form_id").should include("jQuery('#custom_form_id')")
end

0 comments on commit 151e162

Please sign in to comment.
Something went wrong with that request. Please try again.