Commit 1f4925d45a9f6a58806b95213b51b2c62e2e0946
Committed by
Parley
1 parent
c2f10b56
Exists in
master
and in
5 other branches
Enable user enter and exit from institutions on user profile editor
(more_inst_for_users) Signed-off-by: Arthur Del Esposte <arthurmde@gmail.com> Signed-off-by: Fabio Teixeira <fabio1079@gmail.com>
Showing
2 changed files
with
58 additions
and
18 deletions
Show diff stats
lib/mpog_software_plugin.rb
| ... | ... | @@ -237,18 +237,11 @@ class MpogSoftwarePlugin < Noosfero::Plugin |
| 237 | 237 | end |
| 238 | 238 | |
| 239 | 239 | def user_transaction |
| 240 | - old_community = Institution.find(context.profile.user.institution_id).community | |
| 241 | - | |
| 240 | + user_editor_institution_actions | |
| 241 | + | |
| 242 | 242 | User.transaction do |
| 243 | 243 | context.profile.user.update_attributes!(context.params[:user]) |
| 244 | 244 | end |
| 245 | - | |
| 246 | - new_community = Institution.find(context.params[:user][:institution_id]).community | |
| 247 | - if old_community != new_community | |
| 248 | - person = context.profile.user.person | |
| 249 | - old_community.remove_member(person) | |
| 250 | - new_community.add_member(person) | |
| 251 | - end | |
| 252 | 245 | end |
| 253 | 246 | |
| 254 | 247 | def institution_transaction |
| ... | ... | @@ -349,4 +342,38 @@ class MpogSoftwarePlugin < Noosfero::Plugin |
| 349 | 342 | context.profile.software_info.controlled_vocabulary.update_attributes!(context.params[:controlled_vocabulary]) |
| 350 | 343 | end |
| 351 | 344 | end |
| 345 | + | |
| 346 | + private | |
| 347 | + | |
| 348 | + # Add and remove the user from it's institutions communities | |
| 349 | + def user_editor_institution_actions | |
| 350 | + user = context.profile.user | |
| 351 | + | |
| 352 | + old_communities = [] | |
| 353 | + context.profile.user.institutions.each do |institution| | |
| 354 | + old_communities << institution.community | |
| 355 | + end | |
| 356 | + | |
| 357 | + new_communities = [] | |
| 358 | + unless context.params[:user][:institution_ids].nil? | |
| 359 | + context.params[:user][:institution_ids].delete("") | |
| 360 | + | |
| 361 | + context.params[:user][:institution_ids].each do |id| | |
| 362 | + new_communities << Institution.find(id).community | |
| 363 | + end | |
| 364 | + end | |
| 365 | + | |
| 366 | + leave_communities = (old_communities - new_communities) | |
| 367 | + enter_communities = (new_communities - old_communities) | |
| 368 | + | |
| 369 | + leave_communities.each do |community| | |
| 370 | + community.remove_member(user.person) | |
| 371 | + user.institutions.delete(community.institution) | |
| 372 | + end | |
| 373 | + | |
| 374 | + enter_communities.each do |community| | |
| 375 | + community.add_member(user.person) | |
| 376 | + user.institutions << community.institution | |
| 377 | + end | |
| 378 | + end | |
| 352 | 379 | end | ... | ... |
views/person_editor_extras.html.erb
| ... | ... | @@ -15,15 +15,28 @@ |
| 15 | 15 | </div> |
| 16 | 16 | |
| 17 | 17 | <div class="formfieldline" id="select_institution"> |
| 18 | - <%= label_tag "user[institution_id]", _('Institution'), :class=>"formlabel" %> | |
| 18 | + <%= label_tag "user[institution_ids]", _('Institutions'), :class=>"formlabel" %> | |
| 19 | 19 | |
| 20 | - <div class="formfield type-text"> | |
| 21 | - <%= select_tag "user[institution_id]", options_for_select(Institution.all.map {|i| [i.name, i.id]}, context.profile.institution_id) %> | |
| 20 | + <div class="institution_container"> | |
| 21 | + <%= text_field_tag(:institution, "", :id=>"input_institution") %> | |
| 22 | + | |
| 23 | + <% context.profile.user.institutions.each do |institution| %> | |
| 24 | + <%= hidden_field_tag("user[institution_ids][]", institution.id, :class => 'user_institutions') %> | |
| 25 | + <% end %> | |
| 22 | 26 | </div> |
| 23 | -<script type="text/javascript"> | |
| 24 | -jQuery(document).ready(function(){ | |
| 25 | - var select = jQuery("#select_institution select"); | |
| 26 | - select.append("<option value='-1'><%=_('Other')%></option>"); | |
| 27 | -}); | |
| 28 | -</script> | |
| 27 | + | |
| 28 | + <%= content_tag(:div, _("The searched institution does not exist"), :id=>"institution_empty_ajax_message", :class=>"errorExplanation hide-field") %> | |
| 29 | + <%= link_to(_("Add new institution"), "#", :class=>'button with-text icon-add', :id => 'add_new_institution') %> | |
| 30 | + | |
| 31 | + <%= hidden_field_tag("user[institution_ids][]", "", :class => 'user_institutions') %> | |
| 32 | + <%= hidden_field_tag("institution_selected", "") %> | |
| 33 | + | |
| 34 | + <ul class="institutions_added"> | |
| 35 | + <% context.profile.user.institutions.each do |institution| %> | |
| 36 | + <li data-institution="<%= institution.id %>"> | |
| 37 | + <%= institution.name %> | |
| 38 | + <%= link_to("", "#", :class => "button without-text icon-remove remove-institution") %> | |
| 39 | + </li> | |
| 40 | + <% end %> | |
| 41 | + </ul> | |
| 29 | 42 | </div> | ... | ... |