Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
administrate-field-lazy_belongs_to
Advanced tools
An input field that shows search results of an association, lazily.
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
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.
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
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.
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) |
Administrate
: A Rails engine that helps you put together a super-flexible admin dashboard.Administrate::BaseController
: :stars: A set of application controller improvements.Administrate::DefaultOrder
: :1234: Sets the default order for a resource in a administrate controller.Administrate::SerializedFields
: :ab: Automatically deserialize administrate fields on form submit.Administrate::Field::Code
: :pencil: A text
field that shows code.Administrate::Field::Hyperlink
: :pencil: A string
field that is shows a hyperlink.Adminisrtate::Field::JsonEditor
: :pencil: A text
field that shows a JSON editor.Administrate::Field::ScopedBelongsTo
: :pencil: A belongs_to
field that yields itself to the scope lambda
.Administrate::Field::ScopedHasMany
: :pencil: A has_many
field that yields itself to the scope lambda
.Administrate::Field::TimeAgo
: :pencil: A date_time
field that shows its data as time_ago
since.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.
Bug reports and pull requests are welcome on GitHub at XPBytes/administrate-field-lazy_belongs_to.
FAQs
Unknown package
We found that administrate-field-lazy_belongs_to demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.