starting on infinite scrolling of select2 inside members form. member itself might be working, roles are definitely not

Conflicts:

	app/controllers/members_controller.rb
	app/models/principal.rb

Conflicts:

	app/controllers/members_controller.rb

removes deprecated test for autocompletion

Conflicts:

	Gemfile
	app/views/members/_new_member_form.rhtml
	public/javascripts/members_select_boxes.js

Conflicts:
	Gemfile

Conflicts:
	Gemfile
	test/functional/members_controller_test.rb
pull/82/head
jwollert 12 years ago
parent b8aaa2bd64
commit 26bff02cc7
  1. 15
      app/controllers/members_controller.rb
  2. 10
      app/models/principal.rb
  3. 32
      app/views/members/_new_member_form.rhtml
  4. 68
      public/javascripts/members_select_boxes.js
  5. 9
      test/functional/members_controller_test.rb

@ -86,8 +86,19 @@ class MembersController < ApplicationController
end
def autocomplete_for_member
roles = Role.find_all_givable
available_principals = @project.possible_members(params[:q], 100)
size = params[:page_limit].to_i
principals = []
page = params[:page].to_i
@principals = Principal.paginated_search(params[:q], page, { :page_limit => size })
# we always get all the items on a page, so just check if we just got the last
@more = @principals.total_pages > page
@total = @principals.total_entries
respond_to do |format|
format.json { render :layout => false }
format.html { render :layout => false }
end
render :partial => 'members/autocomplete_for_member', :locals => { :available_principals => available_principals, :roles => roles }
end

@ -43,6 +43,16 @@ class Principal < ActiveRecord::Base
to_s
end
def self.possible_members(criteria, limit)
Principal.active_or_registered.like(criteria).find(:all, :limit => limit)
end
def self.paginated_search(search, page, options = {})
limit = options.fetch(:page_limit) || 10
registered_scope = Principal.active_or_registered.like(search).scope(:find)
paginate({ :per_page => limit, :page => page }.merge(registered_scope))
end
def <=>(principal)
if self.class.name == principal.class.name
self.to_s.downcase <=> principal.to_s.downcase

@ -0,0 +1,32 @@
<%= javascript_include_tag "members_select_boxes.js" %>
<% remote_form_for(:member, :url => {:controller => 'members', :action => 'new', :id => project}, :method => :post, :html => {:id => "members_add_form"}) do |f| %>
<fieldset>
<legend><%= l(:label_member_new) %></legend>
<p>
<label>
<% options = principals.collect { |obj| [obj.name, obj.id] } %>
<%= select_tag "member[user_ids][]", options_for_select(options),
:title => l(:label_principal_search),
:multiple => true,
:'data-placeholder' => l(:label_principal_search),
:'data-ajaxURL' => url_for(:controller => "members", :action => "autocomplete_for_member"),
:'data-projectId' => project.id,
:class => "select2-select" %>
</label>
</p>
<p>
<label>
<% options = roles.collect { |obj| [obj.name, obj.id] } %>
<%= select_tag 'member[role_ids][]', options_for_select(options),
:title => l(:label_role_plural),
:multiple => true,
:'data-placeholder' => l(:label_role_plural),
:'data-ajaxURL' => url_for(:controller => "members", :action => "autocomplete_for_roles"),
:'data-projectId' => project.id,
:class => "select2-select" %>
</label>
</p>
<p><%= submit_tag l(:button_add), :id => 'member-add-submit' %></p>
</fieldset>
<% end %>

@ -0,0 +1,68 @@
jQuery(document).ready(function($) {
var load_cb, memberstab, update_cb;
init_members_cb = function () {
$("#members_add_form select.select2-select").each(function (ix, elem){
if (!$.isEmptyObject(elem.siblings('div.select2-select.select2-container'))) {
setTimeout (function () {
var attributes, allowed, currentName, fakeInput;
attributes = {}
allowed = ["title", "placeholder"];
for(var i = 0; i < $(elem).get(0).attributes.length; i++) {
currentName = $(elem).get(0).attributes[i].name;
if(currentName.indexOf("data-") == 0 || $.inArray(currentName, allowed)); //only ones starting with data-
attributes[currentName] = $(elem).attr(currentName);
}
fakeInput = $(elem).after("<input type='hidden'></input>").siblings(":input:first");
fakeInput.attr(attributes);
$(fakeInput).select2({
minimumInputLength: 1,
ajax: {
url: $(fakeInput).attr("data-ajaxURL"),
dataType: 'json',
quietMillis: 100,
contentType: "application/json",
data: function (term, page) {
return {
q: term, //search term
page_limit: 10, // page size
page: page, // page number
id: fakeInput.attr("data-projectId") // current project id
};
},
results: function (data, page) {
// notice we return the value of more so Select2 knows if more results can be loaded
active_principals = []
data.results.principals.each(function (e) {
if (e.active === true) {
active_principals.push(e);
}
});
return {'results': active_principals, 'more': data.results.more};
}
},
formatResult: formatPrincipal, // omitted for brevity, see the source of this page
formatSelection: formatPrincipalSelection
});
// $(elem).hide();
}, 0);
}
});
}
memberstab = $('#tab-members').first();
if ((memberstab != null) && (memberstab.hasClass("selected"))) {
init_members_cb();
} else {
memberstab.click(init_members_cb);
}
formatPrincipal = function (principal) {
var markup = "<span class='select2-match' data-value='" + principal.id + "'>" + principal.name + "</span>";
return markup;
}
formatPrincipalSelection = function (principal) {
return principal.name;
}
});

@ -94,13 +94,4 @@ class MembersControllerTest < ActionController::TestCase
assert_redirected_to '/projects/ecookbook/settings/members'
assert !User.find(3).member_of?(Project.find(1))
end
def test_autocomplete
get :autocomplete_for_member, :id => 1, :q => 'mis'
assert_response :success
assert_template 'autocomplete'
assert_tag :label, :content => /User Misc/,
:after => { :tag => 'input', :attributes => { :name => 'member[user_ids][]', :value => '8' } }
end
end

Loading…
Cancel
Save