Class: ActionCrud::Helpers::Link

Inherits:
Object
  • Object
show all
Defined in:
lib/action_crud/helpers/link.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, record = nil, action = nil, *args) ⇒ Link

Initialize link generator



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/action_crud/helpers/link.rb', line 7

def initialize(context, record=nil, action=nil, *args)
  @options = args.extract_options!
  @context = context
  @record  = record || @context.try(:current_record)
  @action  = action
  @label   = options.fetch :label, nil
  @options = options.except :label

  if delete?
    @options.reverse_merge!(method: :delete, data: { confirm: 'Are you sure?' })
  end
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



4
5
6
# File 'lib/action_crud/helpers/link.rb', line 4

def action
  @action
end

#labelObject

Returns the value of attribute label.



4
5
6
# File 'lib/action_crud/helpers/link.rb', line 4

def label
  @label
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/action_crud/helpers/link.rb', line 4

def options
  @options
end

#recordObject

Returns the value of attribute record.



4
5
6
# File 'lib/action_crud/helpers/link.rb', line 4

def record
  @record
end

Instance Method Details

#action_labelObject

Get link label



26
27
28
# File 'lib/action_crud/helpers/link.rb', line 26

def action_label
  label || "#{action}".humanize
end

#delete?Boolean

Is delete action

Returns:

  • (Boolean)


21
22
23
# File 'lib/action_crud/helpers/link.rb', line 21

def delete?
  action.in? [:delete, :destroy]
end

#record_pathObject

Get record path



31
32
33
# File 'lib/action_crud/helpers/link.rb', line 31

def record_path
  ActionCrud::Helpers::Route.new(@context, record, action).path || "#{action}"
end

#renderObject

Render link



49
50
51
# File 'lib/action_crud/helpers/link.rb', line 49

def render
  @context.link_to action_label, record_path, options if record_path.present?
end

#render_multiple(*args) ⇒ Object

Render multiple



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/action_crud/helpers/link.rb', line 36

def render_multiple(*args)
  default = options
  options = args.extract_options!
  actions = args.concat(action).concat(options.keys)
  links   = actions.uniq.map do |item|
    args = Hash(options[item]).reverse_merge(default)
    ActionCrud::Helpers::Link.new(@context, record, item, args).render
  end

  links.join.html_safe
end