Class: Toolbox::ActionRenderer

Inherits:
Renderer
  • Object
show all
Defined in:
lib/toolbox/rendering.rb

Instance Attribute Summary

Attributes inherited from Renderer

#widget_config

Instance Method Summary collapse

Methods inherited from Renderer

#initialize, #translate_field, #value

Constructor Details

This class inherits a constructor from Toolbox::Renderer

Instance Method Details

#labelObject



328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/toolbox/rendering.rb', line 328

def label
    if @widget_config.label
        @widget_config.label
     else
        t = @widget_config.name.to_s.split '_'
        l = @view.send(:_, t[0].humanize)
        if t.size > 1 # e.g. new_group
          model_name = t[1..-1].join '_'
          l += ' ' + @view.send(:s_, model_name.humanize.downcase)
        end
        l
    end
end

#render_action(rec, context, current_action = nil) ⇒ Object



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
# File 'lib/toolbox/rendering.rb', line 342

def render_action rec, context, current_action = nil
  t = @widget_config.name.to_s.split '_'
  action = t[0]
  foreign_action  = t.size > 1
  if foreign_action # e.g. new_group
    model_name = t[1..-1].join '_'
  else
    model_name = @widget_config.model_name
    return nil if action == current_action
  end
  css_class = @widget_config.active == 1 ? 'enabled ' : 'disabled '
  css_class += @widget_config.css_class || action

  if @widget_config.active == 1
    case action
    when 'separator'
      @widget_config.name if context
    when 'new'
      # copy the _id params to save the context
      pars = {}
      pars.merge! context if context
      # delete self-reference
      pars.delete model_name + '_id'
      # set the object as parameter
      pars[rec.class.name.underscore + '_id'] = rec #if foreign_action
      url = @view.send("#{action}_#{model_name}_path".to_sym, pars)
      if !@widget_config.direct_link && (context || foreign_action)
        @view.link_to_remote label, { :url => url, :method => :get }, {:class => css_class }
      else
        @view.link_to label, url, {:class => css_class}
      end
    when 'show'
      @view.link_to label, @view.polymorphic_path(rec), :class => css_class
    when 'destroy'
      msg = @view.send(:_, 'Are you sure?')
      if context
        url = @view.polymorphic_path(rec)
        @view.link_to_remote label, { :url => url, :method => :delete, :confirm => msg }, {:class => css_class }
      else
        @view.link_to label, rec, :confirm => msg, :method => :delete, :class => css_class
      end
    when 'list'
      @view.link_to label, @view.send("#{model_name.pluralize}_path".to_sym), :class => css_class
    when 'edit'
      url = @view.edit_polymorphic_path rec
      if context
        @view.link_to_remote label, { :url => url, :method => :get }, {:class => css_class }
      else
        @view.link_to label, url
      end
    else
      url = @widget_config.url || @view.formatted_polymorphic_path([rec, action]) #eval("@view.#{action}_#{model_name}_path(rec)")
      if context
        if @widget_config.direct_link
          @view.link_to label, url, :class => css_class
        else
          @view.link_to_remote label, { :url => url, :method => :get }, {:class => css_class }
        end
      else
        @view.link_to label, url
      end
    end # case
  else # if disabled
    if context
      @view. :a, label, :href => '#', :class => css_class
    else
      label
    end
  end
end