Module: ApplicationHelper

Defined in:
lib/app/helpers/application_helper.rb

Overview

Methods added to this helper will be available to all templates in the application.

Instance Method Summary collapse

Instance Method Details

#hbutton_to(text, url, active = true) ⇒ Object



396
397
398
399
400
401
402
403
404
# File 'lib/app/helpers/application_helper.rb', line 396

def hbutton_to(text, url, active=true)
  out=''
  out<<"<button type='button' onclick=\"JavaScript: window.location='"
  out<< url_for(url)
  out<<"'\">"
  out<<text
  out<<'</button> '
  return out
end

#hi(lib) ⇒ Object



301
302
303
304
305
306
307
# File 'lib/app/helpers/application_helper.rb', line 301

def hi(lib)
  if lib.to_s==""
    return "-"
  else
    h(lib)
  end
end

#ican?(*args) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
9
10
11
12
13
# File 'lib/app/helpers/application_helper.rb', line 6

def ican?(*args)
    begin
        # if CanCan plugin installed
          can?(*args)
    rescue
          true
    end
end

#icannot?(*args) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
# File 'lib/app/helpers/application_helper.rb', line 14

def icannot?(*args)
    begin
        # if CanCan plugin installed
          cannot?(*args)
    rescue
          false
    end
end

#image_addObject



329
330
331
# File 'lib/app/helpers/application_helper.rb', line 329

def image_add
  return image_tag("aslon_scaffold/add.gif", :border=>0, :style=>"vertical-align:middle",:id=>'add')+"[Ajouter]"
end

#image_deleteObject



325
326
327
# File 'lib/app/helpers/application_helper.rb', line 325

def image_delete
  return image_tag("aslon_scaffold/delete.gif", :border=>0, :style=>"vertical-align:middle")+"[Supp]"
end

#image_editObject



321
322
323
# File 'lib/app/helpers/application_helper.rb', line 321

def image_edit
  return image_tag("aslon_scaffold/edit.gif", :border=>0, :style=>"vertical-align:middle")+"[Edit]"
end

#image_exportObject



345
346
347
# File 'lib/app/helpers/application_helper.rb', line 345

def image_export
   return image_tag("icons/mini/icon_download.gif", :border=>0, :style=>"vertical-align:middle")+"Export .csv"
end

#image_folderObject



337
338
339
# File 'lib/app/helpers/application_helper.rb', line 337

def image_folder
  return image_tag("aslon_scaffold/folder.gif", :border=>0, :style=>"vertical-align:bottom")
end

#image_importObject



349
350
351
# File 'lib/app/helpers/application_helper.rb', line 349

def image_import
   return image_tag("icons/mini/page_new.gif", :border=>0, :style=>"vertical-align:middle")+"Import"
end

#image_refreshObject



341
342
343
# File 'lib/app/helpers/application_helper.rb', line 341

def image_refresh
  return image_tag("icons/mini/page_refresh.gif", :border=>0, :style=>"vertical-align:bottom")
end

#image_zoomObject



333
334
335
# File 'lib/app/helpers/application_helper.rb', line 333

def image_zoom
  return image_tag("aslon_scaffold/magnifier.png", :border=>0, :style=>"vertical-align:middle")+"[Voir]"
end

#l(lib) ⇒ Object

todo rendre multiplangues



292
293
294
# File 'lib/app/helpers/application_helper.rb', line 292

def l(lib)
  return lib.to_s.humanize
end


75
76
77
78
79
# File 'lib/app/helpers/application_helper.rb', line 75

def link_to_image_add(objet)
 if ican? :create,objet
    link_to image_add,eval("new_#{objet.class.to_s.underscore}_path(objet)"),:id=>'add'
 end
end


80
81
82
83
84
# File 'lib/app/helpers/application_helper.rb', line 80

