Skip to content

Commit

Permalink
Merge pull request #46 from procore/support-class-inheritance
Browse files Browse the repository at this point in the history
[ZL-452] Support class inheritance
  • Loading branch information
njbbaer authored Mar 24, 2020
2 parents af84925 + d52dd9b commit a801267
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/procore-sift.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,31 @@ def filter_errors
def filter_validator
@_filter_validator ||= FilterValidator.build(
filters: filters,
sort_fields: self.class.sort_fields,
sort_fields: sort_fields,
filter_params: filter_params,
sort_params: sort_params,
)
end

def filters
self.class.filters
self.class.ancestors
.take_while { |klass| klass.name != "Sift" }
.flat_map { |klass| klass.try(:filters) }
.compact
.uniq { |f| [f.param, f.class] }
end

def sorts_exist?
filters.any? { |filter| filter.is_a?(Sort) }
end

def sort_fields
self.class.ancestors
.take_while { |klass| klass.name != "Sift" }
.flat_map { |klass| klass.try(:sort_fields) }
.compact
end

class_methods do
def filter_on(parameter, type:, internal_name: parameter, default: nil, validate: nil, scope_params: [])
filters << Filter.new(parameter, type, internal_name, default, validate, scope_params)
Expand Down
54 changes: 54 additions & 0 deletions test/controller_inheritance_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require "test_helper"

class PostsInheritanceTest < ActionDispatch::IntegrationTest
test "it works" do
post = Post.create!

get("/posts_alt")

json = JSON.parse(@response.body)
assert_equal 1, json.size
assert_equal(post.id, json.first["id"])
end

test "it inherits filter from parent controller" do
post = Post.create!
Post.create!

get("/posts_alt", params: { filters: { id: post.id } })

json = JSON.parse(@response.body)
assert_equal 1, json.size
assert_equal post.id, json.first["id"]
end

test "it inherits sort from parent controller" do
Post.create!(title: "z")
Post.create!(title: "a")

get("/posts_alt", params: { sort: "title" })

json = JSON.parse(@response.body, object_class: OpenStruct)
assert_equal ["a", "z"], json.map(&:title)
end

test "it overrides inherited body filter with priority filter" do
Post.create!(priority: 3)
Post.create!(priority: 1)
Post.create!(priority: 2)
get("/posts_alt", params: { filters: { body: [2, 3] } })

json = JSON.parse(@response.body, object_class: OpenStruct)
assert_equal [3, 2], json.map(&:priority)
end

test "it overrides inherited body sort with priority sort" do
Post.create!(priority: 3)
Post.create!(priority: 1)
Post.create!(priority: 2)
get("/posts_alt", params: { sort: "body" })

json = JSON.parse(@response.body, object_class: OpenStruct)
assert_equal [1, 2, 3], json.map(&:priority)
end
end
4 changes: 4 additions & 0 deletions test/dummy/app/controllers/posts_alt_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class PostsAltController < PostsController
filter_on :body, type: :int, internal_name: :priority
sort_on :body, type: :int, internal_name: :priority
end
1 change: 1 addition & 0 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
resources :posts
resources :posts_alt
end

0 comments on commit a801267

Please sign in to comment.