Class: ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder

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

Overview

:nodoc:

Constant Summary collapse

CACHE =
{ 'path' => {}, 'url' => {} }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_strategy, prefix, suffix) ⇒ HelperMethodBuilder

Returns a new instance of HelperMethodBuilder.



228
229
230
231
232
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 228

def initialize(key_strategy, prefix, suffix)
  @key_strategy = key_strategy
  @prefix       = prefix
  @suffix       = suffix
end

Instance Attribute Details

#prefixObject (readonly)

Returns the value of attribute prefix.



226
227
228
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 226

def prefix
  @prefix
end

#suffixObject (readonly)

Returns the value of attribute suffix.



226
227
228
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 226

def suffix
  @suffix
end

Class Method Details

.build(action, type) ⇒ Object



175
176
177
178
179
180
181
182
183
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 175

def self.build(action, type)
  prefix = action ? "#{action}_" : ""
  suffix = type
  if action.to_s == 'new'
    HelperMethodBuilder.singular prefix, suffix
  else
    HelperMethodBuilder.plural prefix, suffix
  end
end

.get(action, type) ⇒ Object



167
168
169
170
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 167

def self.get(action, type)
  type   = type.to_s
  CACHE[type].fetch(action) { build action, type }
end

.pathObject



173
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 173

def self.path; CACHE['path'.freeze][nil]; end

.plural(prefix, suffix) ⇒ Object



189
190
191
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 189

def self.plural(prefix, suffix)
  new(->(name) { name.route_key }, prefix, suffix)
end

.polymorphic_method(recipient, record_or_hash_or_array, action, type, options) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 193

def self.polymorphic_method(recipient, record_or_hash_or_array, action, type, options)
  builder = get action, type

  case record_or_hash_or_array
  when Array
    record_or_hash_or_array = record_or_hash_or_array.compact
    if record_or_hash_or_array.empty?
      raise ArgumentError, "Nil location provided. Can't build URI."
    end
    if record_or_hash_or_array.first.is_a?(ActionDispatch::Routing::RoutesProxy)
      recipient = record_or_hash_or_array.shift
    end

    method, args = builder.handle_list record_or_hash_or_array
  when String, Symbol
    method, args = builder.handle_string record_or_hash_or_array
  when Class
    method, args = builder.handle_class record_or_hash_or_array

  when nil
    raise ArgumentError, "Nil location provided. Can't build URI."
  else
    method, args = builder.handle_model record_or_hash_or_array
  end


  if options.empty?
    recipient.send(method, *args)
  else
    recipient.send(method, *args, options)
  end
end

.singular(prefix, suffix) ⇒ Object



185
186
187
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 185

def self.singular(prefix, suffix)
  new(->(name) { name.singular_route_key }, prefix, suffix)
end

.urlObject



172
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 172

def self.url;  CACHE['url'.freeze][nil]; end

Instance Method Details

#handle_class(klass) ⇒ Object



242
243
244
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 242

def handle_class(klass)
  [get_method_for_class(klass), []]
end

#handle_class_call(target, klass) ⇒ Object



246
247
248
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 246

def handle_class_call(target, klass)
  target.send get_method_for_class klass
end

#handle_list(list) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 271

def handle_list(list)
  record_list = list.dup
  record      = record_list.pop

  args = []

  route  = record_list.map { |parent|
    case parent
    when Symbol, String
      parent.to_s
    when Class
      args << parent
      parent.model_name.singular_route_key
    else
      args << parent.to_model
      parent.to_model.model_name.singular_route_key
    end
  }

  route <<
  case record
  when Symbol, String
    record.to_s
  when Class
    @key_strategy.call record.model_name
  else
    model = record.to_model
    if model.persisted?
      args << model
      model.model_name.singular_route_key
    else
      @key_strategy.call model.model_name
    end
  end

  route << suffix

  named_route = prefix + route.join("_")
  [named_route, args]
end

#handle_model(record) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 250

def handle_model(record)
  args  = []

  model = record.to_model
  name = if model.persisted?
           args << model
           model.model_name.singular_route_key
         else
           @key_strategy.call model.model_name
         end

  named_route = prefix + "#{name}_#{suffix}"

  [named_route, args]
end

#handle_model_call(target, model) ⇒ Object



266
267
268
269
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 266

def handle_model_call(target, model)
  method, args = handle_model model
  target.send(method, *args)
end

#handle_string(record) ⇒ Object



234
235
236
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 234

def handle_string(record)
  [get_method_for_string(record), []]
end

#handle_string_call(target, str) ⇒ Object



238
239
240
# File 'lib/action_dispatch/routing/polymorphic_routes.rb', line 238

def handle_string_call(target, str)
  target.send get_method_for_string str
end