class ActiveRecord::Events::Naming

Attributes

field_name[R]
field_type[R]
infinitive[R]
object[R]

Public Class Methods

new(infinitive, options = {}) click to toggle source
# File lib/active_record/events/naming.rb, line 6
def initialize(infinitive, options = {})
  @infinitive = infinitive
  @object = options[:object].presence
  @field_name = options[:field_name].to_s
  @field_type = options[:field_type].try(:to_sym)
end

Public Instance Methods

action() click to toggle source
# File lib/active_record/events/naming.rb, line 29
def action
  concatenate(infinitive, object) + '!'
end
collective_action() click to toggle source
# File lib/active_record/events/naming.rb, line 37
def collective_action
  concatenate(infinitive, 'all', plural_object)
end
field() click to toggle source
# File lib/active_record/events/naming.rb, line 13
def field
  return field_name if field_name.present?

  suffix = field_type == :date ? 'on' : 'at'

  concatenate(object, past_participle, suffix)
end
inverse_predicate() click to toggle source
# File lib/active_record/events/naming.rb, line 25
def inverse_predicate
  concatenate(object, 'not', past_participle) + '?'
end
inverse_scope() click to toggle source
# File lib/active_record/events/naming.rb, line 45
def inverse_scope
  concatenate(object, 'not', past_participle)
end
predicate() click to toggle source
# File lib/active_record/events/naming.rb, line 21
def predicate
  concatenate(object, past_participle) + '?'
end
safe_action() click to toggle source
# File lib/active_record/events/naming.rb, line 33
def safe_action
  concatenate(infinitive, object)
end
scope() click to toggle source
# File lib/active_record/events/naming.rb, line 41
def scope
  concatenate(object, past_participle)
end

Private Instance Methods

concatenate(*parts) click to toggle source
# File lib/active_record/events/naming.rb, line 56
def concatenate(*parts)
  parts.compact.join('_')
end
past_participle() click to toggle source
# File lib/active_record/events/naming.rb, line 60
def past_participle
  infinitive.verb.conjugate(tense: :past, aspect: :perfective)
end
plural_object() click to toggle source
# File lib/active_record/events/naming.rb, line 64
def plural_object
  object.to_s.pluralize if object.present?
end