Class: DcPieceRenderer
- Inherits:
-
Object
- Object
- DcPieceRenderer
- Includes:
- CmsCommonHelper, DcApplicationHelper
- Defined in:
- app/renderers/dc_piece_renderer.rb
Overview
Piece renderer renders data from dc_piece collection documents.
Example:
<div id="page">
<%= dc_render(:dc_piece, :name => 'some_piece') %>
</div>
Instance Attribute Summary
Attributes included from DcApplicationHelper
#design, #form, #ids, #json_ld, #menu, #menu_item, #options, #page, #page_title, #part, #parts, #record, #record_footer, #site, #tables
Instance Method Summary collapse
-
#default ⇒ Object
Default DcPiece render method.
-
#initialize(parent, opts = {}) ⇒ DcPieceRenderer
constructor
Object initialization.
-
#link_4edit ⇒ Object
Return link code for editing this piece.
-
#render_css ⇒ Object
Return CSS part of code.
-
#render_html ⇒ Object
Renderer dispatcher.
-
#script ⇒ Object
Script renderer method expects rails erb code (view) in the script field.
Methods included from CmsCommonHelper
#dc_choices4_field, #dc_choices_for_field, dc_choices_for_field, #dc_date_time, #dc_format_date_time, dc_format_date_time, #dc_format_number, dc_format_number, #dc_help_body, #dc_help_button, #dc_help_fields, #dc_help_for_tab, #dc_icon4_boolean, #dc_icon_for_boolean, #dc_name4_id, #dc_name4_value, #dc_name_for_id, #dc_name_for_value, dc_name_for_value, #dc_steps_menu_get, #t, t, #t_label_for_column, #t_label_for_field, #t_tablename
Methods included from DcApplicationHelper
#_origin, #dc_add2_record_cookie, #dc_add_json_ld, #dc_add_meta_tag, #dc_big_table, #dc_big_table_name_for_value, #dc_choices4, #dc_choices4_all_collections, #dc_choices4_folders_list, #dc_choices4_menu, #dc_choices4_site_policies, #dc_cms_menu, #dc_deprecate, #dc_document_path, #dc_dont?, #dc_edit_mode?, #dc_edit_title, #dc_error_messages_for, #dc_flash_messages, #dc_get_json_ld, #dc_get_link_canonical_tag, #dc_get_seo_meta_tags, #dc_get_site, #dc_icon_for_link, #dc_iframe_edit, #dc_img_alt, #dc_img_alt_tag, #dc_internal_var, #dc_limit_string, #dc_link_for_create, #dc_link_for_edit, #dc_link_for_edit1, #dc_link_menu_tag, #dc_link_to, #dc_menu_class, #dc_new_title, #dc_page_bottom, #dc_page_class, #dc_page_edit_menu, #dc_page_top, #dc_render, #dc_render_design, #dc_render_design_part, #dc_render_from_site, #dc_render_partial, #dc_replace_in_design, #dc_submit_tag, #dc_table_title, #dc_user_can_view, #dc_user_has_role, #dc_warning_messages_for, #decamelize_type
Constructor Details
#initialize(parent, opts = {}) ⇒ DcPieceRenderer
Object initialization. It also loads requested dc_piece document.
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'app/renderers/dc_piece_renderer.rb', line 40 def initialize( parent, opts={} ) #:nodoc: @parent = parent @opts = opts @piece = DcPiece.find(opts[:id]) if opts[:id] if @piece.nil? # alternatively find by name name = opts[:name] || opts[:id] @piece = if @opts[:site] DcPiece.find_by(name: name, site_id: dc_get_site._id) else DcPiece.find_by(name: name) end end end |
Instance Method Details
#default ⇒ Object
Default DcPiece render method.
98 99 100 101 |
# File 'app/renderers/dc_piece_renderer.rb', line 98 def default html = link_4edit() html << @piece.body end |
#link_4edit ⇒ Object
Return link code for editing this piece.
57 58 59 60 61 62 63 64 65 66 67 |
# File 'app/renderers/dc_piece_renderer.rb', line 57 def link_4edit() html = '' return html if @opts[:edit_mode] < 2 @opts[:editparams].merge!( { table: 'dc_piece', form_name: 'dc_piece', controller: 'cmsedit', action: 'edit', id: @piece.id, title: "#{t('drgcms.edit')}: #{@piece.name}" } ) html << dc_link_for_edit( @opts[:editparams] ) end |
#render_css ⇒ Object
Return CSS part of code.
115 116 117 |
# File 'app/renderers/dc_piece_renderer.rb', line 115 def render_css @piece ? "#{@piece.css}" : '' end |
#render_html ⇒ Object
Renderer dispatcher. Method returns HTML part of code.
106 107 108 109 110 |
# File 'app/renderers/dc_piece_renderer.rb', line 106 def render_html return "DcPiece #{@opts[:id]} #{@opts[:name]} not found!" unless @piece method = @opts[:method] || 'default' respond_to?(method) ? send(method) : "Error DcPiece: Method #{method} doesn't exist!" end |
#script ⇒ Object
Script renderer method expects rails erb code (view) in the script field. Used for designs with common code which can be shared and one part which is different. It’s functionality can be replaced with dc_replace_in_design method with ‘piece’ option specified.
Example: As used in design. Backslashing < and % is important <% <% part = “<div class=‘some-class’><%= dc_render(:my_renderer, method: ‘render_method’) %></div>” %> <%= dc_render(:dc_piece, id: ‘common’, method: ‘script’, replace: ‘[main]’, with: part) %>
Want to replace more than one part. Use array. <%= dc_render(:dc_piece, id: ‘common’, method: ‘script’, replace: [‘[part1]’,‘’], with: [part1, part2]) %>
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'app/renderers/dc_piece_renderer.rb', line 82 def script s = @piece.script if @opts[:replace] # replace more than one part of code if @opts[:replace].class == Array 0.upto(@opts[:replace].size - 1) {|i| s.sub!(@opts[:replace][i], @opts[:with][i])} else s.sub!(@opts[:replace], @opts[:with]) end end @parent.render(inline: s, layout: @opts[:layout]) end |