def select_data_code(options = {})
      paginate = (options.key?(:paginate) ? options[:paginate] : @table.paginate?)
      # Check order
      unless @table.options.keys.include?(:order)
        columns = @table.table_columns
        @table.options[:order] = (columns.any? ? columns.first.name.to_sym : { id: :desc })
      end

      class_name = @table.model.name
      class_name = "(controller_name != '#{class_name.tableize}' ? controller_name.to_s.classify.constantize : #{class_name})" if collection?

      # Find data
      query_code = class_name.to_s
      query_code << scope_code if scope_code
      query_code << ".select(#{select_code})" if select_code
      query_code << ".from(#{from_code})" if from_code
      query_code << ".where(#{conditions_code})" unless @table.options[:conditions].blank?
      query_code << ".joins(#{@table.options[:joins].inspect})" unless @table.options[:joins].blank?
      unless includes_reflections.empty?
        expr = includes_reflections.inspect[1..-2]
        query_code << ".includes(#{expr})"
        query_code << ".references(#{expr})"
      end

      code = ''
      code << "#{query_code}\n"

      code << if @table.options[:count].present?
                "#{var_name(:count)} = #{query_code}.count(#{@table.options[:count].inspect})\n"
              else
                "#{var_name(:count)} = #{query_code}.count\n"
              end

      query_code << ".group(#{@table.options[:group].inspect})" unless @table.options[:group].blank?
      query_code << ".reorder(#{var_name(:order)})"

      if paginate
        code << "#{var_name(:limit)}  = (#{var_name(:params)}[:per_page] || 25).to_i\n"

        code << "if params[:page]\n"
        code << "  #{var_name(:page)} = (#{var_name(:params)}[:page] || 1).to_i\n"
        code << "elsif params['#{table.name}-id'] and #{var_name(:index)} = #{query_code}.pluck(:id).index(params['#{table.name}-id'].to_i)\n"
        # Find page of request element
        code << "  #{var_name(:page)} = (#{var_name(:index)}.to_f / #{var_name(:limit)}).floor + 1\n"
        code << "else\n"
        code << "  #{var_name(:page)} = 1\n"
        code << "end\n"
        code << "#{var_name(:page)}   = 1 if #{var_name(:page)} < 1\n"

        code << "#{var_name(:offset)} = (#{var_name(:page)} - 1) * #{var_name(:limit)}\n"
        code << "#{var_name(:last)}   = (#{var_name(:count)}.to_f / #{var_name(:limit)}).ceil.to_i\n"
        code << "#{var_name(:last)}   = 1 if #{var_name(:last)} < 1\n"

        code << "return #{view_method_name}(options.merge(page: 1)) if 1 > #{var_name(:page)}\n"
        code << "return #{view_method_name}(options.merge(page: #{var_name(:last)})) if #{var_name(:page)} > #{var_name(:last)}\n"
        query_code << ".offset(#{var_name(:offset)})"
        query_code << ".limit(#{var_name(:limit)})"
      end

      code << "#{records_variable_name} = #{query_code} || {}\n"
      code
    end
