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’



48
49
50
# File 'app/models/effective/resources/naming.rb', line 48

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

#class_pathObject

‘effective’



52
53
54
# File 'app/models/effective/resources/naming.rb', line 52

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

#human_nameObject



64
65
66
# File 'app/models/effective/resources/naming.rb', line 64

def human_name
  name.gsub('::', ' ').underscore.gsub('_', ' ')
end

#human_plural_nameObject



68
69
70
# File 'app/models/effective/resources/naming.rb', line 68

def human_plural_name
  name.pluralize.gsub('::', ' ').underscore.gsub('_', ' ')
end

#initialized_nameObject



18
19
20
# File 'app/models/effective/resources/naming.rb', line 18

def initialized_name
  @initialized_name
end

#nameObject

‘post’



6
7
8
# File 'app/models/effective/resources/naming.rb', line 6

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

#namespaceObject

‘admin/things’



56
57
58
# File 'app/models/effective/resources/naming.rb', line 56

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

#namespacesObject

‘admin’, ‘things’


60
61
62
# File 'app/models/effective/resources/naming.rb', line 60

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

#plural_nameObject

‘posts’



10
11
12
# File 'app/models/effective/resources/naming.rb', line 10

def plural_name # 'posts'
  name.pluralize
end

#resource_nameObject

‘effective_post’ used by permitted params



14
15
16
# File 'app/models/effective/resources/naming.rb', line 14

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.



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

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



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

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