GithubHelp home page GithubHelp logo

cselmer / administrate-field-lazy_belongs_to Goto Github PK

View Code? Open in Web Editor NEW

This project forked from xpbytes/administrate-field-lazy_belongs_to

0.0 1.0 0.0 42 KB

:pencil: A belongs to-like field that lazily loads candidates from a custom endpoint.

License: MIT License

Ruby 32.55% JavaScript 30.45% CSS 11.15% HTML 25.84%

administrate-field-lazy_belongs_to's Introduction

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

option type default description
placeholder string 'Select a %<associated_class>s' The button text if there is no current value
action lambda -> (field, q: string) The path to get the associated
value_attribute string or symbol id The attribute that you want to store in the field (association_id)
label_attribute string or symbol name The attribute that you want to show to the user (label)
size number 10 The number of results to show at most (the rest will scroll)

Related

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.

administrate-field-lazy_belongs_to's People

Contributors

cselmer avatar sleeplessbyte avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.