Sha256: 576857683a080c5d8ff12fb3835b38156a0bd6b23941179b9100639159effdc5

Contents?: true

Size: 1.61 KB

Versions: 67

Compression:

Stored size: 1.61 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
        columns = collection.klass.new.try(:to_select2_search_columns).presence
        collection = Effective::Resource.new(collection).search_any(term, columns: columns)
      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

67 entries across 67 versions & 1 rubygems

Version Path
effective_resources-2.20.5 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.20.4 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.20.3 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.20.2 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.20.1 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.20.0 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.19.13 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.19.12 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.19.11 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.19.10 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.19.9 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.19.8 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.19.7 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.19.6 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.19.5 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.19.4 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.19.3 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.19.2 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.19.1 app/controllers/concerns/effective/select2_ajax_controller.rb
effective_resources-2.19.0 app/controllers/concerns/effective/select2_ajax_controller.rb