Module: ActiveComponent
- Included in:
- ActionView::Base, Base
- Defined in:
- lib/active_component.rb,
lib/active_component/base.rb,
lib/active_component/config.rb,
lib/active_component/template_handler.rb
Defined Under Namespace
Modules: Config Classes: ActiveComponentError, Base, InvalidHtmlError, Railtie, TemplateHandler
Constant Summary collapse
- HTML5_ELEMENTS =
{ :meta => [:base, :command, :link, :meta, :noscript, :script, :style, :title], :flow => [:a, :abbr, :address, :article, :aside, :audio, :b, :bdo, :blockquote, :br, :button, :canvas, :cite, :code, :command, :datalist, :del, :details, :dfn, :div, :dl, :em, :embed, :fieldset, :figure, :footer, :form, :h1, :h2, :h3, :h4, :h5, :h6, :header, :hgroup, :hr, :i, :iframe, :img, :input, :ins, :kbd, :keygen, :label, :map, :mark, :math, :menu, :meter, :nav, :noscript, :object, :ol, :output, :p, :pre, :progress, :q, :ruby, :samp, :script, :section, :select, :small, :span, :strong, :sub, :sup, :svg, :table, :textarea, :time, :ul, :var, :video, :wbr], :sectioning => [:article, :aside, :nav, :section], :heading => [:h1, :h2, :h3, :h4, :h5, :h6, :hgroup], :phrasing => [:abbr, :audio, :b, :bdo, :br, :button, :canvas, :cite, :code, :command, :datalist, :dfn, :em, :embed, :i, :iframe, :img, :input, :kbd, :keygen, :label, :mark, :math, :meter, :noscript, :object, :output, :progress, :q, :ruby, :samp, :script, :select, :small, :span, :strong, :sub, :sup, :svg, :textarea, :time, :var, :video, :wbr], :embedded => [:audio, :canvas, :embed, :iframe, :img, :math, :object, :svg, :video], :interactive => [:a, :button, :details, :embed, :iframe, :keygen, :label, :select, :textarea], :sectioning_roots => [:blockquote, :body, :details, :fieldset, :figure, :td], :form_associated => [:button, :fieldset, :input, :keygen, :label, :meter, :object, :output, :progress, :select, :textarea], :block_candidates => [:section, :nav, :article, :aside, :h1, :h2, :h3, :h4, :h5, :h6, :hgroup, :header, :footer, :address, :p, :pre, :blockquote, :div], :uncategorized => [:col, :colgroup, :dd, :dt, :figcaption, :head, :html, :legend, :li, :optgroup, :option, :param, :rp, :rt, :source, :summary, :tbody, :tfoot, :th, :thead, :tr] }
- EMPTY_ELEMENTS =
[:area, :base, :br, :col, :command, :embed, :hr, :img, :input, :keygen, :link, :meta, :param, :source, :wbr]
- PHRASING_ELEMENTS =
HTML5_ELEMENTS[:phrasing] - HTML5_ELEMENTS[:interactive] - HTML5_ELEMENTS[:embedded] - EMPTY_ELEMENTS - [:noscript, :time] + [:ins, :del]
- BLOCK_ELEMENTS =
HTML5_ELEMENTS[:block_candidates] - HTML5_ELEMENTS[:sectioning] - HTML5_ELEMENTS[:sectioning_roots] - HTML5_ELEMENTS[:heading] - [:p, :pre] + [:head, :html, :hgroup]
- SECTION_ELEMENTS =
- HEADING_ELEMENTS =
HTML5_ELEMENTS[:heading] - [:hgroup]
Instance Method Summary collapse
-
#print_contents(tag, content_or_contents, method_or_methods = nil, *flags_and_attributes) ⇒ Object
Generates a collection of tags wrapping content that is optionally printed using method(s).
-
#print_object(object, method = nil) ⇒ Object
Prints a single object, optionally using a method.
-
#print_tag(name, *rest) ⇒ Object
Wraps haml_tag and directly captures the output buffer product.
-
#wrap_contents(tag, content_or_contents, method_or_methods = nil, *flags_and_attributes) ⇒ Object
Wraps content(s) into a single tag, optionally using a method.
Instance Method Details
#print_contents(tag, content_or_contents, method_or_methods = nil, *flags_and_attributes) ⇒ Object
Generates a collection of tags wrapping content that is optionally printed using method(s)
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/active_component.rb', line 54 def print_contents(tag, content_or_contents, method_or_methods = nil, *flags_and_attributes) flags = [] attributes = {} # Collect all flags (non-Hash) and attributes (by merging all Hashs) for arg in flags_and_attributes arg.is_a?(Hash) ? attributes.merge!(arg) : flags << arg end # Create a callable printing procedure for the case # that its whole output should be wrapped with a tag printing_procedure = Proc.new do unless method_or_methods.present? # Print content(s) without using methods content_or_contents.transmogrify do |content| if flags.include? :wrap_whole_content # Write printed object to buffer (without tag) write_to_buffer print_object(content) else # Wrap printed object with a tag and write result to buffer tag_to_buffer(tag, print_object(content), attributes) end end else unless flags.include? :couple_methods_with_contents # Print content(s) using (fixed set of) method(s) content_or_contents.transmogrify do |content| method_or_methods.transmogrify do |method| if flags.include? :wrap_whole_content # Write printed object to buffer (without tag) write_to_buffer print_object(content, method) else # Wrap printed object with a tag and write result to buffer tag_to_buffer(tag, print_object(content, method), attributes) end end end else # Print contents using individually paired methods content_or_contents.transmogrify_with_index do |content, index| method = method_or_methods[index] if flags.include? :wrap_whole_content # Write printed object to buffer (without tag) write_to_buffer print_object(content, method) else # Wrap printed objects with a tag and write result to buffer tag_to_buffer(tag, print_object(content, method), attributes) end end end end end if flags.include? :wrap_whole_content # Wrap output of printing procedure with tag and write result to buffer tag_to_buffer(tag, attributes, &printing_procedure) else # Call printing procedure and write result to buffer printing_procedure.call end # Return buffer content buffer end |
#print_object(object, method = nil) ⇒ Object
Prints a single object, optionally using a method
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/active_component.rb', line 141 def print_object(object, method = nil) #logger = RAILS_DEFAULT_LOGGER #logger.info "\"print_object speaking. I am about to print Object: " + object.inspect + " Method: " + method.inspect + ". Over.\"" unless method.present? if object.respond_to? :call begin object.call.to_s # Haml buffers may be provided in callable form, but have to be captured rescue Haml::Error # Rescue is only successful if buffer available in current scope print_buffer { object.call } end else object.to_s # Each object responds to :to_s end else # If the given method can be invoked on the object, the result is returned if method.respond_to?(:to_sym) && object.respond_to?(method) object.send(method.to_sym).to_s # If the given method can be alled with the object, the result is returned elsif method.respond_to? :call # Call method with object if it takes at most 1 required parameter # Arity returns -n-1 if n > 0 optional parameters exist if method.arity == 1 || method.arity == -1 || method.arity == -2 method.call(object).to_s else raise ArgumentError, "Content is not printable. Too many (or no) parameters expected in the following method: " + method.inspect end else raise ArgumentError, "Content is not printable. Provide a Proc/Method that can be called with object or a method name that can be invoked on the object. Alternatively, do not provide a method argument so that the object's :to_html, :call, or :to_s method is called. Parameters given: Object: " + object.inspect + " Method: " + method.inspect end end end |
#print_tag(name, *flags, attributes = {}) ⇒ Object #print_tag(name, text, *flags, attributes = {}) ⇒ Object
Wraps haml_tag and directly captures the output buffer product. This should only be used if a single haml_tag
should be captured. Note that capturing buffer content should be done as rare as possible for performance reasons. For non-trivial content you might want to use ‘print_buffer { tag_to_buffer(:ul) { tag_to_buffer(:li, content) } }` instead.
135 136 137 138 |
# File 'lib/active_component.rb', line 135 def print_tag(name, *rest) puts "warning: print_tag does not except blocks. Use print_buffer { tag_to_buffer(:ul) { tag_to_buffer(:li, content) } } instead" if block_given? print_buffer { tag(name, *rest) } end |
#wrap_contents(tag, content_or_contents, method_or_methods = nil, *flags_and_attributes) ⇒ Object
Wraps content(s) into a single tag, optionally using a method
119 120 121 |
# File 'lib/active_component.rb', line 119 def wrap_contents(tag, content_or_contents, method_or_methods = nil, *flags_and_attributes) print_contents(tag, content_or_contents, method_or_methods, :wrap_whole_content, *flags_and_attributes) end |