Sha256: 7a66e60bb589bbda8ffa06e3a5f5f6883e75550fb265328697c1114a5b5c34bf

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

require 'administrate/field/belongs_to'
require 'rails'

module Administrate
  module Field
    class BelongsToSearch < Administrate::Field::BelongsTo
      class Engine < ::Rails::Engine
        initializer 'administrate-field-belongs_to_search.add_assets' do |app|
          app.config.assets.precompile << 'belongs_to_search.js' if app.config.respond_to? :assets
          Administrate::Engine.add_javascript 'belongs_to_search.js' if defined?(Administrate::Engine)
        end
      end

      def associated_class
        super
      end

      def belong_to_scope_package(current_user, model)
        # model is Package
        # create a room need package reference
        model_variable = model.to_s.underscore.pluralize #=> packages
        # so we scope the current user packages
        # methode send(...) t.ly/56cI
        scoped_model = current_user.send(model_variable).pluck(:id) 
        candidates = Package.find(*scoped_model) # => array of Packages
        # but we need an activeRecordRelation and no an array 
        # let transform that array to an activeRecordRelation here t.ly/2D0A
        candidates_activeRecord = Package.where(id: candidates.map(&:id)) # => Package::ActiveRecord_Relation 

        [nil] + candidates_activeRecord.map do |resource|
          [display_candidate_resource(resource), resource.send(primary_key)]
        end

      end

      def associated_resource_options
        # candidate_resources is Package::ActiveRecord_Relation
        [nil] + candidate_resources.map do |resource|
          # resource is #=> Package
          [display_candidate_resource(resource), resource.send(primary_key)]
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
viniBaxter-spa-belongs_to_search-3.0.0 lib/viniBaxter/spa/belongs_to_search.rb