Module: ActionView::LookupContext::Details

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

Instance Method Summary collapse

Instance Method Details

#details_keyObject

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



135
136
137
# File 'lib/action_view/lookup_context.rb', line 135

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

#formats=(values) ⇒ Object

Overload formats= to reject [:“/”] values.



149
150
151
152
153
154
155
156
# File 'lib/action_view/lookup_context.rb', line 149

def formats=(values)
  if values && values.size == 1
    value = values.first
    values = nil    if value == :"*/*"
    values << :html if value == :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).



142
143
144
145
146
# File 'lib/action_view/lookup_context.rb', line 142

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.



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

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 i18n_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.



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

def locale=(value)
  if value
    config = I18n.config.respond_to?(:i18n_config) ? I18n.config.i18n_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.



159
160
161
162
# File 'lib/action_view/lookup_context.rb', line 159

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.



201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/action_view/lookup_context.rb', line 201

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.



183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/action_view/lookup_context.rb', line 183

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