Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Uses length validation to hide add link when you reach maximum # of nested objects #46

Open
wants to merge 1 commit into from

7 participants

Sebastian Edwards Ryan Bates mexxer godalphul Arthur Klepchukov Michael Glass amnesia7
Sebastian Edwards

Hides the add link when reaches maximum number of nested objects as defined in length validation. Ie.

class Project < ActiveRecord::Base
  has_many :tasks
  validates :tasks, :length => { :in => 1..3 }
end

Adds the attribute 'data-maximum' to the link and checks this against the number of visible nested entries on both add and remove and also on page load.

Have changed jQuery template only as am not familiar with Prototype. Probably should be altered to bind to the 'nested:fieldAdded' and 'nested:fieldRemoved' triggers.

Hope this is useful.

Ryan Bates
Owner

Cool idea. Anyone want to add this to the prototype javascript file?

mexxer

Works perfectly!

What do I have to change if I want to hide the remove links for a data minimum, though?

godalphul

Has this been added to the most recent commit?

amnesia7

I assume the equivalent for minimum would be more or less the same code but reversed for minimum rather than maximum.

Arthur Klepchukov

Sad to see this wither on the vine. Would be very helpful!

Michael Glass

I migrated a lot of this work forward. Will be ready by EOW. Backwards compatible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 26, 2011
  1. Sebastian Edwards

    Hides the add link when object reaches maximum number of nested objec…

    SebastianEdwards authored
    …ts as defined in length validation.
This page is out of date. Refresh to see the latest.
12 lib/generators/nested_form/templates/jquery_nested_form.js
View
@@ -35,6 +35,7 @@ jQuery(function($) {
content = content.replace(regexp, "new_" + new_id);
var field = $(content).insertBefore(this);
+ check_maximum();
$(this).closest("form").trigger({type: 'nested:fieldAdded', field: field});
return false;
});
@@ -45,7 +46,18 @@ jQuery(function($) {
hidden_field.value = '1';
}
$(this).closest('.fields').hide();
+ check_maximum();
$(this).closest("form").trigger('nested:fieldRemoved');
return false;
});
+
+ function check_maximum() {
+ $('form a.add_nested_fields').each(function(){
+ var assoc = $(this).attr('data-association'); // Name of child
+ var maximum = $(this).attr('data-maximum'); // Maximum # of children
+ $('.' + assoc+':visible').length >= maximum ? $(this).hide() : $(this).show();
+ });
+ }
+
+ check_maximum();
});
10 lib/nested_form/builder.rb
View
@@ -16,6 +16,14 @@ def link_to_add(*args, &block)
association = args.pop
options[:class] = [options[:class], "add_nested_fields"].compact.join(" ")
options["data-association"] = association
+ validators = self.object.class.validators_on(association)
+ unless validators.empty?
+ length_validator = validators.select {|item| item.class == ActiveModel::Validations::LengthValidator}.first
+ unless length_validator.nil?
+ maximum = length_validator.options[:maximum]
+ options["data-maximum"] = maximum
+ end
+ end
args << (options.delete(:href) || "javascript:void(0)")
args << options
@fields ||= {}
@@ -57,7 +65,7 @@ def fields_for_with_nested_attributes(association_name, args, block)
end
def fields_for_nested_model(name, object, options, block)
- output = '<div class="fields">'.html_safe
+ output = "<div class=\"fields #{object.class.name.underscore.pluralize}\">".html_safe
output << super
output.safe_concat('</div>')
output
Something went wrong with that request. Please try again.