Module: Effective::Resources::Naming

Included in:
Effective::Resource
Defined in:
app/models/effective/resources/naming.rb

Constant Summary collapse

SPLIT =
/ or
/\/|::/

Instance Method Summary collapse

Instance Method Details

#class_nameObject

‘Effective::Post’



50
51
52
# File 'app/models/effective/resources/naming.rb', line 50

def class_name # 'Effective::Post'
  @model_klass ? @model_klass.name : name.classify
end

#class_pathObject

‘effective’



54
55
56
# File 'app/models/effective/resources/naming.rb', line 54

def class_path # 'effective'
  class_name.split('::')[0...-1].map { |name| name.underscore } * '/'
end

#initialized_nameObject



20
21
22
# File 'app/models/effective/resources/naming.rb', line 20

def initialized_name
  @initialized_name
end

#nameObject

‘post’



8
9
10
# File 'app/models/effective/resources/naming.rb', line 8

def name # 'post'
  @name ||= ((klass.present? ? klass.name : initialized_name).to_s.split(SPLIT).last || '').singularize.underscore
end

#namespaceObject

‘admin/things’



58
59
60
# File 'app/models/effective/resources/naming.rb', line 58

def namespace # 'admin/things'
  (namespaces.join('/') if namespaces.present?)
end

#namespacesObject

‘admin’, ‘things’


62
63
64
# File 'app/models/effective/resources/naming.rb', line 62

def namespaces # ['admin', 'things']
  @namespaces || []
end

#plural_nameObject

‘posts’



12
13
14
# File 'app/models/effective/resources/naming.rb', line 12

def plural_name # 'posts'
  name.pluralize
end

#resource_nameObject

‘effective_post’ used by permitted params



16
17
18
# File 'app/models/effective/resources/naming.rb', line 16

def resource_name # 'effective_post' used by permitted params
  @resource_name ||= ((klass.present? ? klass.name : initialized_name).to_s.split(SPLIT).join('_') || '').singularize.underscore
end

#route_nameObject

There could be a few, this is the best guess.



25
26
27
28
29
30
31
32
33
# File 'app/models/effective/resources/naming.rb', line 25

def route_name # 'post' initialized from the controller_path/initialized_name and not the class
  names = class_name.split('::')

  if names.length > 1
    Array(names[0]) + namespaces + Array(names[1..-1])
  else
    namespaces + names
  end.compact.map(&:downcase).join('/').pluralize
end

#route_name_fallbacksObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/effective/resources/naming.rb', line 35

def route_name_fallbacks
  mod = class_name.split('::').first.to_s.downcase
  admin = ('admin' if namespace.present? && namespace.include?('/admin'))

  matches = [
    route_name.singularize,
    [*namespace, plural_name].join('/'),
    [*admin, plural_name].join('/'),
    [*namespace, name].join('/'),
    [*admin, name].join('/'),
    [*mod, *namespace, plural_name].join('/'),
    [*mod, *namespace, name].join('/')
  ]
end