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

administrate-field-lazy_belongs_to

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_belongs_to

  • 0.2.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Administrate::Field::LazyBelongsTo

Build Status: master Gem Version MIT license

An input field that shows search results of an association, lazily.

Example

Installation

Add this line to your application's Gemfile:

gem 'administrate-field-lazy_belongs_to'

And then execute:

$ bundle

Or install it yourself as:

$ gem install administrate-field-lazy_belongs_to

Usage

This field needs a bit of setup, unless you're using XPBytes/administrate-base_controller, in order to lazily collect the values you're looking for. If you are using the BaseController, you only need to set-up the fields.

Controller

You need to have a route that yields an array of objects with at least the value and label you want to show. The best way is to re-use everything administrate offers you, which is the default behaviour of Administrate::BaseController:

class BookController < Admin::ApplicationController
  def index
    respond_to do |format|
      format.json do
        resources = Administrate::Search.new(scoped_resource, dashboard_class, params[:search].to_s.strip).run
        resources = apply_resource_includes(resources)
        resources = order.apply(resources)
        resources = resources.page(params[:page]).per(records_per_page)

        render json: resources
      end

      format.any { super }
    end
  end
end

This way, your already existing route also is available in JSON. You may optimize this by only use the id and value field: resources.to_json(fields: %i[id name]).

You could also use the dashboard display:

render json: resources.map do |resource| 
  { id: resource.id, name: dashboard.display_resource(resource) }
end

Fields

The rest of the setup is as easy as any other field, except that you need to tell the field where to query the results, which key it should use for the value (defaults to id) and the label (defaults to name).

require "administrate/base_dashboard"

class BookDashboard < 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,
    name: Field::String,
    author: Field::LazyBelongsTo.with_options(
      placeholder: 'Select the author',
      action: ->(field, q:) { field.url_helpers.admin_publisher_books_path(field.resource.publisher, search: q) },
      value_attribute: :id,
      label_attribute: :name
    ),
    created_at: Field::DateTime,
    updated_at: Field::DateTime
  }.freeze
  
  # ...
end

You get the field and the search template q as argument and named argument. By default this is {q} which is replaced with the actual query on the front-end. You can use the field.resource shortcut to scope the url; in this example above it will only show authors with the current publisher.

Options

optiontypedefaultdescription
placeholderstring'Select a %<associated_class>s'The button text if there is no current value
actionlambda -> (field, q: string)The path to get the associated
value_attributestring or symbolidThe attribute that you want to store in the field (association_id)
label_attributestring or symbolnameThe attribute that you want to show to the user (label)
sizenumber10The number of results to show at most (the rest will scroll)
  • Administrate: A Rails engine that helps you put together a super-flexible admin dashboard.
  • Administrate::BaseController: :stars: A set of application controller improvements.

Concerns

Fields

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at XPBytes/administrate-field-lazy_belongs_to.

FAQs

Package last updated on 02 May 2019

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