Class: Cms::EngineAwarePathBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/cms/engine_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(model_class_or_content_type_or_model) ⇒ EngineAwarePathBuilder

Returns a new instance of EngineAwarePathBuilder.



5
6
7
8
9
10
11
12
# File 'lib/cms/engine_helper.rb', line 5

def initialize(model_class_or_content_type_or_model)
  # ContentType
  if model_class_or_content_type_or_model.respond_to? :model_class
    @path_subject = model_class_or_content_type_or_model.model_class
  else # Class or Model
    @path_subject = model_class_or_content_type_or_model
  end
end

Instance Method Details

#build(view) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/cms/engine_helper.rb', line 27

def build(view)
  path = []
  path << engine_name
  path << "cms" if main_app_model?
  path << path_subject

  path[0] = view.send(path[0]) # Replace the engine name with an actual lookup of the proper Engine routeset
  path
end

#engine_nameObject

Determine which ‘Engine’ this model is from based on the class



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cms/engine_helper.rb', line 42

def engine_name
  name = EngineHelper.module_name(subject_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

Returns:

  • (Boolean)


37
38
39
# File 'lib/cms/engine_helper.rb', line 37

def main_app_model?
  engine_name == "main_app"
end

#path_subjectObject

The object that will be added to the constructed path.



15
16
17
# File 'lib/cms/engine_helper.rb', line 15

def path_subject
  @path_subject
end

#subject_classObject



19
20
21
22
23
24
25
# File 'lib/cms/engine_helper.rb', line 19

def subject_class
  if @path_subject.instance_of?(Class)
    @path_subject
  else
    @path_subject.class
  end
end