Sha256: 46149571e3bfe14d897cd6bb713fcdc2425ef377784dc72f23da84df41b123c7

Contents?: true

Size: 1.43 KB

Versions: 28

Compression:

Stored size: 1.43 KB

Contents

module ForestLiana
  class HasManyGetter
    def initialize(resource, association, params)
      @resource = resource
      @association = association
      @params = params
    end

    def perform
      @records = @resource
        .unscoped
        .find(@params[:id])
        .send(@params[:association_name])
      @records = sort_query
    end

    def records
      @records.limit(limit).offset(offset)
    end

    def count
      @records.to_a.length
    end

    private

    def association_table_name
      @resource.reflect_on_association(@params[:association_name])
        .try(:table_name)
    end

    def offset
      return 0 unless pagination?

      number = @params[:page][:number]
      if number && number.to_i > 0
        (number.to_i - 1) * limit
      else
        0
      end
    end

    def limit
      return 10 unless pagination?

      if @params[:page][:size]
        @params[:page][:size].to_i
      else
        10
      end
    end

    def pagination?
      @params[:page] && @params[:page][:number]
    end

    def sort_query
      if @params[:sort]
        field = @params[:sort]
        order = detect_sort_order(field)
        field.slice!(0) if order == :desc

        @records = @records
          .order("#{association_table_name}.#{field} #{order.upcase}")
      else
        @records
      end
    end

    def detect_sort_order(field)
      return (if field[0] == '-' then :desc else :asc end)
    end

  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
forest_liana-1.3.51 app/services/forest_liana/has_many_getter.rb
forest_liana-1.3.50 app/services/forest_liana/has_many_getter.rb
forest_liana-1.3.49 app/services/forest_liana/has_many_getter.rb
forest_liana-1.3.48 app/services/forest_liana/has_many_getter.rb
forest_liana-1.3.47 app/services/forest_liana/has_many_getter.rb
forest_liana-1.3.46 app/services/forest_liana/has_many_getter.rb
forest_liana-1.3.45 app/services/forest_liana/has_many_getter.rb
forest_liana-1.3.44 app/services/forest_liana/has_many_getter.rb
forest_liana-1.3.43 app/services/forest_liana/has_many_getter.rb
forest_liana-1.3.42 app/services/forest_liana/has_many_getter.rb
forest_liana-1.3.41 app/services/forest_liana/has_many_getter.rb
forest_liana-1.3.40 app/services/forest_liana/has_many_getter.rb
forest_liana-1.3.39 app/services/forest_liana/has_many_getter.rb
forest_liana-1.3.38 app/services/forest_liana/has_many_getter.rb
forest_liana-1.3.37 app/services/forest_liana/has_many_getter.rb
forest_liana-1.3.36 app/services/forest_liana/has_many_getter.rb
forest_liana-1.3.35 app/services/forest_liana/has_many_getter.rb
forest_liana-1.3.34 app/services/forest_liana/has_many_getter.rb
forest_liana-1.3.33 app/services/forest_liana/has_many_getter.rb
forest_liana-1.3.32 app/services/forest_liana/has_many_getter.rb