Skip to content

Commit

Permalink
rename filterable -> queryable
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffpeterson committed Nov 21, 2024
1 parent 89b432d commit 93a0c4a
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 70 deletions.
12 changes: 0 additions & 12 deletions lib/cafe_car/filterable.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/cafe_car/model.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module CafeCar::Model
extend ActiveSupport::Concern

include CafeCar::Filterable
include CafeCar::Queryable

class_methods do
def sorted(*args)
Expand Down
12 changes: 6 additions & 6 deletions lib/cafe_car/filter_builder.rb → lib/cafe_car/query_builder.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module CafeCar
class FilterBuilder
class QueryBuilder
require "activerecord_where_assoc"

attr_reader :scope
Expand Down Expand Up @@ -30,26 +30,26 @@ def association!(name, value, ...)
case value
when true then @scope.where_assoc_exists(name)
when false then @scope.where_assoc_not_exists(name)
else @scope.where_assoc_exists(name) { filtered(value, ...) }
else @scope.where_assoc_exists(name) { query(value, ...) }
end
end
end

def filter!(params = nil)
def query!(params = nil)
params.each { param!(_1, _2) } if params
self
end

def filter(...) = update!(&:all).filter!(...)
def query(...) = update!(&:all).query!(...)
end
end

# Article.filtered do
# Article.query do
# published
# user { username(/bob/) }
# user.username(/bob/)
# end
#
# Article.filtered(published: true, user: {username: /bob/})
# Article.query(published: true, user: {username: /bob/})
#
# Article.published(true).where_assoc_exists(:user) { where(username: /bob/) }
12 changes: 12 additions & 0 deletions lib/cafe_car/queryable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module CafeCar::Queryable
extend ActiveSupport::Concern

class_methods do
def query(params) = query_builder.query(params).scope
def query!(params) = query_builder.query!(params).scope

def query_builder
CafeCar::QueryBuilder.new(self)
end
end
end
12 changes: 0 additions & 12 deletions test/cafe_car/filterable_test.rb

This file was deleted.

12 changes: 12 additions & 0 deletions test/cafe_car/queryable_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require "test_helper"

class CafeCar::QueryableTest < ActiveSupport::TestCase
test "query attributes" do
assert_includes Article.query(title: "bob").to_sql, %("title" = 'bob')
end

test "query associations" do
assert_includes Article.query(author: true).to_sql, %(EXISTS)
assert_includes Article.query(author: {username: "bob"}).to_sql, %(EXISTS)
end
end
4 changes: 0 additions & 4 deletions test/dummy/test/fixtures/action_text/rich_texts.yml

This file was deleted.

9 changes: 0 additions & 9 deletions test/dummy/test/fixtures/posts.yml

This file was deleted.

7 changes: 0 additions & 7 deletions test/dummy/test/fixtures/users.yml

This file was deleted.

7 changes: 0 additions & 7 deletions test/dummy/test/models/post_test.rb

This file was deleted.

7 changes: 0 additions & 7 deletions test/dummy/test/models/user_test.rb

This file was deleted.

7 changes: 2 additions & 5 deletions test/presenters/cafe_car/presenter_test.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
require "test_helper"
# require "presenters/cafe_car/presenter"
require "cafe_car"

module CafeCar
class PresenterTest < ActionView::TestCase
tests CafeCar::Presenter

def test_find
assert_equal SymbolPresenter, Presenter.find(:hi)
test "find" do
assert_equal SymbolPresenter, Presenter.find(Symbol)
end
end
end

0 comments on commit 93a0c4a

Please sign in to comment.