netzke / netzke-core

Framework for Sencha Ext JS + Ruby on Rails client-server components
http://netzke.org
Other
263 stars 77 forks source link

Netzke in a Hobo based Rails application #50

Closed GertThiel closed 11 years ago

GertThiel commented 11 years ago

Updated lib/netzke-core to require the hobo_support gem early so Netzke can be used in a Rails application that depends on Hobo http://hobocentral.net

Hobo includes a bunch of extensions and patches to various Rails and Ruby classes including ActiveRecord, Array and Hash in it's hobo_support gem.

To let both libraries co-exist nicely let's make sure that the Hobo extensions and patches to Rails and Ruby classes get loaded before requiring Netzke's extensions and patches.

This update should have not influence on plain Rails applications because the require statement is guarded by if defined? HoboSupport.

GertThiel commented 11 years ago

I just tested this update in a plain Rails application (w/o Hobo) to make sure that is does not harm any plain Rails application.

This update does not force anyone to use Hobo. It just allows the use of both libraries together.

mxgrn commented 11 years ago

To let both libraries co-exist nicely… ...Netzke's extensions and patches

What are exactly Netzke extensions and patches that cause problems when used with Hobo? I would rather update Netzke such that it doesn't do anything harmful.

GertThiel commented 11 years ago

Without my patch Netzke "killed" Hobo's extensions to the Array class. At least this caused exceptions in my application.

I understand that you prefer to fix Netzke's extensions instead of accepting my quite specific patch. But I investigated the conflict for about two days and could not come up with a more elegant solution.

mxgrn commented 11 years ago

I investigated the conflict for about two days and could not come up with a more elegant solution.

Could you specify which Netzke Array extension was the problem? Do you think prefixing Netzke methods in Array with "netzke_" could help? Thanks!

GertThiel commented 11 years ago

I could not identify what was the trouble maker but Netzke removed one of Hobo's extensions to the Array class (Array#member_class).

On the other hand, I would always prefix my additions to standard Rails and Ruby classes if these are not universal. Your json(ify) related methods made my afraid of possible conflicts if someone would have an API server in his every same application.

GertThiel commented 11 years ago

Would an minimal sample application help you to dive into this issue?

mxgrn commented 11 years ago

Your json(ify) related methods made my afraid of possible conflicts if someone would have an API server in his every same application.

I totally agree. Dunno why I still haven't renamed them - it's stupid to do this kind of patches on Array or Hash. Will do it as soon as I have a moment - hopefully, this will solve the problem with Hobo (although I still don't see, how Array#member_class was affected).

mxgrn commented 11 years ago

Would an minimal sample application help you to dive into this issue?

Let's start with what's obvious: those Netzke patches of core classes must be renamed. Then we'll see whether the problem is still there and if so, investigate further. Thanks!

GertThiel commented 11 years ago

I would consider writing an ActionController::Renderer for Ext JS favored JSON instead of polluting Rails and Ruby classes with something so specific.

This is some old code to do the same for SmartClient favored JSON:

Mime::Type.register_alias Mime::JSON, :isc_json                                                                                                                 

ActionController::Renderers.add :isc_json do |json, options|                                                                                                    
  include_root_in_json = ActiveRecord::Base.include_root_in_json                                                                                                
  ActiveRecord::Base.include_root_in_json = false                                                                                                               

  status = options[:status] || 0                                                                                                                                

  meta_data = { :status => status, :data => json }                                                                                                              

  meta_data.merge!({ :totalRows => options[:totalRows] }) unless options[:totalRows].nil?                                                                       
  meta_data.merge!({ :startRow  => options[:startRow]  }) unless options[:startRow].nil?                                                                        
  meta_data.merge!({ :endRow    => options[:endRow]    }) unless options[:endRow].nil?                                                                          

  response = { :response => meta_data }                                                                                                                         

  response = ActiveSupport::JSON.encode(response) unless response.respond_to?(:to_str)                                                                          
  response = "#{options[:callback]}(#{response})" unless options[:callback].blank?                                                                              

  ActiveRecord::Base.include_root_in_json = include_root_in_json                                                                                                

  self.content_type ||= Mime::JSON                                                                                                                              
  self.response_body = response                                                                                                                                 
end
``
GertThiel commented 11 years ago

This is the corresponding controller code:

class ItemsController < InheritedResources::Base                                                                                                                

  before_filter :analyse_params                                                                                                                                 

  respond_to :html, :isc_json, :json, :xml                                                                                                                      

  def index                                                                                                                                                     
    index! do |format|                                                                                                                                          
      format.isc_json { render :isc_json => collection,                                                                                                         
                                 :callback => params[:_callback],                                                                                               
                                   :start_row => @start_row,                                                                                                    
                                     :end_row => @end_row,                                                                                                      
                                       :totalRows => @total_rows }                                                                                              
    end                                                                                                                                                         
  end                                                                                                                                                           

protected                                                                                                                                                       

  def analyse_params                                                                                                                                            
    logger.debug "\n\n\n\nPARSED PARAMS\n\n#{CGI::parse(request.query_string).inspect}\n\n\n\n"                                                                 

    @start_row  = Integer(params[:_startRow])                                                                                                                   
    @end_row    = Integer(params[:_endRow])                                                                                                                     
    @total_rows = Item.count                                                                                                                                    
    @end_row    = @total_rows - 1 if @end_row >= @total_rows                                                                                                    

    @order = case params[:_sortBy]                                                                                                                              
             when /^-(.+)/ then [$1.to_sym, :desc]                                                                                                              
             when /(.+)/   then [$1.to_sym, :asc]                                                                                                               
                           else [:_id, :asc]                                                                                                                    
             end                                                                                                                                                

    @criteria = {}                                                                                                                                              
  end                                                                                                                                                           

  def collection                                                                                                                                                
    @items ||= end_of_association_chain.                                                                                                                        
                 where(@criteria).                                                                                                                              
                   order_by(@order).                                                                                                                            
                     skip(@start_row).                                                                                                                          
                       limit(@end_row - @start_row + 1)                                                                                                         
  end                                                                                                                                                           

end

Both code snippets are from a Rails 3.0.1 application prototype which I created to evaluate a SmartClient integration into my Rails business application.

mxgrn commented 11 years ago

Can you try latest master? I brought extension of core Ruby classes to a minimum and prefixed the remaining injected methods with "netzke_".

GertThiel commented 11 years ago

Congratulations! My Netzke components now perform nicely in my Hobo based application.

GertThiel commented 11 years ago

How updates netzke-communitypack? The latest update killed Netzke::Communitypack::ActionColumn for example.

mxgrn commented 11 years ago

Concerning the ActionColumn specifically: you may want to check out the code of nomadcoder/yanit @ github, or be more specific about your issue. Concerning Community-pack in general: I think I'm going to get rid of it, as releasing so much code (especially not written by myself) is too difficult. I'm thinking of decomposing it into several gems. ActionColumn will find its way back to Basepack, probably.

mxgrn commented 11 years ago

Congratulations! My Netzke components now perform nicely in my Hobo based application.

Glad to hear that, closing the issue, thanks for your time!