Module: MongoCrushyform::ClassMethods
- Defined in:
- lib/mongo_crushyform.rb
Instance Method Summary collapse
-
#crushyfield_required ⇒ Object
What represents a required field Can be overriden.
- #crushyform_types ⇒ Object
- #dropdown_cache ⇒ Object
-
#html_escape(s) ⇒ Object
Stolen from ERB.
- #reset_dropdown_cache ⇒ Object
-
#to_dropdown(selection = nil, nil_name = '** UNDEFINED **') ⇒ Object
Cache dropdown options for children classes to use Meant to be reseted each time an entry is created, updated or destroyed So it is only rebuild once required after the list has changed Maintaining an array and not rebuilding it all might be faster But it will not happen much so that it is fairly acceptable.
Instance Method Details
#crushyfield_required ⇒ Object
What represents a required field Can be overriden
157 |
# File 'lib/mongo_crushyform.rb', line 157 def crushyfield_required; "<span class='crushyfield-required'> *</span>"; end |
#crushyform_types ⇒ Object
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/mongo_crushyform.rb', line 9 def crushyform_types @crushyform_types ||= { :none => proc{''}, :string => proc do |m,c,o| if o[:autocompleted] values = o[:autocomplete_options] || m.class.collection.distinct(c) js = <<-EOJS <script type="text/javascript" charset="utf-8"> $(function(){ $( "##{m.field_id_for(c)}" ).autocomplete({source: ["#{values.join('","')}"]}); }); </script> EOJS end tag = "<input type='%s' name='%s' value=\"%s\" id='%s' class='%s' %s />%s\n" % [o[:input_type]||'text', o[:input_name], o[:input_value], m.field_id_for(c), o[:input_class], o[:required]&&'required', o[:required]] "#{tag}#{js}" end, :slug => proc do |m,c,o| crushyform_types[:string].call(m,c,o) end, :boolean => proc do |m,c,o| crushid = m.field_id_for(c) s = ['checked', nil] s.reverse! unless o[:input_value] out = "<span class='%s'>" out += "<input type='radio' name='%s' value='true' id='%s' %s /> <label for='%s'>Yes</label> " out += "<input type='radio' name='%s' value='false' id='%s-no' %s /> <label for='%s-no'>No</label>" out += "</span>\n" out % [o[:input_class], o[:input_name], crushid, s[0], crushid, o[:input_name], crushid, s[1], crushid] end, :text => proc do |m,c,o| "<textarea name='%s' id='%s' class='%s' %s>%s</textarea>%s\n" % [o[:input_name], m.field_id_for(c), o[:input_class], o[:required]&&'required', o[:input_value], o[:required]] end, :date => proc do |m,c,o| o[:input_value] = o[:input_value].strftime("%Y-%m-%d") if o[:input_value].respond_to?(:strftime) o[:required] = "%s Format: yyyy-mm-dd" % [o[:required]] crushyform_types[:string].call(m,c,o) end, :time => proc do |m,c,o| o[:input_value] = o[:input_value].strftime("%T") if o[:input_value].respond_to?(:strftime) o[:required] = "%s Format: hh:mm:ss" % [o[:required]] crushyform_types[:string].call(m,c,o) end, :datetime => proc do |m,c,o| o[:input_value] = o[:input_value].strftime("%Y-%m-%d %T") if o[:input_value].respond_to?(:strftime) o[:required] = "%s Format: yyyy-mm-dd hh:mm:ss" % [o[:required]] crushyform_types[:string].call(m,c,o) end, :parent => proc do |m,c,o| parent_class = o[:parent_class].nil? ? Kernel.const_get(c.sub(/^id_/, '')) : m.resolve_class(o[:parent_class]) option_list = parent_class.to_dropdown(o[:input_value]) "<select name='%s' id='%s' class='%s'>%s</select>\n" % [o[:input_name], m.field_id_for(c), o[:input_class], option_list] end, :children => proc do |m,c,o| children_class = o[:children_class].nil? ? Kernel.const_get(c.sub(/^ids_/, '')) : m.resolve_class(o[:children_class]) opts = o.update({ :multiple=>true, :select_options=>children_class.dropdown_cache }) @crushyform_types[:select].call(m,c,opts) end, :attachment => proc do |m,c,o| deleter = "<input type='checkbox' name='#{o[:input_name]}' class='deleter' value='nil' /> Delete this file<br />" unless m.doc[c].nil? "%s%s<input type='file' name='%s' id='%s' class='%s' />%s\n" % [m.to_thumb(c), deleter, o[:input_name], m.field_id_for(c), o[:input_class], o[:required]] end, :select => proc do |m,c,o| out = "<select name='%s%s' id='%s' class='%s' %s title='-- Select --'>\n" % [o[:input_name], ('[]' if o[:multiple]), m.field_id_for(c), o[:input_class], ('multiple' if o[:multiple])] o[:select_options] = m.__send__(o[:select_options]) unless o[:select_options].kind_of?(Array) = o[:select_options].dup if (o[:multiple] && !o[:input_value].nil? && o[:input_value].size>1) o[:input_value].reverse.each do |v| elem = .find{|x| x==v||(x||[])[1]==v } .unshift(.delete(elem)) unless elem.nil? end end if .kind_of?(Array) .each do |op| key,val = op.kind_of?(Array) ? [op[0],op[1]] : [op,op] if key==:optgroup out << "<optgroup label='%s'>\n" % [val] elsif key==:closegroup out << "</optgroup>\n" else selected = 'selected' if (val==o[:input_value] || (o[:input_value]||[]).include?(val)) out << "<option value='%s' %s>%s</option>\n" % [val,selected,key] end end end out << "</select>%s\n" % [o[:required]] end, :string_list => proc do |m,c,o| if o[:autocompleted] values = o[:autocomplete_options] || m.class.collection.distinct(c) js = <<-EOJS <script type="text/javascript" charset="utf-8"> $(function(){ $( "##{m.field_id_for(c)}" ) .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 o[:autocompleted] = false # reset so that it does not autocomplete for :string type below end tag = @crushyform_types[:string].call(m,c,o.update({:input_value=>(o[:input_value]||[]).join(',')})) "#{tag}#{js}" end, :permalink => proc do |instance, column_name, | values = "<option value=''>Or Browse the list</option>\n" tag = @crushyform_types[:string].call(instance, column_name, ) return tag if [:permalink_classes].nil? [:permalink_classes].each do |sym| c = Kernel.const_get sym entries = c.find unless entries.count==0 values << "<optgroup label='#{c.human_name}'>\n" entries.each do |e| values << "<option value='#{e.permalink}' #{'selected' if e.permalink==[: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 |
#dropdown_cache ⇒ Object
173 174 175 176 177 |
# File 'lib/mongo_crushyform.rb', line 173 def dropdown_cache @dropdown_cache ||= self.find({},:fields=>['_id',label_column]).inject([]) do |out,row| out.push([row.to_label, row.id.to_s, "<option value='#{row.id}' ", ">#{row.to_label}</option>\n"]) end end |
#html_escape(s) ⇒ Object
Stolen from ERB
159 160 161 |
# File 'lib/mongo_crushyform.rb', line 159 def html_escape(s) s.to_s.gsub(/&/, "&").gsub(/\"/, """).gsub(/>/, ">").gsub(/</, "<") end |
#reset_dropdown_cache ⇒ Object
178 |
# File 'lib/mongo_crushyform.rb', line 178 def reset_dropdown_cache; @dropdown_cache = nil; end |
#to_dropdown(selection = nil, nil_name = '** UNDEFINED **') ⇒ Object
Cache dropdown options for children classes to use
Meant to be reseted each time an entry is created, updated or destroyed
So it is only rebuild once required after the list has changed
Maintaining an array and not rebuilding it all might be faster
But it will not happen much so that it is fairly acceptable
167 168 169 170 171 172 |
# File 'lib/mongo_crushyform.rb', line 167 def to_dropdown(selection=nil, nil_name='** UNDEFINED **') dropdown_cache.inject("<option value=''>#{nil_name}</option>\n") do |out, row| selected = 'selected' if row[1]==selection "%s%s%s%s" % [out, row[2], selected, row[3]] end end |