Skip to content

Commit a801267

Browse files
authored
Merge pull request #46 from procore/support-class-inheritance
[ZL-452] Support class inheritance
2 parents af84925 + d52dd9b commit a801267

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

lib/procore-sift.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,31 @@ def filter_errors
3939
def filter_validator
4040
@_filter_validator ||= FilterValidator.build(
4141
filters: filters,
42-
sort_fields: self.class.sort_fields,
42+
sort_fields: sort_fields,
4343
filter_params: filter_params,
4444
sort_params: sort_params,
4545
)
4646
end
4747

4848
def filters
49-
self.class.filters
49+
self.class.ancestors
50+
.take_while { |klass| klass.name != "Sift" }
51+
.flat_map { |klass| klass.try(:filters) }
52+
.compact
53+
.uniq { |f| [f.param, f.class] }
5054
end
5155

5256
def sorts_exist?
5357
filters.any? { |filter| filter.is_a?(Sort) }
5458
end
5559

60+
def sort_fields
61+
self.class.ancestors
62+
.take_while { |klass| klass.name != "Sift" }
63+
.flat_map { |klass| klass.try(:sort_fields) }
64+
.compact
65+
end
66+
5667
class_methods do
5768
def filter_on(parameter, type:, internal_name: parameter, default: nil, validate: nil, scope_params: [])
5869
filters << Filter.new(parameter, type, internal_name, default, validate, scope_params)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
require "test_helper"
2+
3+
class PostsInheritanceTest < ActionDispatch::IntegrationTest
4+
test "it works" do
5+
post = Post.create!
6+
7+
get("/posts_alt")
8+
9+
json = JSON.parse(@response.body)
10+
assert_equal 1, json.size
11+
assert_equal(post.id, json.first["id"])
12+
end
13+
14+
test "it inherits filter from parent controller" do
15+
post = Post.create!
16+
Post.create!
17+
18+
get("/posts_alt", params: { filters: { id: post.id } })
19+
20+
json = JSON.parse(@response.body)
21+
assert_equal 1, json.size
22+
assert_equal post.id, json.first["id"]
23+
end
24+
25+
test "it inherits sort from parent controller" do
26+
Post.create!(title: "z")
27+
Post.create!(title: "a")
28+
29+
get("/posts_alt", params: { sort: "title" })
30+
31+
json = JSON.parse(@response.body, object_class: OpenStruct)
32+
assert_equal ["a", "z"], json.map(&:title)
33+
end
34+
35+
test "it overrides inherited body filter with priority filter" do
36+
Post.create!(priority: 3)
37+
Post.create!(priority: 1)
38+
Post.create!(priority: 2)
39+
get("/posts_alt", params: { filters: { body: [2, 3] } })
40+
41+
json = JSON.parse(@response.body, object_class: OpenStruct)
42+
assert_equal [3, 2], json.map(&:priority)
43+
end
44+
45+
test "it overrides inherited body sort with priority sort" do
46+
Post.create!(priority: 3)
47+
Post.create!(priority: 1)
48+
Post.create!(priority: 2)
49+
get("/posts_alt", params: { sort: "body" })
50+
51+
json = JSON.parse(@response.body, object_class: OpenStruct)
52+
assert_equal [1, 2, 3], json.map(&:priority)
53+
end
54+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class PostsAltController < PostsController
2+
filter_on :body, type: :int, internal_name: :priority
3+
sort_on :body, type: :int, internal_name: :priority
4+
end

test/dummy/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Rails.application.routes.draw do
22
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
33
resources :posts
4+
resources :posts_alt
45
end

0 commit comments

Comments
 (0)