Sha256: 41339d2f601ebbe361d8111fba484bf1e46b39e3ddfff44f81d59ced00543142

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

module JellyController
  protected
  include Jelly::Common

  def jelly_callback(callback_base_name = @action_name, options = {}, &block)
    raw_jelly_callback(options) do
      arguments = block.try(:call) || []
      arguments = [arguments] unless arguments.is_a?(Array)
      jelly_callback_hash("on_#{callback_base_name}", *arguments).merge(options)
    end
  end

  def raw_jelly_callback(options={}, &block)
    options.symbolize_keys!
    options[:format] ||= if request.xhr?
      :json
    elsif params[:callback]
      :jsonp
    else
      :iframe
    end
    render :inline => jelly_callback_erb(options, &block)
  end

  def jelly_callback_erb(options={}, &block)
    options[:format] ||= :json
    @jelly_block = block
    case options[:format].to_sym
      when :iframe
        "<textarea>#{jelly_callback_erb_template}</textarea>"
      when :jsonp
        @jsonp_callback = params[:callback]
        jelly_callback_erb_template
      else
        jelly_callback_erb_template
    end
  end

  def jelly_callback_erb_template
    <<-ERB
      <%= begin
        json = instance_eval(&@jelly_block).to_json
        @jsonp_callback ? "\#{@jsonp_callback}(\#{json});" : json
      end %>
    ERB
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
honkster-jelly-0.8.2 lib/jelly/jelly_controller.rb