def select_data_code(options = {})
      paginate = (options.key?(:paginate) ? options[:paginate] : @table.paginate?)
      # Check order
      unless @table.options.keys.include?(:order)
        columns = @table.table_columns
        @table.options[:order] = (columns.any? ? columns.first.name.to_sym : { id: :desc })
      end

      class_name = @table.model.name
      class_name = "(controller_name != '#{class_name.tableize}' ? controller_name.to_s.classify.constantize : #{class_name})" if collection?

      # Find data
      query_code = class_name.to_s
      query_code << scope_code if scope_code
      query_code << ".select(#{select_code})" if select_code
      query_code << ".from(#{from_code})" if from_code
      query_code << ".where(#{conditions_code})" unless @table.options[:conditions].blank?
      query_code << ".joins(#{@table.options[:joins].inspect})" unless @table.options[:joins].blank?
      unless includes_reflections.empty?
        expr = includes_reflections.inspect[1..-2]
        query_code << ".includes(#{expr})"
        query_code << ".references(#{expr})"
      end

      code = ''
      code << "#{query_code}\n"

      code << if @table.options[:count].present?
                "#{var_name(:count)} = #{query_code}.count(#{@table.options[:count].inspect})\n"
              else
                "#{var_name(:count)} = #{query_code}.count\n"
              end

      query_code << ".group(#{@table.options[:group].inspect})" unless @table.options[:group].blank?
      query_code << ".reorder(#{var_name(:order)})"

      if paginate
        code << "#{var_name(:limit)}  = (#{var_name(:params)}[:per_page] || 25).to_i\n"

        code << "if params[:page]\n"
        code << "  #{var_name(:page)} = (#{var_name(:params)}[:page] || 1).to_i\n"
        code << "elsif params['#{table.name}-id'] and #{var_name(:index)} = #{query_code}.pluck(:id).index(params['#{table.name}-id'].to_i)\n"
        # Find page of request element
        code << "  #{var_name(:page)} = (#{var_name(:index)}.to_f / #{var_name(:limit)}).floor + 1\n"
        code << "else\n"
        code << "  #{var_name(:page)} = 1\n"
        code << "end\n"
        code << "#{var_name(:page)}   = 1 if #{var_name(:page)} < 1\n"

        code << "#{var_name(:offset)} = (#{var_name(:page)} - 1) * #{var_name(:limit)}\n"
        code << "#{var_name(:last)}   = (#{var_name(:count)}.to_f / #{var_name(:limit)}).ceil.to_i\n"
        code << "#{var_name(:last)}   = 1 if #{var_name(:last)} < 1\n"

        code << "return #{view_method_name}(options.merge(page: 1)) if 1 > #{var_name(:page)}\n"
        code << "return #{view_method_name}(options.merge(page: #{var_name(:last)})) if #{var_name(:page)} > #{var_name(:last)}\n"
        query_code << ".offset(#{var_name(:offset)})"
        query_code << ".limit(#{var_name(:limit)})"
      end

      code << "#{records_variable_name} = #{query_code} || {}\n"
      code
    end
def select_data_code ( options = { } ) paginate = ( options . key? ( :paginate ) ? options [ :paginate ] : @table . paginate? ) # Check order unless @table . options . keys . include? ( :order ) columns = @table . table_columns @table . options [ :order ] = ( columns . any? ? columns . first . name . to_sym : { id : :desc } ) end class_name = @table . model . name class_name = "(controller_name != '#{class_name.tableize}' ? controller_name.to_s.classify.constantize : #{class_name})" if collection? # Find data query_code = class_name . to_s query_code << scope_code if scope_code query_code << ".select(#{select_code})" if select_code query_code << ".from(#{from_code})" if from_code query_code << ".where(#{conditions_code})" unless @table . options [ :conditions ] . blank? query_code << ".joins(#{@table.options[:joins].inspect})" unless @table . options [ :joins ] . blank? unless includes_reflections . empty? expr = includes_reflections . inspect [ 1 .. - 2 ] query_code << ".includes(#{expr})" query_code << ".references(#{expr})" end code = '' code << "#{query_code}\n" code << if @table . options [ :count ] . present? "#{var_name(:count)} = #{query_code}.count(#{@table.options[:count].inspect})\n" else "#{var_name(:count)} = #{query_code}.count\n" end query_code << ".group(#{@table.options[:group].inspect})" unless @table . options [ :group ] . blank? query_code << ".reorder(#{var_name(:order)})" if paginate code << "#{var_name(:limit)}  = (#{var_name(:params)}[:per_page] || 25).to_i\n" code << "if params[:page]\n" code << "  #{var_name(:page)} = (#{var_name(:params)}[:page] || 1).to_i\n" code << "elsif params['#{table.name}-id'] and #{var_name(:index)} = #{query_code}.pluck(:id).index(params['#{table.name}-id'].to_i)\n" # Find page of request element code << "  #{var_name(:page)} = (#{var_name(:index)}.to_f / #{var_name(:limit)}).floor + 1\n" code << "else\n" code << "  #{var_name(:page)} = 1\n" code << "end\n" code << "#{var_name(:page)}   = 1 if #{var_name(:page)} < 1\n" code << "#{var_name(:offset)} = (#{var_name(:page)} - 1) * #{var_name(:limit)}\n" code << "#{var_name(:last)}   = (#{var_name(:count)}.to_f / #{var_name(:limit)}).ceil.to_i\n" code << "#{var_name(:last)}   = 1 if #{var_name(:last)} < 1\n" code << "return #{view_method_name}(options.merge(page: 1)) if 1 > #{var_name(:page)}\n" code << "return #{view_method_name}(options.merge(page: #{var_name(:last)})) if #{var_name(:page)} > #{var_name(:last)}\n" query_code << ".offset(#{var_name(:offset)})" query_code << ".limit(#{var_name(:limit)})" end code << "#{records_variable_name} = #{query_code} || {}\n" code end
Generate select code for the table taking all parameters in account
Generate select code for the table taking all parameters in account