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

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

Overview

:nodoc:

Direct Known Subclasses

SingletonResource

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entities, api_only, shallow, options = {}) ⇒ Resource

Returns a new instance of Resource.



1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
# File 'lib/action_dispatch/routing/mapper.rb', line 1180

def initialize(entities, api_only, shallow, options = {})
  if options[:param].to_s.include?(":")
    raise ArgumentError, ":param option can't contain colons"
  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       = options.delete :only
  @except     = options.delete :except
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



1178
1179
1180
# File 'lib/action_dispatch/routing/mapper.rb', line 1178

def controller
  @controller
end

#paramObject (readonly)

Returns the value of attribute param.



1178
1179
1180
# File 'lib/action_dispatch/routing/mapper.rb', line 1178

def param
  @param
end

#pathObject (readonly) Also known as: collection_scope

Returns the value of attribute path.



1178
1179
1180
# File 'lib/action_dispatch/routing/mapper.rb', line 1178

def path
  @path
end

Instance Method Details

#actionsObject



1205
1206
1207
1208
1209
1210
1211
# File 'lib/action_dispatch/routing/mapper.rb', line 1205

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

#available_actionsObject



1213
1214
1215
1216
1217
1218
1219
# File 'lib/action_dispatch/routing/mapper.rb', line 1213

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.



1237
1238
1239
# File 'lib/action_dispatch/routing/mapper.rb', line 1237

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

#default_actionsObject



1197
1198
1199
1200
1201
1202
1203
# File 'lib/action_dispatch/routing/mapper.rb', line 1197

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

#member_scopeObject Also known as: shallow_scope



1247
1248
1249
# File 'lib/action_dispatch/routing/mapper.rb', line 1247

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

#nameObject



1221
1222
1223
# File 'lib/action_dispatch/routing/mapper.rb', line 1221

def name
  @as || @name
end

#nested_paramObject



1257
1258
1259
# File 'lib/action_dispatch/routing/mapper.rb', line 1257

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

#nested_scopeObject



1261
1262
1263
# File 'lib/action_dispatch/routing/mapper.rb', line 1261

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

#new_scope(new_path) ⇒ Object



1253
1254
1255
# File 'lib/action_dispatch/routing/mapper.rb', line 1253

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

#pluralObject



1225
1226
1227
# File 'lib/action_dispatch/routing/mapper.rb', line 1225

def plural
  @plural ||= name.to_s
end

#resource_scopeObject



1241
1242
1243
# File 'lib/action_dispatch/routing/mapper.rb', line 1241

def resource_scope
  controller
end

#shallow?Boolean

Returns:

  • (Boolean)


1265
1266
1267
# File 'lib/action_dispatch/routing/mapper.rb', line 1265

def shallow?
  @shallow
end

#singleton?Boolean

Returns:

  • (Boolean)


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

def singleton?; false; end

#singularObject Also known as: member_name



1229
1230
1231
# File 'lib/action_dispatch/routing/mapper.rb', line 1229

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