Sha256: ba206e9acf1509f78f9700242db87bbdaaecb08fcfee6f3f68032e7ac280255b

Contents?: true

Size: 1.21 KB

Versions: 5

Compression:

Stored size: 1.21 KB

Contents

module Rafters::ComponentContext
  extend ActiveSupport::Concern

  included do
    attr_accessor :rendered_components
    helper_method :rendered_components
    helper_method :render_component
    alias_method_chain :render, :component
  end

  def component_attributes(name, settings = {})
    component = component(name, settings)
    component.as_json
  end

  def render_component(name, settings = {}, template_name = nil)
    component = component(name, settings)
    component_renderer.render(component, template_name)
  end

  def render_with_component(*args, &block)
    if params[:component]
      component, settings = params[:component], params[:settings]

      respond_to do |format|
        format.html { render_without_component(text: render_component(component, settings)) }
        format.json { render_without_component(json: component_attributes(component, settings)) }
      end
    else
      render_without_component(*args, &block)
    end
  end

  private

  def component_renderer
    @_component_renderer ||= Rafters::ComponentRenderer.new(self)
  end

  def component(name, settings = {})
    component_klass = "#{name}_component".classify.constantize
    component = component_klass.new(settings)
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rafters-0.2.0 lib/rafters/component_context.rb
rafters-0.1.3 lib/rafters/component_context.rb
rafters-0.1.2 lib/rafters/component_context.rb
rafters-0.1.1 lib/rafters/component_context.rb
rafters-0.1.0 lib/rafters/component_context.rb