Sha256: 8c8bcdb2b649955cd43b83a0b3f0586c6c406b7bb41904be6010c0143b98d4f4

Contents?: true

Size: 876 Bytes

Versions: 16

Compression:

Stored size: 876 Bytes

Contents

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

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

      @records
    end

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

    def count
      @records.to_a.length
    end

    private

    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

  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
forest_liana-1.1.16 app/services/forest_liana/has_many_getter.rb
forest_liana-1.1.15 app/services/forest_liana/has_many_getter.rb
forest_liana-1.1.14 app/services/forest_liana/has_many_getter.rb
forest_liana-1.1.13 app/services/forest_liana/has_many_getter.rb
forest_liana-1.1.12 app/services/forest_liana/has_many_getter.rb
forest_liana-1.1.11 app/services/forest_liana/has_many_getter.rb
forest_liana-1.1.10 app/services/forest_liana/has_many_getter.rb
forest_liana-1.1.8 app/services/forest_liana/has_many_getter.rb
forest_liana-1.1.7 app/services/forest_liana/has_many_getter.rb
forest_liana-1.1.6 app/services/forest_liana/has_many_getter.rb
forest_liana-1.1.5 app/services/forest_liana/has_many_getter.rb
forest_liana-1.1.4 app/services/forest_liana/has_many_getter.rb
forest_liana-1.1.3 app/services/forest_liana/has_many_getter.rb
forest_liana-1.1.2 app/services/forest_liana/has_many_getter.rb
forest_liana-1.1.1 app/services/forest_liana/has_many_getter.rb
forest_liana-1.1.0 app/services/forest_liana/has_many_getter.rb