Class: Cmtool::Menu::ResourceLink

Inherits:
ElementBase show all
Defined in:
lib/cmtool/menu.rb

Overview

Resource link, indicates a link to standard resource. The first argument should

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ElementBase

#controller_names, #method_missing, #options

Constructor Details

#initialize(resource, options = {}) ⇒ ResourceLink

Returns a new instance of ResourceLink.



87
88
89
# File 'lib/cmtool/menu.rb', line 87

def initialize(resource, options = {})
  @resource, @options = resource, options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Cmtool::Menu::ElementBase

Instance Attribute Details

#resourceObject (readonly)

Returns the value of attribute resource.



86
87
88
# File 'lib/cmtool/menu.rb', line 86

def resource
  @resource
end

Instance Method Details

#controller_nameObject



95
96
97
# File 'lib/cmtool/menu.rb', line 95

def controller_name
  @resource.name.underscore.split('/').last.pluralize
end

#engineObject



105
106
107
108
109
110
111
112
113
114
# File 'lib/cmtool/menu.rb', line 105

def engine
  base = @options[:scope].presence
  base ||= @resource.name.split('::').first if @resource.name.index('::')
  return Rails.application unless base
  return base if base.is_a?(Rails::Engine)
  if e = "#{base}::Engine".safe_constantize
    return e
  end
  return Rails.application
end

#nested_controller_nameObject



115
116
117
118
119
120
121
# File 'lib/cmtool/menu.rb', line 115

def nested_controller_name
  if @options[:scope].present?
    "#{@options[:scope]}::#{@resource.name}".underscore.pluralize
  else
    @resource.name.underscore.pluralize
  end
end

#pathObject



101
102
103
# File 'lib/cmtool/menu.rb', line 101

def path
  engine.routes.url_helpers.url_for(controller: nested_controller_name, action: 'index', only_path: true)
end

#resource_link?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/cmtool/menu.rb', line 91

def resource_link?
  true
end

#titleObject



98
99
100
# File 'lib/cmtool/menu.rb', line 98

def title
  @resource.model_name.human_plural
end