def link_to_image_delete(objet)
    if ican? :destroy, objet
      link_to(image_delete, objet, :confirm => 'Are you sure?', :method => :delete, :id=>'delete')
    end
end


70
71
72
73
74
# File 'lib/app/helpers/application_helper.rb', line 70

def link_to_image_edit(objet)
  if ican? :edit,objet
     link_to image_edit,eval("edit_#{objet.class.to_s.underscore.gsub('/','_')}_path(objet)"),:id=>'edit'
  end
end

#llib(lib) ⇒ Object



296
297
298
299
# File 'lib/app/helpers/application_helper.rb', line 296

def llib(lib)
  return lib.to_s.humanize if $l[lib]==nil
  return "<b>"+$l[lib]+"</b>"
end

#mbObject

Outputs the error messages block. The first argument specifies a hash options:

  • :on => :products Also includes AR validation errors for @products

  • :clear => true Clears messages after displaying

  • :keep => true Keeps around messages for next response cycle



361
362
363
# File 'lib/app/helpers/application_helper.rb', line 361

def mb
  out = "é"
end

#message_block(options = {}) ⇒ Object



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/app/helpers/application_helper.rb', line 365

def message_block(options = {})
  out = ""

  [:back, :confirm, :error, :info, :warn, :notice].each do |type|
    next if flash[type].nil? or flash[type].empty?
    flash[type] = [flash[type]] unless flash[type].is_a?(Array)

    out << "<div class=\"container #{type}\"><ul>\n"
    flash[type].each {|msg| out << "<li>#{h(msg.to_s)}</li>\n"}
    out << "</ul></div>\n"

    flash[type] = nil if options[:clear]
    flash.keep[type] if options[:keep]
  end

  if options[:on]
    options[:on] = [options[:on]] unless options[:on].kind_of?(Array)
    models = options[:on].map {|m| instance_variable_get("@" + m.to_s)}.select {|m| !m.nil?}
    errors = models.inject([]) {|b, m| b += m.errors.full_messages}

    if errors.size > 0
      out << "<div class=\"container error\"><ul>\n"
      errors.each {|msg| out << "<li>#{h(msg.to_s)}</li>\n"}
      out << "</ul></div>\n"
    end
  end

  (:div, out.html_safe, :id => 'message_block', :class => 'flash')
end

#model_name(model) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/app/helpers/application_helper.rb', line 47

def model_name(model)
  begin
     eval("#{model}.model_name.human")
   rescue
     model.humanize
   end
end

#show(objet, attribut) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/app/helpers/application_helper.rb', line 31

def show(objet,attribut)


   aslib= eval "#{objet.class.name}.human_attribute_name(attribut)"
   begin
   if attribut.split('.').size==2
     return ("<li class='string input optional stringish'><label  class='label'>#{aslib}</label>"+objet.send( attribut.split('.').first).send( attribut.split('.').second).to_s+"</li>").html_safe
   else
      return ("<li class='string input optional stringish'><label class='label'>#{aslib}</label>"+url(objet.send(attribut).to_s)+"</li>").html_safe
   end

  rescue
    return ("<li class='string input optional stringish'><label class='label'>#{aslib}</label>?</li>").html_safe
  end
end

#submit_action(form, lib, action) ⇒ Object



406
407
408
# File 'lib/app/helpers/application_helper.rb', line 406

def submit_action(form, lib, action)
  button_to_function lib, "$('#{form}').action='#{action}';$('#{form}').submit();"
end

#table(model, table, h, actions) ⇒ Object




162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/app/helpers/application_helper.rb', line 162

