Class: I18n::Tasks::Scanners::PrismScanners::ParsedClass

Inherits:
Root
  • Object
show all
Defined in:
lib/i18n/tasks/scanners/prism_scanners/nodes.rb

Instance Attribute Summary collapse

Attributes inherited from Root

#calls, #children, #file_path, #node, #parent, #rails, #translation_calls

Instance Method Summary collapse

Methods inherited from Root

#add_call, #add_translation_call, #partial_view?, #rails_view?

Constructor Details

#initialize(node:, parent:, rails:) ⇒ ParsedClass

Returns a new instance of ParsedClass.



217
218
219
220
221
222
223
224
# File 'lib/i18n/tasks/scanners/prism_scanners/nodes.rb', line 217

def initialize(node:, parent:, rails:)
  @private_method = false
  @methods = []
  @private_methods = []
  @before_actions = []

  super
end

Instance Attribute Details

#private_methodObject (readonly)

Returns the value of attribute private_method.



215
216
217
# File 'lib/i18n/tasks/scanners/prism_scanners/nodes.rb', line 215

def private_method
  @private_method
end

Instance Method Details

#add_child(node) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/i18n/tasks/scanners/prism_scanners/nodes.rb', line 226

def add_child(node)
  case node
  when ParsedMethod
    if @private_method
      @private_methods << node
    else
      @methods << node
    end
  when ParsedBeforeAction
    @before_actions << node
  end

  super
end

#controller?Boolean

Returns:

  • (Boolean)


318
319
320
# File 'lib/i18n/tasks/scanners/prism_scanners/nodes.rb', line 318

def controller?
  @rails && @node.name.to_s.end_with?("Controller")
end

#mailer?Boolean

Returns:

  • (Boolean)


322
323
324
# File 'lib/i18n/tasks/scanners/prism_scanners/nodes.rb', line 322

def mailer?
  @rails && @node.name.to_s.end_with?("Mailer")
end

#pathObject



314
315
316
# File 'lib/i18n/tasks/scanners/prism_scanners/nodes.rb', line 314

def path
  (@parent&.path || []) + [path_name]
end

#path_nameObject



326
327
328
329
330
331
# File 'lib/i18n/tasks/scanners/prism_scanners/nodes.rb', line 326

def path_name
  path = @node.constant_path.full_name_parts.map { |s| s.to_s.underscore }
  path.last.delete_suffix!("_controller") if controller?

  path
end

#private_methods!Object



302
303
304
# File 'lib/i18n/tasks/scanners/prism_scanners/nodes.rb', line 302

def private_methods!
  @private_method = true
end

#processObject

rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity



241
242
243
244
245
246
247
# File 'lib/i18n/tasks/scanners/prism_scanners/nodes.rb', line 241

def process # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  if controller?
    process_controller
  else
    super
  end
end

#process_controllerObject



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
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
# File 'lib/i18n/tasks/scanners/prism_scanners/nodes.rb', line 249

def process_controller
  methods_by_name = @methods.group_by(&:name)
  private_methods_by_name = @private_methods.group_by(&:name)

  # For each before_action we need to
  # - Find which method it calls
  # - Find out which methods it applies to
  # - Calculate translation calls (and see if they are relative)
  # - Add the translation calls to the methods it applies to

  @before_actions.each do |before_action|
    before_action_name = before_action.name&.to_sym
    method_call = methods_by_name[before_action_name]&.first || private_methods_by_name[before_action_name]&.first
    translation_calls = (method_call&.translation_calls || []) + before_action.translation_calls

    # We need to handle the parent here, should not be the before_action when it is called in the method.
    @methods.each do |method|
      next unless before_action.applies_to?(method.name)

      method.add_translation_call(
        translation_calls.map do |call|
          call.with_parent(method)
        end
      )
    end
  end

  nested_calls = {}
  new_translation_calls = []

  @methods.each do |method|
    method.calls.each do |call|
      next if call.receiver.present?

      other_method = methods_by_name[call.name]&.first || private_methods_by_name[call.name]&.first
      next unless other_method

      nested_calls[method.name] ||= []
      nested_calls[method.name] << other_method.name

      if nested_calls[call.name]&.include?(method.name)
        next
      end

      other_method.translation_calls.each do |translation_call|
        new_translation_calls.push(translation_call.with_parent(method))
      end
    end
  end

  @translation_calls + @children.flat_map(&:process) + new_translation_calls
end

#support_candidate_keys?Boolean

Returns:

  • (Boolean)


310
311
312
# File 'lib/i18n/tasks/scanners/prism_scanners/nodes.rb', line 310

def support_candidate_keys?
  controller?
end

#support_relative_keys?Boolean

Returns:

  • (Boolean)


306
307
308
# File 'lib/i18n/tasks/scanners/prism_scanners/nodes.rb', line 306

def support_relative_keys?
  controller? || mailer?
end