Module: Cms::EngineHelper

Included in:
ContentType
Defined in:
lib/cms/engine_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.decorate(instance) ⇒ Object

Add this module if its not already.


97
98
99
# File 'lib/cms/engine_helper.rb', line 97

def self.decorate(instance)
  instance.extend EngineHelper unless instance.respond_to?(:engine_name)
end

.module_name(klass) ⇒ Object

Finds the top level module for a given class. Cms::Thing -> Cms Thing -> nil Whatever::Thing -> Whatever


107
108
109
110
111
# File 'lib/cms/engine_helper.rb', line 107

def self.module_name(klass)
  names = klass.name.split("::")
  return names.first if names.size > 1
  nil
end

Instance Method Details

#engine_exists?Boolean


62
63
64
# File 'lib/cms/engine_helper.rb', line 62

def engine_exists?
  !main_app_model?
end

#engine_nameObject


66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cms/engine_helper.rb', line 66

def engine_name
  name = EngineHelper.module_name(target_class)
  return "main_app" unless name

  begin
    engine = "#{name}::Engine".constantize
  rescue NameError
    # This means there is no Engine for this model, so its from the main Rails App.
    return "main_app"
  end
  engine.engine_name
end

#main_app_model?Boolean


58
59
60
# File 'lib/cms/engine_helper.rb', line 58

def main_app_model?
  engine_name == "main_app"
end

#path_elementsObject


79
80
81
82
83
# File 'lib/cms/engine_helper.rb', line 79

def path_elements
  path = []
  path << "cms" if main_app_model?
  path << path_subject
end

#path_subjectObject

Subclasses can override this as necessary


92
93
94
# File 'lib/cms/engine_helper.rb', line 92

def path_subject
  self
end

#target_classObject

Subclasses can override this as necessary


86
87
88
89
# File 'lib/cms/engine_helper.rb', line 86

def target_class
  return self.class unless self.instance_of?(Class)
  self
end