Class: ViewModels::RenderOptions::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/view_models/render_options.rb

Overview

Base class for Partial and Template.

Direct Known Subclasses

Partial, Template

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Base) initialize(prefix, name, options)

Initialize Render Options

Parameters:

  • prefix (String)

    The prefix for rendering

  • name (String)

    The template name

  • options (Hash)

    The render options as a hash



20
21
22
23
24
25
# File 'lib/view_models/render_options.rb', line 20

def initialize prefix, name, options
  @prefix = prefix
  @options = options
  self.template_name = deoptionize name
  @format = @options.delete :format
end

Instance Attribute Details

- (Object) file

The different attributes used to generate render options



13
14
15
# File 'lib/view_models/render_options.rb', line 13

def file
  @file
end

- (Object) format

The different attributes used to generate render options



13
14
15
# File 'lib/view_models/render_options.rb', line 13

def format
  @format
end

- (Object) name

The different attributes used to generate render options



13
14
15
# File 'lib/view_models/render_options.rb', line 13

def name
  @name
end

- (Object) path

The different attributes used to generate render options



13
14
15
# File 'lib/view_models/render_options.rb', line 13

def path
  @path
end

- (Object) prefix

The different attributes used to generate render options



13
14
15
# File 'lib/view_models/render_options.rb', line 13

def prefix
  @prefix
end

- (Object) view_model

The different attributes used to generate render options



13
14
15
# File 'lib/view_models/render_options.rb', line 13

def view_model
  @view_model
end

Instance Method Details

- (String) error_format

The format when trying to render

Returns:

  • (String)

    the render format



44
45
46
47
# File 'lib/view_models/render_options.rb', line 44

def error_format
  format = self.format
  format ? "format #{format}" : "default format"
end

- (Object) error_message

Generate a suitable error message for the error options.



29
30
31
# File 'lib/view_models/render_options.rb', line 29

def error_message
  "'#{error_path}#{name}' with #{error_format}"
end

- (String) error_path

The error path, always returns a string

Returns:

  • (String)

    The error path



36
37
38
39
# File 'lib/view_models/render_options.rb', line 36

def error_path
  path = self.path
  path ? "#{path}/" : ""
end

- (Object) format!(view)

TODO:

Rails specific.

Parameters:

  • view (ActionView)

    The view to render



61
62
63
# File 'lib/view_models/render_options.rb', line 61

def format! view
  view.template_format = @format if @format
end

- (Object) path_key

Used for caching.



67
68
69
# File 'lib/view_models/render_options.rb', line 67

def path_key
  [self.path, self.name, self.format]
end

- (Hash) to_render_options

Used when rendering.

Returns:

  • (Hash)

    The render options



52
53
54
55
56
# File 'lib/view_models/render_options.rb', line 52

def to_render_options
  @options[:locals] ||= {}
  @options[:locals].reverse_merge! :view_model => view_model
  @options.reverse_merge :file => file
end