module RunSimpleRender

Constants

VERSION

Public Instance Methods

render(path= :default, args: {}, layout: false ) click to toggle source

Call any existing rendition

@param path [Symbol] Optional. Defaults to :default @param args [Hash] Optional. Referencen in the rendition as args @return [String] Html generated by the rendition

@example

User.render(:card, args:{background_picture: 'nature.jpg'})
User.render(:detail)
# File lib/run_simple_render.rb, line 17
def render(path= :default, args: {}, layout: false )
  unless path.nil?
    if path.to_s.include?('/')
      template_path = path.to_s
    else
      template_path = 'renditions/'
      template_path += "#{self.class.base_class.name}/" unless self.class.base_class.name == self.class.name
      template_path += "#{self.class.name}/#{path.to_s}"
    end
    template_path = template_path.underscore
    locals = {obj:self, args: args}
    begin
      ApplicationController.new.render_to_string(template: template_path, locals: locals, layout: false).html_safe
        #Call the parent if not let it fail
    rescue ActionView::MissingTemplate
      puts "Warning: [#{self.class.name}] did not have #{template_path} defined. Trying the parent [#{self.class.base_class.name}]"
      if path.downcase.to_s != template_path and template_path.split('/').size == 4
        template_path = template_path.split('/')
        template_path.delete_at(2)
        template_path = template_path.join('/')
        ApplicationController.new.render_to_string(template: template_path, locals: locals, layout: false).html_safe
      end
    end
  end
end