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

- (Object) add_javascript_params(key, data, replace = false)

Appends new 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.

  • replace (Boolean) (defaults to: false)

    Whether to replace existing data rather than merge.



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ballast/concerns/view.rb', line 47

def add_javascript_params(key, data, replace = false)
  @javascript_params ||= HashWithIndifferentAccess.new

  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

- (Browser) browser

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)
end

- (Boolean) browser_supported?(conf_file = nil)

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

Parameters:

  • conf_file (String) (defaults to: nil)

    The configuration file which holds the definitions.

Returns:

  • (Boolean)

    true if the browser is supported, false otherwise.



28
29
30
31
# File 'lib/ballast/concerns/view.rb', line 28

def browser_supported?(conf_file = nil)
  conf_file ||= (Rails.root + "config/supported-browsers.yml").to_s if defined?(Rails)
  browser.supported?(conf_file)
end

- (String|Hash) javascript_params(id = nil, tag = :details)

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.

Returns:

  • (String|Hash)

    Javascript parameters as HTML or the hash.



38
39
40
# File 'lib/ballast/concerns/view.rb', line 38

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

- (String) scope_css

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
  "%s %s" % [controller_path.gsub("/", "-"), action_name]
end