app/controllers/commons_controller_mixin.rb
def configure_search
    data = model
    
    unless current_user.is_member?
      data = model.where(customer_id: current_user.customer.id)
    end
    
    @search = data.ransack(params[:q])

    @results = @search.result(distinct: true)\
      .order(issue_date: :desc).order(id: :desc)

    # Another query without distinct to get the sums right
    # This is because the query with distinct looks like:
    # SELECT SUM(DISTINCT "commons"."net_amount") FROM "commons" INNER JOIN "items" ...
    # and does not make the right sum
    @totals = @search.result()

    if params[:tag_list].present?
      @results = @results.tagged_with(params[:tag_list].split(/\s*,\s*/))
      @totals = @totals.tagged_with(params[:tag_list].split(/\s*,\s*/))
    end

    # filter by customer
    if params[:customer_id]
      @results = @results.where customer_id: params[:customer_id]
      @totals = @totals.where customer_id: params[:customer_id]
      @customer = Customer.find params[:customer_id]
    end

    @gross = @totals.sum :gross_amount
    @net = @totals.sum :net_amount

    @count = @results.count

    # series has to be included after totals calculations
    @results = @results.includes :series
  end
# Protected: configures search
#
# Sets @search to be used with search_form_for.
#
# Returns the same value received
configures search
Sets @search to be used with search_form_for.

Returns the same value received
configures search Sets @search to be used with search_form_for . Returns the same value received
def configure_search
    data = model
    unless current_user.is_member?
      data = model.where(customer_id: current_user.customer.id)
    end
    @search = data.ransack(params[:q])
    @results = @search.result(distinct: true)\
      .order(issue_date: :desc).order(id: :desc)
    @totals = @search.result()
    if params[:tag_list].present?
      @results = @results.tagged_with(params[:tag_list].split(/\s*,\s*/))
      @totals = @totals.tagged_with(params[:tag_list].split(/\s*,\s*/))
    end
    if params[:customer_id]
      @results = @results.where customer_id: params[:customer_id]
      @totals = @totals.where customer_id: params[:customer_id]
      @customer = Customer.find params[:customer_id]
    end
    @gross = @totals.sum :gross_amount
    @net = @totals.sum :net_amount
    @count = @results.count
    @results = @results.includes :series
  end
def configure_search data = model unless current_user . is_member? data = model . where ( customer_id : current_user . customer . id ) end @search = data . ransack ( params [ :q ] ) @results = @search . result ( distinct : true ) . order ( issue_date : :desc ) . order ( id : :desc ) @totals = @search . result ( ) if params [ :tag_list ] . present? @results = @results . tagged_with ( params [ :tag_list ] . split ( / \s *, \s * / ) ) @totals = @totals . tagged_with ( params [ :tag_list ] . split ( / \s *, \s * / ) ) end if params [ :customer_id ] @results = @results . where customer_id : params [ :customer_id ] @totals = @totals . where customer_id : params [ :customer_id ] @customer = Customer . find params [ :customer_id ] end @gross = @totals . sum :gross_amount @net = @totals . sum :net_amount @count = @results . count @results = @results . includes :series end
Protected: configures search
Sets @search to be used with search_form_for.
Protected : configures search Sets @search to be used with search_form_for .
# Another query without distinct to get the sums right # This is because the query with distinct looks like: # SELECT SUM(DISTINCT "commons"."net_amount") FROM "commons" INNER JOIN "items" ... # and does not make the right sum # filter by customer # series has to be included after totals calculations