app/controllers/backbone_rails_store_controller.rb
def refresh
    response = {}
    begin
      ActiveRecord::Base.transaction do

        # Prepare response for requested models
        response = {}

        # Relations to be fetched
        relations = params[:relations]

        if relations
          response[:models] = {}
          resp_relations = response[:relations] = {}

          # TODO: Optimize!
          relations.each do |model_type, model_info|
            model_class = acl_scoped_class(model_info[:railsClass], :read)
            relation_type = model_info[:relationType]
            relation_class = model_info[:railsRelationClass]
            relation_attribute = model_info[:railsRelationAttribute].underscore.to_sym
            resp_relations[model_type] = {} unless resp_relations[model_type]
            resp_relations[model_type][relation_type] = {
              :attribute => model_info[:railsRelationAttribute],
              :models => {}
            } unless resp_relations[model_type][relation_type]

            model_class.where(:id => model_info[:ids]).each do |model|
              relation_result = model.send(relation_attribute)
              next unless relation_result
              response[:models][relation_class.to_s] = [] unless response[:models][relation_class.to_s]
              if relation_result.respond_to?(:map)
                resp_relations[model_type][relation_type][:models][model.id] = relation_result.map do |rm|
                  rm.id
                end
                response[:models][relation_class.to_s].concat(relation_result)
                fill_eager_refresh relation_class, relation_result, response
              else
                resp_relations[model_type][relation_type][:models][model.id] = [relation_result.id]
                response[:models][relation_class.to_s].push(relation_result)
                fill_eager_refresh relation_class, [relation_result], response
              end
            end
          end
        end
      end

      # Models to be fetched
      models = params[:refreshModels]
      if models
        response.merge!(refresh_models(models)) do |key, v1, v2|
          v1.merge!(v2) do |key, v3, v4|
            v3.concat(v4)
          end
        end
      end

    rescue ErrorTransportException => exp
      response = {errors: exp.errors}
    end

    respond_to do |format|
      format.json { render json: response }
    end
  end
# TODO: This method should be separated in other methods (refresh, commit, destroy, search)
This method should be separated in other methods (refresh, commit, destroy, search)
This method should be separated in other methods ( refresh commit destroy search )
def refresh
    response = {}
    begin
      ActiveRecord::Base.transaction do
        response = {}
        relations = params[:relations]
        if relations
          response[:models] = {}
          resp_relations = response[:relations] = {}
          relations.each do |model_type, model_info|
            model_class = acl_scoped_class(model_info[:railsClass], :read)
            relation_type = model_info[:relationType]
            relation_class = model_info[:railsRelationClass]
            relation_attribute = model_info[:railsRelationAttribute].underscore.to_sym
            resp_relations[model_type] = {} unless resp_relations[model_type]
            resp_relations[model_type][relation_type] = {
              :attribute => model_info[:railsRelationAttribute],
              :models => {}
            } unless resp_relations[model_type][relation_type]
            model_class.where(:id => model_info[:ids]).each do |model|
              relation_result = model.send(relation_attribute)
              next unless relation_result
              response[:models][relation_class.to_s] = [] unless response[:models][relation_class.to_s]
              if relation_result.respond_to?(:map)
                resp_relations[model_type][relation_type][:models][model.id] = relation_result.map do |rm|
                  rm.id
                end
                response[:models][relation_class.to_s].concat(relation_result)
                fill_eager_refresh relation_class, relation_result, response
              else
                resp_relations[model_type][relation_type][:models][model.id] = [relation_result.id]
                response[:models][relation_class.to_s].push(relation_result)
                fill_eager_refresh relation_class, [relation_result], response
              end
            end
          end
        end
      end
      models = params[:refreshModels]
      if models
        response.merge!(refresh_models(models)) do |key, v1, v2|
          v1.merge!(v2) do |key, v3, v4|
            v3.concat(v4)
          end
        end
      end
    rescue ErrorTransportException => exp
      response = {errors: exp.errors}
    end
    respond_to do |format|
      format.json { render json: response }
    end
  end
def refresh response = { } begin ActiveRecord :: Base . transaction do response = { } relations = params [ :relations ] if relations response [ :models ] = { } resp_relations = response [ :relations ] = { } relations . each do | model_type , model_info | model_class = acl_scoped_class ( model_info [ :railsClass ] , :read ) relation_type = model_info [ :relationType ] relation_class = model_info [ :railsRelationClass ] relation_attribute = model_info [ :railsRelationAttribute ] . underscore . to_sym resp_relations [ model_type ] = { } unless resp_relations [ model_type ] resp_relations [ model_type ] [ relation_type ] = { :attribute => model_info [ :railsRelationAttribute ] , :models => { } } unless resp_relations [ model_type ] [ relation_type ] model_class . where ( :id => model_info [ :ids ] ) . each do | model | relation_result = model . send ( relation_attribute ) next unless relation_result response [ :models ] [ relation_class . to_s ] = [ ] unless response [ :models ] [ relation_class . to_s ] if relation_result . respond_to? ( :map ) resp_relations [ model_type ] [ relation_type ] [ :models ] [ model . id ] = relation_result . map do | rm | rm . id end response [ :models ] [ relation_class . to_s ] . concat ( relation_result ) fill_eager_refresh relation_class , relation_result , response else resp_relations [ model_type ] [ relation_type ] [ :models ] [ model . id ] = [ relation_result . id ] response [ :models ] [ relation_class . to_s ] . push ( relation_result ) fill_eager_refresh relation_class , [ relation_result ] , response end end end end end models = params [ :refreshModels ] if models response . merge! ( refresh_models ( models ) ) do | key , v1 , v2 | v1 . merge! ( v2 ) do | key , v3 , v4 | v3 . concat ( v4 ) end end end rescue ErrorTransportException => exp response = { errors : exp . errors } end respond_to do | format | format . json { render json : response } end end
TODO: This method should be separated in other methods (refresh, commit, destroy, search)
TODO : This method should be separated in other methods ( refresh commit destroy search )
# Prepare response for requested models # Relations to be fetched # TODO: Optimize! # Models to be fetched