Sha256: 82c48e43d9545feae177121b73e6d659ec79b6d6da2f27f867c93b52960f50fe

Contents?: true

Size: 1.87 KB

Versions: 13

Compression:

Stored size: 1.87 KB

Contents

#
# The ActionController extensions
#
#
module RightRails::ControllerExtensions
  
  #
  # This method returns a wrapped RightRails scripts builder
  #
  # USAGE:
  #   def create
  #     @zing = Zing.new(params[:zing])
  #     
  #     if @zing.save
  #       flash[:notice] = 'Here you go'
  #       render rjs.insert @zing
  #     else
  #       render rjs.replace_form_for(@zing)
  #     end
  #   end
  #
  #   def alert_that
  #     render rjs.alert 'that'
  #   end
  #
  #   def as_a_block
  #     render rjs do |page|
  #       page[:zings].last.hide('slide')
  #     end
  #   end
  #
  def rjs(options={}, &block)
    @template.send(:_evaluate_assigns_and_ivars)
    
    wrapper = RenderWrapper.new(@template, options)
        
    if block_given?
      wrapper.render_block(&block)
    else
      wrapper
    end
  end
  
  #
  # This class wraps the standard JavaScript responses in the controller
  # 
  # It delegates all the script generating calls to the JavaScriptGenerator
  # instance, then grabs thre reults and creates a suitable hash of options
  # for the ActionController::Base#render method
  #
  class RenderWrapper
    def initialize(template, options)
      @template  = template
      @generator = RightRails::JavaScriptGenerator.new(template)
      @options   = options
    end
    
    def render_block(&block)
      yield(@generator)
      render
    end
    
    def method_missing(name, *args)
      @generator.send(name, *args)
      render 
    end
    
  protected
    
    #
    # Compiles the options for the controller#render method
    #
    def render
      result = {:text => @generator.to_s, :content_type => Mime::JS}
      
      # iframed uploads context overloading
      if @template.request.content_type == 'multipart/form-data'
        result.merge! :content_type => Mime::HTML, :layout => 'iframed'
      end
      
      result.merge! @options
    end
  end
  
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
right-rails-0.6.0 lib/right_rails/controller_extensions.rb
right-rails-0.5.3 lib/right_rails/controller_extensions.rb
right-rails-0.5.2 lib/right_rails/controller_extensions.rb
right-rails-0.5.1 lib/right_rails/controller_extensions.rb
right-rails-0.5.0 lib/right_rails/controller_extensions.rb
right-rails-0.4.4 lib/right_rails/controller_extensions.rb
right-rails-0.4.3 lib/right_rails/controller_extensions.rb
right-rails-0.4.2 lib/right_rails/controller_extensions.rb
right-rails-0.4.1 lib/right_rails/controller_extensions.rb
right-rails-0.4.0 lib/right_rails/controller_extensions.rb
right-rails-0.3.2 lib/right_rails/controller_extensions.rb
right-rails-0.3.1 lib/right_rails/controller_extensions.rb
right-rails-0.3.0 lib/right_rails/controller_extensions.rb