Module: ActionView::LookupContext::Details

Included in:
ActionView::LookupContext
Defined in:
lib/action_view/lookup_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



145
146
147
# File 'lib/action_view/lookup_context.rb', line 145

def cache
  @cache
end

Instance Method Details

#details_keyObject

Calculate the details key. Remove the handlers from calculation to improve performance since the user cannot modify it explicitly.



149
150
151
# File 'lib/action_view/lookup_context.rb', line 149

def details_key #:nodoc:
  @details_key ||= DetailsKey.get(@details) if @cache
end

#disable_cacheObject

Temporary skip passing the details_key forward.



154
155
156
157
158
159
# File 'lib/action_view/lookup_context.rb', line 154

def disable_cache
  old_value, @cache = @cache, false
  yield
ensure
  @cache = old_value
end

#formats=(values) ⇒ Object

Overload formats= to expand [“/”] values and automatically add :html as fallback to :js.



172
173
174
175
176
177
178
# File 'lib/action_view/lookup_context.rb', line 172

def formats=(values)
  if values
    values.concat(_formats_defaults) if values.delete "*/*"
    values << :html if values == [:js]
  end
  super(values)
end

#freeze_formats(formats, unless_frozen = false) ⇒ Object

Freeze the current formats in the lookup context. By freezing them, you are guaranteeing that next template lookups are not going to modify the formats. The controller can also use this, to ensure that formats won’t be further modified (as it does in respond_to blocks).



164
165
166
167
168
# File 'lib/action_view/lookup_context.rb', line 164

def freeze_formats(formats, unless_frozen=false) #:nodoc:
  return if unless_frozen && @frozen_formats
  self.formats = formats
  @frozen_formats = true
end

#localeObject

Overload locale to return a symbol instead of array.



187
188
189
# File 'lib/action_view/lookup_context.rb', line 187

def locale
  @details[:locale].first
end

#locale=(value) ⇒ Object

Overload locale= to also set the I18n.locale. If the current I18n.config object responds to original_config, it means that it’s has a copy of the original I18n configuration and it’s acting as proxy, which we need to skip.



194
195
196
197
198
199
200
201
# File 'lib/action_view/lookup_context.rb', line 194

def locale=(value)
  if value
    config = I18n.config.respond_to?(:original_config) ? I18n.config.original_config : I18n.config
    config.locale = value
  end

  super(@skip_default_locale ? I18n.locale : _locale_defaults)
end

#skip_default_locale!Object

Do not use the default locale on template lookup.



181
182
183
184
# File 'lib/action_view/lookup_context.rb', line 181

def skip_default_locale!
  @skip_default_locale = true
  self.locale = nil
end

#update_details(new_details) ⇒ Object

Update the details keys by merging the given hash into the current details hash. If a block is given, the details are modified just during the execution of the block and reverted to the previous value after.



223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/action_view/lookup_context.rb', line 223

def update_details(new_details)
  old_details = @details.dup

  registered_detail_setters.each do |key, setter|
    send(setter, new_details[key]) if new_details.key?(key)
  end

  begin
    yield
  ensure
    @details_key = nil
    @details = old_details
  end
end

#with_layout_formatObject

A method which only uses the first format in the formats array for layout lookup. This method plays straight with instance variables for performance reasons.



205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/action_view/lookup_context.rb', line 205

def with_layout_format
  if formats.size == 1
    yield
  else
    old_formats = formats
    _set_detail(:formats, formats[0,1])

    begin
      yield
    ensure
      _set_detail(:formats, old_formats)
    end
  end
end