Module: JcheckRails
- Extended by:
- JcheckRails
- Included in:
- JcheckRails
- Defined in:
- lib/jcheck_rails.rb,
lib/jcheck_rails/encoder.rb
Defined Under Namespace
Modules: Encoder Classes: Engine
Constant Summary collapse
- KNOW_VALIDATORS =
[:acceptance, :confirmation, :exclusion, :format, :inclusion, :length, :numericality, :presence]
Instance Method Summary collapse
- #jcheck_attribute_name(object, attribute) ⇒ Object
-
#jcheck_for(*args) ⇒ Object
This will reflect into your model and generate correct jCheck validations for you.
- #jcheck_for_object_attribute(object, attribute) ⇒ Object
Instance Method Details
#jcheck_attribute_name(object, attribute) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/jcheck_rails.rb', line 75 def jcheck_attribute_name(object, attribute) if object.class.respond_to? :human_attribute_name object.class.human_attribute_name(attribute) else attribute.to_s.humanize end end |
#jcheck_for(*args) ⇒ Object
This will reflect into your model and generate correct jCheck validations for you. In first argument you should send the current object of model, if you want to get just the validations for an given attribute, send the attribute name as second parameter. In third parameter you can send options to be used in jCheck initialization, but there some special keys that can be sent in the options. You can also send options as second parameter, in this case the attribute will be considered nil.
<%= form_for(@object) do |f| %>
...
<% end %>
<%= jcheck_for(@object) %>
Configuration options:
-
:variable
- Variable name to be used in javascript (default is: “validator”) -
:form_id
- The id of form in html to be used in jQuery selector (default is same behaviour asform_for
do to generate form id) -
:field_prefix
- Field prefix to be used into jCheck, send nil to avoid field_prefix (default is same prefix as form_for will do) -
:generate_field_names
- Define if it should generate field custom_label for jCheck (default is true) -
:only_attributes
- Filter the attributes that should be reflected (default nil) -
:exclude_attributes
- Filter the attributes that should not be reflected (default nil)
Also, any other configuration option will be sent to jCheck() initializer.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/jcheck_rails.rb', line 37 def jcheck_for(*args) = args. object = args.shift attribute = args.length > 0 ? args[0] : nil return jcheck_for_object_attribute(object, attribute) if attribute .reverse_merge!( :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, :only_attributes => nil, :exclude_attributes => nil ) only_attributes = .delete :only_attributes exclude_attributes = .delete :exclude_attributes variable = .delete :variable form_id = .delete :form_id generate_field_names = .delete :generate_field_names validations = [] 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 validations << "#{variable}.validates(#{Encoder.convert_to_javascript attribute}, #{attr_validations});" end %{<script type="text/javascript"> jQuery(function() { var #{variable} = jQuery('##{form_id}').jcheck(#{Encoder.convert_to_javascript()}); #{validations.join(" ")} #{field_names.join(" ")} }); </script>}.html_safe end |
#jcheck_for_object_attribute(object, attribute) ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/jcheck_rails.rb', line 83 def jcheck_for_object_attribute(object, attribute) validations = object.class._validators[attribute].inject([]) do |acc, validator| = (validator) acc << "#{Encoder.convert_to_javascript validator.kind}: #{Encoder.convert_to_javascript()}" if KNOW_VALIDATORS.include? validator.kind acc end "{#{validations.join(', ')}}" end |