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, #engine_link?, #method_missing, #options

Constructor Details

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

Returns a new instance of ResourceLink.



124
125
126
# File 'lib/cmtool/menu.rb', line 124

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.



123
124
125
# File 'lib/cmtool/menu.rb', line 123

def resource
  @resource
end

Instance Method Details

#controller_nameObject



132
133
134
# File 'lib/cmtool/menu.rb', line 132

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

#engineObject



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/cmtool/menu.rb', line 142

def engine
  return @options[:engine] if @options[:engine].present?
  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



153
154
155
156
157
158
159
# File 'lib/cmtool/menu.rb', line 153

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

#pathObject



138
139
140
# File 'lib/cmtool/menu.rb', line 138

def path
  engine.routes.url_helpers.url_for({controller: nested_controller_name, action: 'index', only_path: true}.merge(@options[:path] || {}))
end

#resource_link?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/cmtool/menu.rb', line 128

def resource_link?
  true
end

#titleObject



135
136
137
# File 'lib/cmtool/menu.rb', line 135

def title
  @resource.model_name.human_plural
end