Class: ViewDelegates::ViewDelegate

Inherits:
Object
  • Object
show all
Defined in:
lib/view_delegates/poros/view_delegate.rb

Overview

Base class for delegates

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view_data = {}) ⇒ ViewDelegate

Initialize method

Parameters:

  • view_data (Hash) (defaults to: {})

    hash containing all delegate properties



24
25
26
27
28
29
30
31
# File 'lib/view_delegates/poros/view_delegate.rb', line 24

def initialize(view_data = {})
  self.class.ar_models&.each do |t|
    send("#{t}=", view_data[t]) if view_data[t]
  end
  self.class.properties&.each do |t|
    send("#{t}=", view_data[t]) if view_data[t]
  end
end

Class Attribute Details

.delegate_cacheViewDelegates::Cache

View delegate cache system



14
15
16
# File 'lib/view_delegates/poros/view_delegate.rb', line 14

def delegate_cache
  @delegate_cache
end

.polymorph_functionObject

Returns the value of attribute polymorph_function.



5
6
7
# File 'lib/view_delegates/poros/view_delegate.rb', line 5

def polymorph_function
  @polymorph_function
end

Instance Method Details

#render(view, local_params: {}, &block) ⇒ Object

Renders as a string the view passed as params

Parameters:

  • view (Symbol)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/view_delegates/poros/view_delegate.rb', line 35

def render(view, local_params: {}, &block)
  locals = {}
  self.class.view_locals&.each do |method|
    locals[method] = send(method)
  end
  self.ar_models = {}
  self.class.ar_models&.each do |ar_model|
    self.ar_models[ar_model] = instance_variable_get(:"@#{ar_model}")
  end
  self.class.properties&.each do |property|
    locals[property] = instance_variable_get "@#{property}"
  end
  locals = locals.merge(self.ar_models).merge(local_params)
  result = ViewDelegateController.render(self.class.view_path + '/' + view.to_s,
                                         locals: locals)

  if block
    block.call(result)
  else
    result
  end
end