module Ballast::Concerns::View

A concern to help view handling.

Public Instance Methods

browser() click to toggle source

Returns an instance of the browser.

@return [Browser] A browser object.

# File lib/ballast/concerns/view.rb, line 20
def browser
  @browser ||= Brauser::Browser.new(request.user_agent, request.headers["Accept-Language"])
end
browser_supported?(file = "config/supported-browsers.yml", root: nil) click to toggle source

Checks if the current browser is supported according to a definition YAML file.

@param file [String] The configuration file which holds the definitions. @param root [String|NilClass] The directory that contains the configuration file. @return [Boolean] `true` if the browser is supported, `false` otherwise.

# File lib/ballast/concerns/view.rb, line 29
def browser_supported?(file = "config/supported-browsers.yml", root: nil)
  browser.supported?(((Ballast::Configuration.default_root || root) + "/" + file).to_s)
end
javascript_params(id = nil, tag: :details, attribute: "data-jid") click to toggle source

Outputs the Javascript parameters.

@param id [String|NilClass|FalseClass] The id for the tag. If `nil` or `false`, the parameters will be returned as an hash. @param tag [Symbol] The tag to use for HTML. @param attribute [Symbol] The attribute to use for the HTML element id. @return [String|Hash] Javascript parameters as HTML or as an hash.

# File lib/ballast/concerns/view.rb, line 58
def javascript_params(id = nil, tag: :details, attribute: "data-jid")
  initialize_view_params
  id ? content_tag(tag, @javascript_params.to_json.html_safe, attribute => id) : @javascript_params
end
layout_param(key = nil, default_value = nil)
Alias for: layout_params
layout_params(key = nil, default_value = nil) click to toggle source

Returns one or all layout parameters.

@param key [String|Symbol|NilClass] The parameter to return. If set to `nil`, all the parameters will be returned as an hash. @param default_value [Object|NilClass] The default value if the parameter is not present. @return [Object|Hash|NilClass] The parameter or the entire layout parameters hash.

# File lib/ballast/concerns/view.rb, line 38
def layout_params(key = nil, default_value = nil)
  initialize_view_params
  key ? @layout_params.fetch(key, default_value) : @layout_params
end
Also aliased as: layout_param
scope_css() click to toggle source

Scopes the CSS of the current page using the controller and action name.

@return [String] The scoped string.

# File lib/ballast/concerns/view.rb, line 13
def scope_css
  format("%s %s", controller_path.gsub("/", "-"), action_name)
end
update_javascript_params(key, data, replace: false) click to toggle source

Adds/Replaces Javascript parameters.

@param key [String|Symbol] The key of the new parameters. If `nil`, the root will be merged/replaced. @param data [Hash] The data to add or replace. @param replace [Boolean] Whether to replace existing data rather than merge.

# File lib/ballast/concerns/view.rb, line 68
def update_javascript_params(key, data, replace: false)
  initialize_view_params

  if key
    @javascript_params[key] = nil if replace
    @javascript_params[key] ||= {}
    @javascript_params[key].merge!(data)
  elsif replace
    @javascript_params = data.with_indifferent_access
  else
    @javascript_params.merge!(data)
  end
end
update_layout_params(**args) click to toggle source

Adds/Replaces layout parameters.

@param args [Hash] The parameters to add or replace.

# File lib/ballast/concerns/view.rb, line 47
def update_layout_params(**args)
  initialize_view_params
  @layout_params.merge!(args)
end