Class: Templet::ViewerCallString

Inherits:
Object
  • Object
show all
Defined in:
app/controllers/templet/viewer_call_string.rb,
lib/generators/templet/templates/core/controllers/viewer_call_string.rb

Overview

Constructs a string of Ruby that, when executed, returns a string of HTML.

It calls a method, named by the given action parameter, on an instance of the type: Templet::ViewerRest.

The resultant code statement is executed within the view context i.e. as if it’s called from the inside of an ERb partial.

It takes this circuitous route to pass various items from the controller to the view e.g. instance variables, model names, link set controls.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action, model_name, controller: nil, parent: nil, models: nil, use_model_name: nil, use_base: false, class_name: nil, module_name: nil, grand_parent: nil, variables: [], wrapper: nil) ⇒ ViewerCallString

action

The REST action, i.e. the controller method name

controller

The name without the suffix ‘Controller’, but may be prefixed with a scope/namespace

model_name

The model name, mirroring the model’s instance variable

parent

An actual instance of a model’s dependent, i.e. the belongs_to

models

A list of models, used in the index action, it names the instance variable

use_model_name

If true, no instance variable of the model is defined, e.g. in index and new

use_base

If true, it will render the view using the superclass, Templet::ApplicationRestViewer

class_name

The name of the Viewer class

module_name

The name of the Viewer module

grand_parent

The name of the parent’s parent, for ‘Back’ buttons

variables

A list of instance variables to be passed into the view (no @ prefix)

wrapper

For specifying an HTML id, i.e. the target of a JS request



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/templet/viewer_call_string.rb', line 44

def initialize(action, model_name, controller: nil,
                                   parent: nil,
                                   models: nil,
                                   use_model_name: nil,
                                   use_base: false,
                                   class_name: nil,
                                   module_name: nil,
                                   grand_parent: nil,
                                   variables: [],
                                   wrapper: nil)
  self.action = action

  self.controller = controller

  self.model_name = model_name

  self.parent = parent

  self.models = models

  # Specify a model name as opposed to an instance in an instance variable
  # It's because there is no model in the actions 'index' and 'new'
  self.use_model_name = if use_model_name.nil?
                          models ? true : false 
                        else
                          use_model_name
                        end

  self.grand_parent = grand_parent

  self.variables = variables

  self.wrapper = wrapper

  self.viewer_class = ViewerCallStringClass.new(controller, model_name,
                                                use_base,
                                                class_name, module_name)
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



14
15
16
# File 'app/controllers/templet/viewer_call_string.rb', line 14

def action
  @action
end

#controllerObject

Returns the value of attribute controller.



14
15
16
# File 'app/controllers/templet/viewer_call_string.rb', line 14

def controller
  @controller
end

#grand_parentObject

Returns the value of attribute grand_parent.



14
15
16
# File 'app/controllers/templet/viewer_call_string.rb', line 14

def grand_parent
  @grand_parent
end

#model_nameObject

Returns the value of attribute model_name.



14
15
16
# File 'app/controllers/templet/viewer_call_string.rb', line 14

def model_name
  @model_name
end

#modelsObject

Returns the value of attribute models.



14
15
16
# File 'app/controllers/templet/viewer_call_string.rb', line 14

def models
  @models
end

#parentObject

Returns the value of attribute parent.



14
15
16
# File 'app/controllers/templet/viewer_call_string.rb', line 14

def parent
  @parent
end

#use_model_nameObject

Returns the value of attribute use_model_name.



14
15
16
# File 'app/controllers/templet/viewer_call_string.rb', line 14

def use_model_name
  @use_model_name
end

#variablesObject

Returns the value of attribute variables.



14
15
16
# File 'app/controllers/templet/viewer_call_string.rb', line 14

def variables
  @variables
end

#viewer_classObject

Returns the value of attribute viewer_class.



18
19
20
# File 'app/controllers/templet/viewer_call_string.rb', line 18

def viewer_class
  @viewer_class
end

#wrapperObject

Returns the value of attribute wrapper.



14
15
16
# File 'app/controllers/templet/viewer_call_string.rb', line 14

def wrapper
  @wrapper
end

Instance Method Details

#call(action = action()) ⇒ Object Also known as: to_s



83
84
85
# File 'app/controllers/templet/viewer_call_string.rb', line 83

def call(action=action())
  "#{viewer_class}.new(#{args}#{opts}).#{action}"
end