Module: Ballast::Concerns::View

Defined in:
lib/ballast/concerns/view.rb

Overview

A concern to help view handling.

Instance Method Summary collapse

Instance Method Details

#browserBrowser

Returns an instance of the browser.

Returns:

  • (Browser)

    A browser object.



20
21
22
# 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) ⇒ Boolean

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

Parameters:

  • file (String) (defaults to: "config/supported-browsers.yml")

    The configuration file which holds the definitions.

  • root (String|NilClass) (defaults to: nil)

    The directory that contains the configuration file.

Returns:

  • (Boolean)

    true if the browser is supported, false otherwise.



29
30
31
# 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") ⇒ String|Hash

Outputs the Javascript parameters.

Parameters:

  • id (String|NilClass|FalseClass) (defaults to: nil)

    The id for the tag. If nil or false, the parameters will be returned as an hash.

  • tag (Symbol) (defaults to: :details)

    The tag to use for HTML.

  • attribute (Symbol) (defaults to: "data-jid")

    The attribute to use for the HTML element id.

Returns:

  • (String|Hash)

    Javascript parameters as HTML or as an hash.



58
59
60
61
# File 'lib/ballast/concerns/view.rb', line 58

def javascript_params(id = nil, tag: :details, attribute: "data-jid")
  initialize_view_params
  id ? (tag, @javascript_params.to_json.html_safe, attribute => id) : @javascript_params
end

#layout_params(key = nil, default_value = nil) ⇒ Object|Hash|NilClass Also known as: layout_param

Returns one or all layout parameters.

Parameters:

  • key (String|Symbol|NilClass) (defaults to: nil)

    The parameter to return. If set to nil, all the parameters will be returned as an hash.

  • default_value (Object|NilClass) (defaults to: nil)

    The default value if the parameter is not present.

Returns:

  • (Object|Hash|NilClass)

    The parameter or the entire layout parameters hash.



38
39
40
41
# 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

#scope_cssString

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

Returns:

  • (String)

    The scoped string.



13
14
15
# 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) ⇒ Object

Adds/Replaces Javascript parameters.

Parameters:

  • key (String|Symbol)

    The key of the new parameters. If nil, the root will be merged/replaced.

  • data (Hash)

    The data to add or replace.

  • replace (Boolean) (defaults to: false)

    Whether to replace existing data rather than merge.



68
69
70
71
72
73
74
75
76
77
78
79
80
# 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) ⇒ Object

Adds/Replaces layout parameters.

Parameters:

  • args (Hash)

    The parameters to add or replace.



47
48
49
50
# File 'lib/ballast/concerns/view.rb', line 47

def update_layout_params(**args)
  initialize_view_params
  @layout_params.merge!(args)
end