lib/ballast/concerns/view.rb in ballast-1.6.0 vs lib/ballast/concerns/view.rb in ballast-1.7.0
- old
+ new
@@ -28,26 +28,46 @@
def browser_supported?(conf_file = nil)
conf_file ||= (Rails.root + "config/supported-browsers.yml").to_s if defined?(Rails)
browser.supported?(conf_file)
end
+ # 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] The default value if the parameter is not present.
+ # @return [Object|Hash] The parameter or the entire layout parameters hash.
+ def layout_params(key = nil, default_value = nil)
+ initialize_view_params
+ key ? @layout_params.fetch(key, default_value) : @layout_params
+ end
+ alias_method :layout_param, :layout_params
+
+ # Adds/Replaces layout parameters.
+ #
+ # @param args [Hash] The new parameters to add.
+ def set_layout_params(**args)
+ initialize_view_params
+ @layout_params.merge!(args)
+ end
+
# 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.
# @return [String|Hash] Javascript parameters as HTML or the hash.
def javascript_params(id = nil, tag = :details)
+ initialize_view_params
id ? content_tag(tag, @javascript_params.to_json.html_safe, "data-jid" => id): @javascript_params
end
# Appends new 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.
# @param replace [Boolean] Whether to replace existing data rather than merge.
def add_javascript_params(key, data, replace = false)
- @javascript_params ||= HashWithIndifferentAccess.new
+ initialize_view_params
if key
@javascript_params[key] = nil if replace
@javascript_params[key] ||= {}
@javascript_params[key].merge!(data)
@@ -55,8 +75,15 @@
@javascript_params = data.with_indifferent_access
else
@javascript_params.merge!(data)
end
end
+
+ private
+ # Prepares parameters for views.
+ def initialize_view_params
+ @layout_params ||= HashWithIndifferentAccess.new
+ @javascript_params ||= HashWithIndifferentAccess.new
+ end
end
end
end
\ No newline at end of file