Skip to content

Commit 4462d91

Browse files
authored
Merge pull request #446 from matestack/add_collection_select_filter
added simple select dropdown filter to collection
2 parents d919d3f + 72f031c commit 4462d91

File tree

5 files changed

+117
-64
lines changed

5 files changed

+117
-64
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
- if options[:type] == :dropdown
2+
3+
%select{@tag_attributes}
4+
%option{disabled: true}= options[:placeholder]
5+
- if @options[:options].is_a?(Hash)
6+
- @options[:options].each do |key, value|
7+
%option{value: key}=value
8+
- if @options[:options].is_a?(Array)
9+
- @options[:options].each do |option|
10+
%option{value: option}=option
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module Matestack::Ui::Core::Collection::Filter::Select
2+
class Select < Matestack::Ui::Core::Component::Static
3+
4+
def setup
5+
if options[:type].nil?
6+
options[:type] = :dropdown #nothing else supported at the moment
7+
end
8+
@tag_attributes.merge!({
9+
"v-model#{'.number' if options[:type] == :number}": input_key,
10+
type: options[:type],
11+
class: options[:class],
12+
id: component_id,
13+
"@keyup.enter": "submitFilter()",
14+
"@change": "$forceUpdate()",
15+
ref: "filter.#{attr_key}",
16+
placeholder: options[:placeholder]
17+
})
18+
end
19+
20+
def input_key
21+
'filter["' + options[:key].to_s + '"]'
22+
end
23+
24+
def attr_key
25+
return options[:key].to_s
26+
end
27+
28+
end
29+
end

lib/matestack/ui/core/components.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def self.require_core_component(name)
5252
require_core_component "collection/filter"
5353
require_core_component "collection/filter/input"
5454
require_core_component "collection/filter/reset"
55+
require_core_component "collection/filter/select"
5556
require_core_component "collection/filter/submit"
5657
require_core_component "collection/order"
5758
require_core_component "collection/order/toggle"
@@ -168,6 +169,7 @@ def self.require_core_component(name)
168169
collection_filter: Matestack::Ui::Core::Collection::Filter::Filter,
169170
collection_filter_input: Matestack::Ui::Core::Collection::Filter::Input::Input,
170171
collection_filter_reset: Matestack::Ui::Core::Collection::Filter::Reset::Reset,
172+
collection_filter_select: Matestack::Ui::Core::Collection::Filter::Select::Select,
171173
collection_filter_submit: Matestack::Ui::Core::Collection::Filter::Submit::Submit,
172174
collection_order: Matestack::Ui::Core::Collection::Order::Order,
173175
collection_order_toggle: Matestack::Ui::Core::Collection::Order::Toggle::Toggle,

spec/0.8/components/dynamic/collection/filter_spec.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def prepare
3131
current_filter = get_collection_filter(my_collection_id)
3232
my_base_query = TestModel.all
3333
my_filtered_query = my_base_query.where("title LIKE ?", "%#{current_filter[:title]}%")
34+
my_filtered_query = my_base_query.where("title LIKE ?", "%#{current_filter[:ends_with]}") unless current_filter[:ends_with].nil?
3435
my_filtered_query = my_filtered_query.where(description: current_filter[:description]) unless current_filter[:description].nil?
3536
@my_collection = set_collection({
3637
id: my_collection_id,
@@ -46,6 +47,7 @@ def response
4647

4748
def filter
4849
collection_filter @my_collection.config do
50+
collection_filter_select id: "my-title-filter-select-ends-with", key: :ends_with, type: :dropdown, options: (1..10).to_a, placeholder: "Ends with"
4951
collection_filter_input id: "my-title-filter-input", key: :title, type: :text, placeholder: "Filter by title"
5052
collection_filter_input id: "my-description-filter-input", key: :description, type: :text, placeholder: "Filter by description"
5153
collection_filter_submit do
@@ -58,7 +60,7 @@ def filter
5860
end
5961

6062
def content
61-
async rerender_on: "my-first-collection-update" do
63+
async rerender_on: "my-first-collection-update", id: "some-unique-id" do
6264
collection_content @my_collection.config do
6365
ul do
6466
@my_collection.data.each do |dummy|
@@ -75,6 +77,7 @@ def content
7577
end
7678

7779
visit "/example"
80+
7881
# display whole range
7982
expect(page).to have_content("some-title-1")
8083
expect(page).to have_content("some-title-11")
@@ -110,6 +113,23 @@ def content
110113
expect(page).not_to have_content("some-title-1")
111114
expect(page).not_to have_content("some-title-2")
112115

116+
click_button "reset"
117+
# display whole range again
118+
expect(page).to have_content("some-title-1")
119+
expect(page).to have_content("some-title-11")
120+
121+
select "2", from: "my-title-filter-select-ends-with"
122+
click_button "filter"
123+
# display only matching result
124+
expect(page).not_to have_content("some-title-1")
125+
expect(page).to have_content("some-title-2")
126+
127+
select "1", from: "my-title-filter-select-ends-with"
128+
click_button "filter"
129+
expect(page).to have_content("some-title-1")
130+
expect(page).not_to have_content("some-title-2")
131+
132+
click_button "reset"
113133
# test persistent state
114134
fill_in "my-title-filter-input", with: "some-title-2"
115135
fill_in "my-description-filter-input", with: "some-description-2"
@@ -126,4 +146,4 @@ def content
126146

127147
end
128148

129-
end
149+
end

spec/dummy/app/matestack/demo/pages/collection.rb

Lines changed: 54 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ def prepare
99
my_base_query = DummyModel.all
1010

1111
my_filtered_query = my_base_query
12-
.where("title LIKE ?", "%#{current_filter[:title]}%")
13-
.order(current_order)
12+
13+
my_filtered_query = my_filtered_query.where("title LIKE ?", "%#{current_filter[:title]}%") if current_filter[:title].present?
14+
my_filtered_query = my_filtered_query.where("title LIKE ?", "#{current_filter[:starts_with]}%") if current_filter[:starts_with].present?
15+
16+
my_filtered_query = my_filtered_query.order(current_order)
1417

1518
@my_collection = set_collection({
1619
id: "my-first-collection",
@@ -23,92 +26,81 @@ def prepare
2326
end
2427

2528
def response
26-
components {
27-
heading size: 2, text: "Collection"
28-
29-
partial :filter
30-
partial :ordering
31-
32-
async rerender_on: "my-first-collection-update" do
33-
partial :content
34-
end
35-
}
29+
heading size: 2, text: "Collection"
30+
31+
partial :filter
32+
partial :ordering
33+
34+
async rerender_on: "my-first-collection-update", id: "my-collection" do
35+
partial :content
36+
end
3637
end
3738

3839
def filter
39-
partial {
40-
collection_filter @my_collection.config do
41-
42-
collection_filter_input key: :title, type: :text, placeholder: "Filter by Title"
43-
collection_filter_input id: "my-description-filter-input", key: :description, type: :text, placeholder: "Filter by description"
44-
collection_filter_submit do
45-
button text: "filter"
46-
end
47-
collection_filter_reset do
48-
button text: "reset"
49-
end
40+
collection_filter @my_collection.config do
5041

42+
collection_filter_select key: :starts_with, options: ("A".."Z").to_a, placeholder: "Starts with"
43+
collection_filter_input key: :title, type: :text, placeholder: "Filter by Title"
44+
collection_filter_input id: "my-description-filter-input", key: :description, type: :text, placeholder: "Filter by description"
45+
collection_filter_submit do
46+
button text: "filter"
5147
end
52-
}
48+
collection_filter_reset do
49+
button text: "reset"
50+
end
51+
52+
end
5353
end
5454

5555
def ordering
56-
partial {
57-
collection_order @my_collection.config do
58-
plain "sort by: "
59-
collection_order_toggle key: :title do
60-
button do
61-
collection_order_toggle_indicator key: :title, asc: '&#8593;', desc: '&#8595;'
62-
plain "title"
63-
end
56+
collection_order @my_collection.config do
57+
plain "sort by: "
58+
collection_order_toggle key: :title do
59+
button do
60+
collection_order_toggle_indicator key: :title, asc: '&#8593;', desc: '&#8595;'
61+
plain "title"
6462
end
6563
end
66-
}
64+
end
6765
end
6866

6967
def content
70-
partial {
71-
collection_content @my_collection.config do
72-
partial :list
73-
partial :paginator
74-
end
75-
}
68+
collection_content @my_collection.config do
69+
partial :list
70+
partial :paginator
71+
end
7672
end
7773

7874
def list
79-
partial {
80-
ul do
81-
@my_collection.paginated_data.each do |dummy|
82-
li do
83-
plain dummy.title
84-
action my_action_config(dummy.id) do
85-
button text: "delete"
86-
end
75+
ul do
76+
@my_collection.paginated_data.each do |dummy|
77+
li do
78+
plain dummy.title
79+
action my_action_config(dummy.id) do
80+
button text: "delete"
8781
end
8882
end
8983
end
90-
}
84+
end
9185
end
9286

9387
def paginator
94-
partial {
95-
plain "showing #{@my_collection.from} to #{@my_collection.to} of \
96-
#{@my_collection.filtered_count} from total #{@my_collection.base_count}"
88+
plain "showing #{@my_collection.from} to #{@my_collection.to} of \
89+
#{@my_collection.filtered_count} from total #{@my_collection.base_count}"
9790

98-
collection_content_previous do
99-
button text: "previous"
100-
end
91+
collection_content_previous do
92+
button text: "previous"
93+
end
10194

102-
@my_collection.pages.each do |page|
103-
collection_content_page_link page: page do
104-
button text: page
105-
end
95+
@my_collection.pages.each do |page|
96+
collection_content_page_link page: page do
97+
button text: page
10698
end
99+
end
107100

108-
collection_content_next do
109-
button text: "next"
110-
end
111-
}
101+
collection_content_next do
102+
button text: "next"
103+
end
112104
end
113105

114106
def my_action_config id

0 commit comments

Comments
 (0)