Sha256: 1a08348b052f8e52030cb105198ddbe5f41318f7fec9539380867992009ce6d1

Contents?: true

Size: 1.56 KB

Versions: 25

Compression:

Stored size: 1.56 KB

Contents

module Effective
  module Select2AjaxController
    extend ActiveSupport::Concern

    def respond_with_select2_ajax(collection, skip_search: false, &block)
      raise('collection should be an ActiveRecord::Relation') unless collection.kind_of?(ActiveRecord::Relation)

      # Authorize
      EffectiveResources.authorize!(self, :index, collection.klass)

      # Scope
      if collection.respond_to?(:select2_ajax)
        collection = collection.select2_ajax
      elsif collection.respond_to?(:sorted)
        collection = collection.sorted
      end

      # Search
      if (term = params[:term]).present? && !skip_search
        collection = Effective::Resource.new(collection).search_any(term, columns: [:email, :first_name, :last_name])
      end

      # Paginate
      per_page = 50
      page = (params[:page] || 1).to_i
      last = (collection.reselect(:id).count.to_f / per_page).ceil
      more = page < last

      offset = [(page - 1), 0].max * per_page
      collection = collection.limit(per_page).offset(offset)

      # Results
      results = collection.map do |resource|
        if block_given?
          option = yield(resource)
          raise('expected a Hash with id and text params') unless option.kind_of?(Hash) && option[:id] && option[:text]
          option
        else
          { id: resource.to_param, text: resource.try(:to_select2) || resource.to_s }
        end
      end

      # Respond
      respond_to do |format|
        format.js do
          render json: { results: results, pagination: { more: more } }
        end
      end
    end

  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
effective_resources-2.7.18 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.7.17 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.7.16 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.7.15 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.7.14 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.7.13 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.7.12 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.7.11 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.7.10 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.7.9 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.7.8 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.7.7 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.7.6 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.7.5 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.7.4 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.7.3 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.7.2 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.7.1 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.7.0 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.6.2 app/controllers/concerns/effective/select2_ajax_controller.rb