app/helpers/bootstrap/alert_helper.rb in bootstrap-view-helpers-0.0.11 vs app/helpers/bootstrap/alert_helper.rb in bootstrap-view-helpers-0.0.12

- old
+ new

@@ -15,10 +15,11 @@ # <li>One</li> # <li>Two</li> # </ul> # <% end %> module Bootstrap::AlertHelper + InvalidAlertTypeError = Class.new(StandardError) ALERT_ATTRIBUTES = %w(error success info block) # @overload alert(text, alert_type, options={}) # @param text [String] text of the label @@ -26,57 +27,57 @@ # @param options [Hash] unrecognized options become html attributes for returned alert <div> # @option options [String] :heading if present, include a heading in the alert # @option options [Boolean] :close if +false+, don't include a close link ('x') # @return [String] Returns html for alert def alert(*args, &block) - text = args.shift unless block_given? + body = alert_body(args, &block) options = canonicalize_options(args.extract_options!) options = ensure_class(options, 'alert') options = add_alert_classes(options, args) heading = options.delete(:heading) show_close = options.delete(:close) != false - if block_given? - content_tag(:div, options) do - alert_close(show_close) + - alert_heading(heading) + - capture(&block) - end - else - content_tag(:div, options) do - alert_close(show_close) + - alert_heading(heading) + - text - end + content_tag(:div, options) do + alert_close(show_close) + + alert_heading(heading) + + body end end # Return an alert box close button # - # @return [String] html for alert close button + # @return [String] html for alert close button unless _show_ is +false+ def alert_close(show=true) return '' unless show content_tag(:button, '&times;'.html_safe, class: 'close', data: {dismiss: 'alert'}) end # Return an alert heading # - # @return [String] html for alert heading + # @return [String] html for alert heading unless _heading_ is blank. def alert_heading(heading) return '' unless heading.present? content_tag(:h4, heading) end private + def alert_body(args, &block) + if block_given? + capture(&block) + else + args.shift + end + end + def add_alert_classes(options, alert_attributes) validate_alert_attributes(alert_attributes) classes = ['alert'] + alert_attributes.map { |e| "alert-#{e}" } ensure_class(options, classes) end def validate_alert_attributes(alert_attributes) - alert_attributes.each { |e| raise(InvalidAlertAttributeError, e.inspect) unless ALERT_ATTRIBUTES.include?(e.to_s) } + alert_attributes.each { |e| raise(InvalidAlertTypeError, e.inspect) unless ALERT_ATTRIBUTES.include?(e.to_s) } end end \ No newline at end of file