Module: AeUsers::HelperFunctions

Defined in:
lib/ae_users.rb

Instance Method Summary collapse

Instance Method Details

#all_permitted?(item, perm) ⇒ Boolean

Returns:

  • (Boolean)


603
604
605
606
607
608
609
610
611
612
# File 'lib/ae_users.rb', line 603

def all_permitted?(item, perm)
  if item
    # try to short-circuit this with an eager load check
    if item.permissions.select {|p| (p.permission == perm or p.permission.nil?) and p.role.nil? and p.person.nil? }.size > 0
      return true
    end
  end
  sql = "permission = ? and (role_id = 0 or role_id is null) and (person_id = 0 or person_id is null)"
  return Permission.find(:all, :conditions => [sql, full_permission_name(item, perm)]).length > 0
end

#app_profile(person = nil) ⇒ Object



622
623
624
625
626
627
628
# File 'lib/ae_users.rb', line 622

def app_profile(person = nil)
  if person.nil?
    person = logged_in_person
  end

  AeUsers.profile_class.find_by_person_id(person.id)
end

#full_permission_name(item, perm) ⇒ Object



585
586
587
588
589
590
591
# File 'lib/ae_users.rb', line 585

def full_permission_name(item, perm)
  if item.kind_of? ActiveRecord::Base
    return perm
  else
    return "#{perm}_#{item.class.name.tableize}"
  end
end

#logged_in?Boolean

Returns:

  • (Boolean)


614
615
616
# File 'lib/ae_users.rb', line 614

def logged_in?
  return controller.logged_in?
end

#logged_in_personObject



618
619
620
# File 'lib/ae_users.rb', line 618

def logged_in_person
  return controller.logged_in_person
end

#permission_grants(item, perm) ⇒ Object



593
594
595
596
597
598
599
600
601
# File 'lib/ae_users.rb', line 593

def permission_grants(item, perm)
  if item.kind_of? ActiveRecord::Base
    grants = item.permissions.select {|p| p.permission == perm }
  else
    full_perm_name = full_permission_name(item, perm)
    grants = Permission.find_all_by_permission(full_perm_name)
  end
  return grants
end

#permission_names(item) ⇒ Object



577
578
579
580
581
582
583
# File 'lib/ae_users.rb', line 577

def permission_names(item)
  if item.kind_of? ActiveRecord::Base
    return item.class.permission_names
  else
    return item.permission_names
  end
end

#user_picker(field_name, options = {}) ⇒ Object



630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
# File 'lib/ae_users.rb', line 630

def user_picker(field_name, options = {})
  options = {
    :people => true,
    :roles => false,
    :callback => nil,
    :default => nil,
    :clear_after => true
  }.update(options)
  
  domid = field_name.gsub(/\W/, "_").gsub(/__+/, "_").sub(/_$/, "").sub(/^_/, "")
  
  default = options[:default]
  rhtml = text_field_tag("#{field_name}_shim", default ? default.name : "", { :style => "width: 15em; display: inline; float: none;" })
  rhtml << hidden_field_tag(field_name, default ? default.id : "")
  auto_complete_url = url_for(:controller => "permission", :action => "auto_complete_for_permission_grantee",
                              :people => options[:people], :roles => options[:roles], :escape => false)
  
  if AeUsers.js_framework == "prototype"
    rhtml << <<-ENDRHTML
<div id="#{domid}_shim_auto_complete" class="auto_complete"></div>
<%= auto_complete_field('#{domid}_shim', :select => "grantee_id", :param_name => "q",
:after_update_element => "function (el, selected) { 
    kid = el.value.split(':');
    klass = kid[0];
    id = kid[1];
    cb = function(klass, id) {
      $('#{domid}').value = el.value;
      #{options[:clear_after] ? "$('#{domid}_shim').value = '';" : "$('#{domid}_shim').value = selected.getAttribute('granteeName');"}
      #{options[:callback]}
    };
    cb(klass, id);
  }",
:url => "#{auto_complete_url}") %>
ENDRHTML
  elsif AeUsers.js_framework == "jquery"
    rhtml << <<-ENDRHTML
<script type="text/javascript">
jQuery(function() {
  jq_domid = "\##{domid.gsub(/(\W)/, '\\\\\\\\\1')}";
  jQuery(jq_domid + "_shim").autocomplete('#{auto_complete_url}',
  {
    formatItem: function(data, i, n, value) {
      return value;
    },
  }).bind('result', function(e, data) {
    jQuery(jq_domid).val(data[1]);
    #{options[:callback]}
  }
   );
});
</script>
ENDRHTML
  end
  
  render :inline => rhtml
end