Class: Props::PartialRenderer

Inherits:
ActionView::AbstractRenderer
  • Object
show all
Defined in:
lib/props_template/extensions/partial_renderer.rb

Constant Summary collapse

OPTION_AS_ERROR_MESSAGE =
"The value (%s) of the option `as` is not a valid Ruby identifier; " \
"make sure it starts with lowercase letter, " \
"and is followed by any combination of letters, numbers and underscores."
IDENTIFIER_ERROR_MESSAGE =
"The partial name (%s) is not a valid Ruby identifier; " \
"make sure your partial name starts with underscore."
INVALID_PARTIAL_MESSAGE =
"The partial name must be a string, but received (%s)."

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, options) ⇒ PartialRenderer

Returns a new instance of PartialRenderer.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/props_template/extensions/partial_renderer.rb', line 124

def initialize(context, options)
  @context = context
  super(@context.lookup_context)
  @options = options.merge(formats: [:json])
  @options.delete(:handlers)
  @details = extract_details(@options)

  partial = @options[:partial]

  if !(String === partial)
    raise_invalid_partial(partial.inspect)
  end

  @path = partial
  @context_prefix = @lookup_context.prefixes.first
  template_keys = retrieve_template_keys(@options)
  @template = find_template(@path, template_keys)
end

Instance Attribute Details

#templateObject (readonly)

Returns the value of attribute template.



122
123
124
# File 'lib/props_template/extensions/partial_renderer.rb', line 122

def template
  @template
end

Class Method Details

.find_and_add_template(builder, context, all_options) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/props_template/extensions/partial_renderer.rb', line 60

def self.find_and_add_template(builder, context, all_options)
  first_opts = all_options[0]

  if first_opts[:partial]
    partial_opts = block_opts_to_render_opts(builder, first_opts)
    renderer = new(context, partial_opts)

    all_options.map do |opts|
      opts[:_template] = renderer.template
      opts
    end
  else
    all_options
  end
end

.raise_invalid_identifier(path) ⇒ Object

Raises:

  • (ArgumentError)


80
81
82
# File 'lib/props_template/extensions/partial_renderer.rb', line 80

def self.raise_invalid_identifier(path)
  raise ArgumentError.new(IDENTIFIER_ERROR_MESSAGE % (path))
end

.raise_invalid_option_as(as) ⇒ Object

Raises:

  • (ArgumentError)


76
77
78
# File 'lib/props_template/extensions/partial_renderer.rb', line 76

def self.raise_invalid_option_as(as)
  raise ArgumentError.new(OPTION_AS_ERROR_MESSAGE % (as))
end

.refine_options(options, item = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/props_template/extensions/partial_renderer.rb', line 91

def self.refine_options(options, item = nil)
  return options if !options[:partial]

  partial, rest = [*options[:partial]]
  rest = (rest || {}).clone
  locals = rest[:locals] || {}
  rest[:locals] = locals

  if item
    as = if !rest[:as]
      retrieve_variable(partial)
    else
      rest[:as].to_sym
    end

    raise_invalid_option_as(as) unless /\A[a-z_]\w*\z/.match?(as.to_s)

    locals[as] = item

    if fragment_name = rest[:fragment]
      fragment_name = Proc === fragment_name ? fragment_name.call(item) : fragment_name.to_s
      rest[:fragment] = fragment_name
    end
  end

  pass_opts = options.clone
  pass_opts[:partial] = [partial, rest]

  pass_opts
end

.retrieve_variable(path) ⇒ Object



85
86
87
88
89
# File 'lib/props_template/extensions/partial_renderer.rb', line 85

def self.retrieve_variable(path)
  base = path[-1] == "/" ? "" : File.basename(path)
  raise_invalid_identifier(path) unless base =~ /\A_?(.*?)(?:\.\w+)*\z/
  $1.to_sym
end

Instance Method Details

#render(template, options) ⇒ Object



143
144
145
146
147
# File 'lib/props_template/extensions/partial_renderer.rb', line 143

def render(template, options)
  #remove this later

  render_partial(template, @context, @options)
end