Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

administrate-field-lazy_has_many

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

administrate-field-lazy_has_many

  • 0.1.2
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Administrate::Field::LazyHasMany

Gem Downloads Gem Version MIT license

An input field that shows search results of has many association, lazily from a custom endpoint.

Installation

Add this line to your application's Gemfile:

gem 'administrate-field-lazy_has_many'

And then execute:

$ bundle

Or install it yourself as:

$ gem install administrate-field-lazy_has_many

Usage

Controller

You need to have a route that does the following:

  1. yields an array of objects with at least the value and text you want to show i.e [{value: 1, text: option1}]
  2. Respond to search attribute
  3. Limit result result_limit

The best way is to re-use everything administrate search feature. You must define this action in administrate related entity controller

     def books_data
      result_limit = params[:result_limit] || 10
      search_term = params[:search].to_s.strip
      resources = Administrate::Search.new(scoped_resource, dashboard_class, search_term).run
      resources = resources.limit(result_limit).map do |resource|
        { value: resource.id, text: dashboard.display_resource(resource) }
      end
      if resources.blank?
        render_empty_array
      else
        render json: resources
      end
    end

Above example action that could work for every model, but you can change the implementation as per need. The only catch is it must return the comply with above conditions.

Field

Field is pretty easy and it does support all the features of administrate has_many field. You can provide all has_many options as per your use case. The only require attribute is action which must be rails path helper.

require "administrate/base_dashboard"

class PostDashboard < Administrate::BaseDashboard
  # ATTRIBUTE_TYPES
  # a hash that describes the type of each of the model's fields.
  #
  # Each different type represents an Administrate::Field object,
  # which determines how the attribute is displayed
  # on pages throughout the dashboard.
  ATTRIBUTE_TYPES = {
    id: Field::String,
    title: Field::String,
    contributors: Field::LazyHasMany.with_options(
      action: 'admin_form_contributors_data_path', result_limit: 10
    ),
    created_at: Field::DateTime,
    updated_at: Field::DateTime
  }.freeze

  # ...
end

i.e result_limit attribute maximum result value the field will post to action endpoint with search paramter

Contributing

  1. Fork it ( https://github.com/Aftab-Akram/administrate-field-lazy_has_many )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

FAQs

Package last updated on 19 Oct 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc