Skip to content

Commit

Permalink
Merge pull request #301 from stefsava/implement_filters_in_nested_select
Browse files Browse the repository at this point in the history
implement filters in nested select #300
  • Loading branch information
ldlsegovia committed May 6, 2020
2 parents a12af5e + 414bc4f commit 4d38c45
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ var initializer = function() {
var url = element.data('url');
var fields = element.data('fields');
var predicate = element.data('predicate');
var filters = element.data('filters');
var displayName = element.data('display-name');
var parent = element.data('parent');
var width = element.data('width');
Expand Down Expand Up @@ -117,6 +118,8 @@ var initializer = function() {
query.q[parent + '_eq'] = parentId;
}

Object.assign(query.q, filters);

return query;
},
processResults: function(data) {
Expand Down
1 change: 1 addition & 0 deletions app/inputs/nested_level_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def load_control_attributes
load_class(@options[:class])
load_data_attr(:fields, default: ["name"], formatter: :to_json)
load_data_attr(:predicate, default: "contains")
load_data_attr(:filters)
load_data_attr(:model, value: model_name)
load_data_attr(:display_name, default: "name")
load_data_attr(:minimum_input_length, default: 1)
Expand Down
74 changes: 74 additions & 0 deletions spec/features/inputs/nested_select_input_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,80 @@
end
end
end
context "with filters" do
before do
register_page(Country, false) {}
register_page(Region, false) {}
register_page(City, false) {}

register_form(Invoice, false) do |f|
f.input :city_id, as: :nested_select,
level_1: { attribute: :country_id },
level_2: {
attribute: :region_id,
filters: { name_contains: 'Met' }
},
level_3: { attribute: :city_id }
end

create_cities
create_invoice(city: @colina)
visit edit_admin_invoice_path(@invoice)
end

it "shows filled select controls based on defined city_id", js: true do
on_input_ctx("invoice_country_id") { expect_select2_selection("Chile") }
on_input_ctx("invoice_region_id") { expect_select2_selection("Metropolitana") }
on_input_ctx("invoice_city_id") { expect_select2_selection("Colina") }
end

context "updating medium level" do
before do
on_input_ctx("invoice_region_id") { open_select2_options }
select2_input.set("a")
end

it "shows no results based on entered text", js: true do
expect_select2_results_count_to_eq(1)
end
end
end

context "without filters" do
before do
register_page(Country, false) {}
register_page(Region, false) {}
register_page(City, false) {}

register_form(Invoice, false) do |f|
f.input :city_id, as: :nested_select,
level_1: { attribute: :country_id },
level_2: { attribute: :region_id },
level_3: { attribute: :city_id }
end

create_cities
create_invoice(city: @colina)
visit edit_admin_invoice_path(@invoice)
end

it "shows filled select controls based on defined city_id", js: true do
on_input_ctx("invoice_country_id") { expect_select2_selection("Chile") }
on_input_ctx("invoice_region_id") { expect_select2_selection("Metropolitana") }
on_input_ctx("invoice_city_id") { expect_select2_selection("Colina") }
end

context "updating medium level" do
before do
on_input_ctx("invoice_region_id") { open_select2_options }
select2_input.set("a")
end

it "shows no results based on entered text", js: true do
expect_select2_results_count_to_eq(2)
end
end
end

context "with nested resources" do
before do
Expand Down

0 comments on commit 4d38c45

Please sign in to comment.