def table(model, table, h, actions)
  sel=''
  sel='&type='+params[:type] if params[:type]
  sel+='&status='+params[:status] if params[:status]

  wp=will_paginate(table)
  info_titre_tableau=page_entries_info(table)
  s=''.force_encoding('utf-8')
  # --------------------------------------------------------------------
  s<<"<table id='liste'>
              <caption>#{info_titre_tableau}</caption>
        <thead>
          <tr>"
  # --------------------------------------------------------------------
  # intitulé des colonnes
  # --------------------------------------------------------------------

    h.each do |value|
      img='aslon_scaffold/sort_desc.gif'
      sens='DESC'
      if params[:order]==value && params[:sens]=='DESC'
        sens='ASC'
        img='aslon_scaffold/sort_desc.gif'
      end

      if params[:order]==value && params[:sens]=='ASC'
        sens='DESC'
        img='aslon_scaffold/sort_asc.gif'
      end
       hlib=eval("#{model.gsub('_','/').camelize}.human_attribute_name(value)")
      # sorting doesn't work with search
      if request.env['QUERY_STRING'] != "method=get" && !value.include?('.')
        s<<"<th scope='col' onclick=\"self.location.href = \'#{request.env['PATH_INFO']}?#{sel}&order=#{value}&sens=#{sens}\'; \">"
        s<< hlib
        s<<" "+image_tag(img, :border=>0, :style=>"vertical-align:middle") if (img)
        s<<"</th>"
      else
#          s<< "<th scope='col'>"+l(key)+"</th>"
        s<< "<th scope='col'>"+hlib+"</th>"
      end
    end

   s<<'</tr>'
   s<<'</thead>'
  # --------------------------------------------------------------------
  # bas du tableau
  # --------------------------------------------------------------------
  s<< " <tfoot>
            <tr>
                <th scope='row'>#{info_titre_tableau}</th>
                <td colspan='#{h.size-1}'>&nbsp; #{wp}</td>
            </tr>
        </tfoot>"
  s<<'<tbody>'
  # --------------------------------------------------------------------
  # lignes du tableau
  # --------------------------------------------------------------------
  i=1
  script="".force_encoding('utf-8')
  for aLine in table
    id=''
    c_id=''
    # ------------- création id de la ligne --------------------------
    h.each do |lib|          
            begin
              v=eval("aLine."+"#{lib}").to_s
            rescue
              v='nil'
            end
            id<<model+'_'+lib+'_'+ v.gsub(' ', '')
            c_id=aLine.id

    end
    # ----------------------------------------------------------------
    # ----------------------------------------------------------------
    if i%2==0
      s<<"<tr id='#{id}' class=#{c_id}>"
    else
      s<<"<tr class='odd #{c_id}' id='#{id}'>"
    end
    i+=1
    # ----------------------------------------------------------------
    cp=0

    h.each do |lib|

        local= I18n.locale
        begin
          v=url(eval("aLine."+"#{lib}").to_s)
        rescue
          v='nil'
        end
        s<<""
        premiere_colonne=v
        t=model.singularize.downcase + "_url(aLine)"
        if cp==0
           s<<("<th scope='row' "+"class='"+model.singularize.downcase+'_'+lib+'_'+ v+'_'+"'>" )
          premiere_colonne= link_to(v, eval(t),:id=>'show')
        else
          s<<('<td '+"class='"+model+'_'+lib.to_s+'_'+ v+'_'+local.to_s+"'>")
        end

        s<< premiere_colonne
        lik= url_for( eval(t))
#        script << "<script>$('#{id}').observe('click',function(e) {window.location='#{lik}';},false)</script>"
#        script << "<script>$('#{id}').observe('click',function(e) {window.location='#{lik}';},false)</script>"
      #  script << "<script>$('##{id}').click(function(){window.location='#{lik}';})</script>"
        script << "<script>$('.#{c_id}').click(function(){window.location='#{lik}';})</script>"
      if cp==0
        s<<"&nbsp;</th>"
      else
        s<<"&nbsp;</td>"
      end
      cp+=1
    end
    s<<'</tr>'

  end
  s<<'</tbody>'
  s<<"</table>"
  s<<script
  if table.size>0
    return s
  else
    return "0 line"
  end

end

#td(lib) ⇒ Object



