#-- # Copyright (c) 2012+ Damjan Rems # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #++ ######################################################################## # ######################################################################## class DcPieceRenderer include DcApplicationHelper ######################################################################## # ######################################################################## def initialize( parent, opts={} ) @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 ######################################################################### # Return code for editing this piece. ######################################################################## def link_4edit() html = '' if @opts[:edit_mode] > 1 @opts[:editparams].merge!( { table: 'dc_piece', controller: 'cmsedit', action: 'edit', id: @piece.id, title: "#{t('drgcms.edit')}: #{@piece.name}" } ) html << dc_link_for_edit( @opts[:editparams] ) end html end ######################################################################## # script renderer expects rails erb code (view) in the script field. # # Usefull when you have many designs which have lots pieces of code in common. ######################################################################## def script html = link_4edit() html << @parent.render(inline: @piece.script, layout: @opts[:layout]) end ######################################################################## # script renderer expects rails erb code (view) in the script field. # Used for designs with common code and one part which is different. # It function is similar to dc_replace_in_design method except that design # is defined in dc_piece document and not in site document. # # Example: As used in design. Backslashing < and % is important \<\% # <% part = "
\<\%= dc_render(:my_renderer, method: 'render_method') \%\>
" %> # <%= 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]','[part2]'], with: [part1, part2]) %> ######################################################################## 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 ######################################################################### # default DcPiece render method ######################################################################## def default html = link_4edit() html << @piece.body end ######################################################################### # render html ######################################################################## 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 ######################################################################## # render css ######################################################################## def render_css @piece ? "#{@piece.css}" : '' end end