Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,49 @@ end

This will generate the path for fetching as `all_options_admin_option_type_option_values(option_type_id: f.object.product.option_type_id)` (e.g. `/admin/option_types/2/option_values/all_options`)

#### Using a different selection attribute instead of ID

You can specify a different attribute instead of ID, this option is optional and by default the ID is completed:

```ruby
ActiveAdmin.register Product do
form do |f|
f.input(:category,
as: :searchable_select,
attribute_select: :value,
ajax: {
params: {
some: 'value'
}
})
end
end
```

```ruby
ActiveAdmin.register Category do
searchable_select_options(scope: lambda do |params|
Category.find_all_by_some(params[:some])
end,
attribute_select: :value,
text_attribute: :name)
end
```

#### Adding additional fields to endpoint responses

You can specify any number of model attributes (this parameter is optional):

```ruby
ActiveAdmin.register Category do
searchable_select_options(scope: lambda do |params|
Category.find_all_by_some(params[:some])
end,
additional_attributes: [:value, :short_title],
text_attribute: :name)
end
```

#### Inlining Ajax Options in Feature Tests

When writing UI driven feature specs (i.e. with Capybara),
Expand Down
26 changes: 19 additions & 7 deletions lib/activeadmin/searchable_select/option_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def initialize(name, options)
@filter = extract_filter_option(options)
@per_page = options.fetch(:per_page, 10)
@additional_payload = options.fetch(:additional_payload, {})
@additional_attributes = options.fetch(:additional_attributes, [])
@attribute_select = options.fetch(:attribute_select, :id)
end

def scope(template, params)
Expand All @@ -34,13 +36,7 @@ def collection_action_name

def as_json(template, params)
records, more = fetch_records(template, params)

results = records.map do |record|
{
id: record.id,
text: display_text(record)
}.merge(hash_of_additional_payload(record) || {})
end
results = records.map { |record| record_as_json(record).merge(hash_of_additional_payload(record) || {}) }

{ results: results, pagination: { more: more } }
end
Expand All @@ -49,6 +45,22 @@ def as_json(template, params)

attr_reader :per_page

def record_as_json(record)
item = {
id: record[@attribute_select.to_sym],
text: display_text(record)
}

@additional_attributes.each do |attr_name|
if record.class.column_names.include? attr_name.to_s
key = attr_name.to_sym
item[key] = record[key]
end
end

item
end

def fetch_records(template, params)
paginate(filter(scope(template, params), params[:term]),
params[:page])
Expand Down
17 changes: 10 additions & 7 deletions lib/activeadmin/searchable_select/select_input_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def collection_from_options

private

def attribute_select
@attribute_select ||= (options[:attribute_select].to_sym rescue :id)
end

def ajax_url
return unless options[:ajax]
[ajax_resource.route_collection_path(path_params),
Expand All @@ -67,16 +71,15 @@ def selected_value_collection
end

def option_for_record(record)
[option_collection.display_text(record), record.id]
[option_collection.display_text(record), record[attribute_select]]
end

def selected_records
@selected_records ||=
if selected_values
option_collection_scope.where(id: selected_values)
else
[]
end
@selected_records ||= if selected_values
option_collection_scope.where("#{attribute_select}": selected_values)
else
[]
end
end

def selected_values
Expand Down