Module: Sequel::Plugins::BureauAdapter

Defined in:
lib/sequel_bureau_adapter.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Class Method Details

.configure(model, opts = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
# File 'lib/sequel_bureau_adapter.rb', line 5

def self.configure(model, opts = {})
  model.bureau_config = opts
  model.crushyform_types.update({
    :autocomplete => proc do |instance, column_name, options|
      values = model.select(column_name).exclude(column_name=>'').map(column_name)
      values = values.inject([]){|s,l| s+l.split(/\s*,\s*/)} if options[:autocomplete_multiple]
      values.uniq.compact!
      tag = model.crushyform_types[:string].call(instance, column_name, options)
      return tag if values.empty?
      unless options[:autocomplete_multiple]
        js = <<-EOJS
        <script type="text/javascript" charset="utf-8">
          $(function(){
            $( "##{instance.crushyid_for(column_name)}" ).autocomplete({source: ["#{values.join('","')}"]});
          });
        </script>
        EOJS
      else
        js = <<-EOJS
        <script type="text/javascript" charset="utf-8">
          $(function(){
            $( "##{instance.crushyid_for(column_name)}" )
            .bind( "keydown", function( event ) {
            	if ( event.keyCode === $.ui.keyCode.TAB &&
            	$( this ).data( "autocomplete" ).menu.active ) {
            		event.preventDefault();
            	}
            })
            .autocomplete({
            	minLength: 0,
            	source: function( request, response ) {
            		response($.ui.autocomplete.filter(["#{values.join('","')}"], request.term.split(/,\s*/).pop()));
            	},
            	focus: function() { return false; },
            	select: function( event, ui ) {
            		var terms = this.value.split(/,\s*/);
            		terms.pop();
            		terms.push(ui.item.value);
            		terms.push("");
            		this.value = terms.join( ", " );
            		return false;
            	}
            });
          });
        </script>
        EOJS
      end
      tag+js
    end,
    :permalink => proc do |instance, column_name, options|
      values = "<option value=''>Or Browse the list</option>\n"
      tag = model.crushyform_types[:string].call(instance, column_name, options)
      return tag if options[:permalink_classes].nil?
      options[:permalink_classes].each do |sym|
        c = Kernel.const_get sym
        entries = c.all
        unless entries.empty?
          values << "<optgroup label='#{c.human_name}'>\n"
          entries.each do |e|
            values << "<option value='#{e.permalink}' #{'selected' if e.permalink==options[:input_value]}>#{e.to_label}</option>\n"
          end
          values << "</optgroup>\n"
        end
      end
      "#{tag}<br />\n<select name='__permalink' class='permalink-dropdown'>\n#{values}</select>\n"
    end
  })
end