Class: ActionDispatch::Routing::Mapper::Resources::Resource

Inherits:
Object
  • Object
show all
Defined in:
actionpack/lib/action_dispatch/routing/mapper.rb

Overview

:nodoc:

Direct Known Subclasses

SingletonResource

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entities, api_only, shallow, only: nil, except: nil, **options) ⇒ Resource

Returns a new instance of Resource.



1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1319

def initialize(entities, api_only, shallow, only: nil, except: nil, **options)
  if options[:param].to_s.include?(":")
    raise ArgumentError, ":param option can't contain colons"
  end

  valid_actions = self.class.default_actions(false) # ignore api_only for this validation
  if (invalid_actions = invalid_only_except_options(valid_actions, only:, except:).presence)
    error_prefix = "Route `resource#{"s" unless singleton?} :#{entities}`"
    raise ArgumentError, "#{error_prefix} - :only and :except must include only #{valid_actions}, but also included #{invalid_actions}"
  end

  @name       = entities.to_s
  @path       = (options[:path] || @name).to_s
  @controller = (options[:controller] || @name).to_s
  @as         = options[:as]
  @param      = (options[:param] || :id).to_sym
  @options    = options
  @shallow    = shallow
  @api_only   = api_only
  @only       = only
  @except     = except
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



1317
1318
1319
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1317

def controller
  @controller
end

#paramObject (readonly)

Returns the value of attribute param.



1317
1318
1319
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1317

def param
  @param
end

#pathObject (readonly) Also known as: collection_scope

Returns the value of attribute path.



1317
1318
1319
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1317

def path
  @path
end

Class Method Details

.default_actions(api_only) ⇒ Object



1308
1309
1310
1311
1312
1313
1314
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1308

def default_actions(api_only)
  if api_only
    [:index, :create, :show, :update, :destroy]
  else
    [:index, :create, :new, :show, :update, :destroy, :edit]
  end
end

Instance Method Details

#actionsObject



1346
1347
1348
1349
1350
1351
1352
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1346

def actions
  if @except
    available_actions - Array(@except).map(&:to_sym)
  else
    available_actions
  end
end

#available_actionsObject



1354
1355
1356
1357
1358
1359
1360
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1354

def available_actions
  if @only
    Array(@only).map(&:to_sym)
  else
    default_actions
  end
end

#collection_nameObject

Checks for uncountable plurals, and appends “_index” if the plural and singular form are the same.



1378
1379
1380
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1378

def collection_name
  singular == plural ? "#{plural}_index" : plural
end

#default_actionsObject



1342
1343
1344
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1342

def default_actions
  self.class.default_actions(@api_only)
end

#member_scopeObject Also known as: shallow_scope



1388
1389
1390
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1388

def member_scope
  "#{path}/:#{param}"
end

#nameObject



1362
1363
1364
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1362

def name
  @as || @name
end

#nested_paramObject



1398
1399
1400
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1398

def nested_param
  :"#{singular}_#{param}"
end

#nested_scopeObject



1402
1403
1404
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1402

def nested_scope
  "#{path}/:#{nested_param}"
end

#new_scope(new_path) ⇒ Object



1394
1395
1396
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1394

def new_scope(new_path)
  "#{path}/#{new_path}"
end

#pluralObject



1366
1367
1368
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1366

def plural
  @plural ||= name.to_s
end

#resource_scopeObject



1382
1383
1384
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1382

def resource_scope
  controller
end

#shallow?Boolean

Returns:

  • (Boolean)


1406
1407
1408
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1406

def shallow?
  @shallow
end

#singleton?Boolean

Returns:

  • (Boolean)


1410
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1410

def singleton?; false; end

#singularObject Also known as: member_name



1370
1371
1372
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1370

def singular
  @singular ||= name.to_s.singularize
end