Skip to content

Commit 17a8bec

Browse files
author
Ivanov-Anton
committed
Improve build path for ajax request
To prevent error in build path for ajax request when we have namespaced classes solves #29 issue
1 parent 4d3fbcc commit 17a8bec

File tree

4 files changed

+113
-38
lines changed

4 files changed

+113
-38
lines changed

lib/activeadmin/searchable_select/select_input_extension.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,21 @@ def collection_from_options
4545

4646
def ajax_url
4747
return unless options[:ajax]
48-
template.polymorphic_path([template.active_admin_namespace.route_prefix, ajax_resource_class],
48+
template.polymorphic_path([active_admin_prefix, resource_route_name || ajax_resource_class],
4949
action: option_collection.collection_action_name,
5050
**ajax_params)
5151
end
5252

53+
def active_admin_prefix
54+
template.active_admin_namespace.route_prefix
55+
end
56+
57+
def resource_route_name
58+
active_admin_namespace = template.active_admin_namespace
59+
str = active_admin_namespace.resource_for(ajax_resource_class)&.route_collection_path
60+
str&.split('/')&.last
61+
end
62+
5363
def all_options_collection
5464
option_collection_scope.all.map do |record|
5565
option_for_record(record)

spec/features/end_to_end_spec.rb

Lines changed: 77 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,60 +5,100 @@
55
require 'support/active_admin_helpers'
66

77
RSpec.describe 'end to end', type: :feature, js: true do
8-
before(:each) do
9-
ActiveAdminHelpers.setup do
10-
ActiveAdmin.register(Category) do
11-
searchable_select_options(scope: Category, text_attribute: :name)
12-
end
8+
context 'class name without namespaces' do
9+
before(:each) do
10+
ActiveAdminHelpers.setup do
11+
ActiveAdmin.register(Category) do
12+
searchable_select_options(scope: Category, text_attribute: :name)
13+
end
1314

14-
ActiveAdmin.register(Post) do
15-
filter(:category, as: :searchable_select, ajax: true)
15+
ActiveAdmin.register(Post) do
16+
filter(:category, as: :searchable_select, ajax: true)
1617

17-
form do |f|
18-
f.input(:category, as: :searchable_select, ajax: true)
18+
form do |f|
19+
f.input(:category, as: :searchable_select, ajax: true)
20+
end
1921
end
20-
end
2122

22-
ActiveAdmin.setup {}
23+
ActiveAdmin.setup {}
24+
end
2325
end
24-
end
2526

26-
describe 'index page with searchable select filter' do
27-
it 'loads filter input options' do
28-
Category.create(name: 'Music')
29-
Category.create(name: 'Travel')
27+
describe 'index page with searchable select filter' do
28+
it 'loads filter input options' do
29+
Category.create(name: 'Music')
30+
Category.create(name: 'Travel')
3031

31-
visit '/admin/posts'
32+
visit '/admin/posts'
3233

33-
expand_select_box
34-
wait_for_ajax
34+
expand_select_box
35+
wait_for_ajax
3536

36-
expect(select_box_items).to eq(%w(Music Travel))
37-
end
37+
expect(select_box_items).to eq(%w(Music Travel))
38+
end
3839

39-
it 'allows filtering options by term' do
40-
Category.create(name: 'Music')
41-
Category.create(name: 'Travel')
40+
it 'allows filtering options by term' do
41+
Category.create(name: 'Music')
42+
Category.create(name: 'Travel')
4243

43-
visit '/admin/posts'
44+
visit '/admin/posts'
4445

45-
expand_select_box
46-
enter_search_term('T')
47-
wait_for_ajax
46+
expand_select_box
47+
enter_search_term('T')
48+
wait_for_ajax
4849

49-
expect(select_box_items).to eq(%w(Travel))
50+
expect(select_box_items).to eq(%w(Travel))
51+
end
52+
53+
it 'loads more items when scrolling down' do
54+
15.times { |i| Category.create(name: "Category #{i}") }
55+
visit '/admin/posts'
56+
57+
expand_select_box
58+
wait_for_ajax
59+
scroll_select_box_list
60+
wait_for_ajax
61+
62+
expect(select_box_items.size).to eq(15)
63+
end
5064
end
65+
end
5166

52-
it 'loads more items when scrolling down' do
53-
15.times { |i| Category.create(name: "Category #{i}") }
54-
visit '/admin/posts'
67+
context 'class name with namespace' do
68+
before(:each) do
69+
ActiveAdminHelpers.setup do
70+
ActiveAdmin.register RGB::Color, as: 'color' do
71+
searchable_select_options scope: RGB::Color, text_attribute: :code
72+
end
73+
74+
ActiveAdmin.register Internal::TagName, as: 'Tag Name' do
75+
filter :color, as: :searchable_select, ajax: { resource: 'RGB::Color' }
76+
end
77+
78+
ActiveAdmin::SearchableSelect.inline_ajax_options = true
79+
ActiveAdmin.setup {}
80+
end
81+
end
5582

56-
expand_select_box
57-
wait_for_ajax
58-
scroll_select_box_list
59-
wait_for_ajax
83+
describe 'index page with searchable select filter' do
84+
let!(:orange) { RGB::Color.create(code: '#eac112', description: 'Orange') }
85+
let(:filters_options) { %w(Any #19bf25 #eac112) }
6086

61-
expect(select_box_items.size).to eq(15)
87+
let!(:other_records) do
88+
RGB::Color.create(code: '#19bf25', description: 'Green')
89+
Internal::TagName.create(name: 'Important Guest', color: orange)
90+
end
91+
92+
before { visit '/admin/tag_names' }
93+
94+
it 'should have all created colors in filter' do
95+
expect(page).to have_select :Color, options: filters_options
96+
end
97+
98+
it 'should have selected option' do
99+
select orange.code, from: :Color
100+
expect(page).to have_select :Color, selected: orange.code
101+
end
62102
end
63103
end
64104

spec/internal/db/schema.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@
2020
t.boolean :published
2121
end
2222

23+
create_table(:rgb_colors, force: true) do |t|
24+
t.string :code
25+
t.text :description
26+
end
27+
28+
create_table(:internal_tag_names, force: true) do |t|
29+
t.string :name
30+
t.text :description
31+
t.integer :color_id
32+
end
33+
2334
create_table(:users, force: true) do |t|
2435
t.string :name
2536
end

spec/support/models.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ class Post < ActiveRecord::Base
1313
scope(:published, -> { where(published: true) })
1414
end
1515

16+
module RGB
17+
class Color < ActiveRecord::Base
18+
self.table_name = :rgb_colors
19+
has_many :tags, class_name: 'Internal::TagName'
20+
end
21+
end
22+
23+
module Internal
24+
class TagName < ActiveRecord::Base
25+
self.table_name = :internal_tag_names
26+
belongs_to :color, class_name: 'RGB::Color', foreign_key: :color_id
27+
end
28+
end
29+
1630
RSpec.configure do |config|
1731
config.after do
1832
DatabaseCleaner.strategy = :truncation

0 commit comments

Comments
 (0)