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



528
529
530
531
532
533
534
535
536
# File 'lib/app/helpers/application_helper.rb', line 528

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



433
434
435
436
437
438
439
# File 'lib/app/helpers/application_helper.rb', line 433

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



461
462
463
# File 'lib/app/helpers/application_helper.rb', line 461

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

#image_deleteObject



457
458
459
# File 'lib/app/helpers/application_helper.rb', line 457

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

#image_editObject



453
454
455
# File 'lib/app/helpers/application_helper.rb', line 453

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

#image_exportObject



477
478
479
# File 'lib/app/helpers/application_helper.rb', line 477

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

#image_folderObject



469
470
471
# File 'lib/app/helpers/application_helper.rb', line 469

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

#image_importObject



481
482
483
# File 'lib/app/helpers/application_helper.rb', line 481

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

#image_refreshObject



473
474
475
# File 'lib/app/helpers/application_helper.rb', line 473

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

#image_zoomObject



465
466
467
# File 'lib/app/helpers/application_helper.rb', line 465

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

#l(lib) ⇒ Object

todo rendre multiplangues



424
425
426
# File 'lib/app/helpers/application_helper.rb', line 424

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

can? :edit,objet.class



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

def link_to_image_add(objet)
  if ican? :create,objet.class
     # link_to image_add,eval("new_#{objet.class.to_s.underscore}_path(objet)"),:id=>'add'
    link_to '<i class="glyphicon glyphicon-plus"></i> Add'.html_safe, eval("new_#{objet.class.to_s.underscore.gsub('/','_')}_path(objet)"), class: 'btn btn-default'
  end
end


82
83
84
85
86
# File 'lib/app/helpers/application_helper.rb', line 82

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


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

def link_to_image_edit(objet)
  if can? :edit,objet.class
      link_to '<i class="glyphicon glyphicon-pencil"></i> Edit'.html_safe, eval("edit_#{objet.class.to_s.underscore.gsub('/','_')}_path(objet)"), class: 'btn btn-default'
  end
 # can? :edit,objet.class
end

#llib(lib) ⇒ Object



428
429
430
431
# File 'lib/app/helpers/application_helper.rb', line 428

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



493
494
495
# File 'lib/app/helpers/application_helper.rb', line 493

def mb
  out = "é"
end

#message_block(options = {}) ⇒ Object



497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
# File 'lib/app/helpers/application_helper.rb', line 497

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



538
539
540
# File 'lib/app/helpers/application_helper.rb', line 538

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

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




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
290
291
292
# File 'lib/app/helpers/application_helper.rb', line 164

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)
  info_titre_tableau=""
  s=''.force_encoding('utf-8')
  # --------------------------------------------------------------------
  s<<"<table class='table table-responsive table-hover table-striped' 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

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



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/app/helpers/application_helper.rb', line 293

def tabledd(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<<"<tbody><table  class='table table-responsive table-hover table-striped' 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 data-field=#{value} 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<<"</table>"
  s<<'</tbody>'
  s<<script
  if table.size>0
    return s
  else
    return "0 line"
  end

end

#td(lib) ⇒ Object



441
442
443
# File 'lib/app/helpers/application_helper.rb', line 441

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




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
159
160
# File 'lib/app/helpers/application_helper.rb', line 94

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

   s=form_tag('javascript:void(0)' )
  # s="#{titre}"
   s=""
  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))
    s<<"<form class='form-inline'>"

    s<<link_to("<i class='glyphicon glyphicon-plus'></i> Add".html_safe, eval(t), class: 'btn btn-default')

  end
  if search
    # s<<" "+image_tag("aslon_scaffold/grid-split.gif", :border=>0, :style=>"vertical-align:middle")
    s<< text_field_tag( :search, nil, {:size=>20,:class=>'form-control',:placeholder=>"Search"})
    s<<"</form>"
    s<< image_tag("aslon_scaffold/indicator.gif", :style=>"vertical-align:middle", :border => 0, :id => "spiner",:style => "display:none;" )

  end

  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)
  ("<div class='aslon_scaffold'>"+t("views.generic.#{action}", :model=>txt)+"</div>").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



445
446
447
448
449
450
451
# File 'lib/app/helpers/application_helper.rb', line 445

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