309
310
311
# File 'lib/app/helpers/application_helper.rb', line 309

def td (lib)
  return "<td>"+hi(lib)+"</td>"
end

#titre(titre, model = nil, search = true, add = true, action = 'search', addlink = nil) ⇒ Object


Création de la barre de titre à associé au tableau




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
154
155
156
157
158
# File 'lib/app/helpers/application_helper.rb', line 92

def titre(titre, model=nil, search=true, add=true, action='search',addlink=nil)

   s=form_tag('javascript:void(0)' )
  s="
<table id='titreliste' cellpadding='0' cellspacing='0' >
<tr>
	<th COLSPAN='10'>#{titre}"
  if add==true
    if addlink==nil
    t="new_"+model.singularize.underscore.gsub('/','_') + "_path(:fournisseur_id=>params[:fournisseur_id],
		:rfq_id=>params[:rfq_id],
              :client_id=>params[:client_id])"
    else
      t=addlink
    end
    s<<link_to(image_add, eval(t),:id=>'add') if ican?(:create, eval(model.camelcase))

  end
  if search
    s<<" "+image_tag("aslon_scaffold/grid-split.gif", :border=>0, :style=>"vertical-align:middle")
    s<<" Search : "
    s<< text_field_tag( :search, nil, {:size=>10})
    s<< image_tag("aslon_scaffold/indicator.gif", :style=>"vertical-align:middle", :border => 0, :id => "spiner",:style => "display:none;" )
  end
  s<<"</th>
</tr>
</table>"
  if search
    cond=":type=>'simle'"
#      s<< observe_field(:search,
#                        :frequency=>1,
#                        :update=>"ajaxw",
#                        :before=>"Element.show('spiner')",
#                        :success=>"Element.hide('spiner')",
#                        :url=>{ :controller=>model.underscore.pluralize,
#                                :action=>action,
#                                :method => 'get',
#                                :only_path=>false,
#                                :type=>params[:type],
#                                :status=>params[:status],
#                                :fournisseur_id=>params[:fournisseur_id],
#                                :rfq_id=>params[:rfq_id],
#                                :client_id=>params[:client_id]
#                        },
#                        :with=>"'search='+encodeURIComponent(value)")
    url=  url_for(:action => 'search', :controller => model.underscore.pluralize)

   s<< "<script>
 $(document).ready(function(){

 $('#search').bind('keyup', function() {
      $('#spiner').show();
      var url = '#{url}';
      var formData = 'search='+encodeURIComponent($('#search').val());
      $.post(url, formData, function(html) {
        $('#spiner').hide(); // hide the spinner
        $('#ajaxw').html(html);
      });
    });

 });
</script>"

  end
  return s.html_safe

end

#titre_action(action, model) ⇒ Object



54
55
56
57
# File 'lib/app/helpers/application_helper.rb', line 54

def titre_action (action,model)
  txt=model_name(model)
  ("<h1 class='aslon_scaffold'>"+t("views.generic.#{action}", :model=>txt)+"</h1>").html_safe
end

#titre_show(objet, lib) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/app/helpers/application_helper.rb', line 59

def titre_show(objet, lib)
  model=objet.class
  begin
    txt=eval("#{model}.model_name.human")
  rescue
    txt= model.model_name
  end
  ("<h1 class='aslon_scaffold'> #{txt} : #{lib} #{link_to_image_edit(objet)} #{link_to_image_delete(objet)}</h1>").html_safe

end

#tri(i) ⇒ Object



313
314
315
316
317
318
319
# File 'lib/app/helpers/application_helper.rb', line 313

def tri(i)
  if i%2==0
    return "<tr class='pair'>"
  else
    return "<tr>"
  end
end

#url(v) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/app/helpers/application_helper.rb', line 23

def url(v)
     if v =~ /http:/
      return "<a href=#{v}>#{v}</a>"
    else
      return v
     end
end