facet 0.16.1 → 0.16.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/facet/base.rb +14 -0
- data/lib/facet/concerns/cache.rb +16 -0
- data/lib/facet/concerns/collection.rb +45 -0
- data/lib/facet/concerns/core.rb +33 -4
- data/lib/facet/concerns/filter.rb +31 -0
- data/lib/facet/concerns/paginate.rb +43 -0
- data/lib/facet/concerns/record.rb +37 -0
- data/lib/facet/concerns/sort.rb +31 -0
- data/lib/facet/version.rb +1 -1
- data/lib/facet.rb +3 -0
- metadata +26 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 981aad54986ddaa928792065efcb8b95e4eb5271138ec2d864f9cd88275501d2
|
4
|
+
data.tar.gz: e05973e52d09014e166755cc7fc9b700bb2dcca51191022fabaa4934ce3d1d7e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a668f00be7c584694c176c3548ae921513d860129a5ee4409ca71353382c3b12bfbaa704deda2a36bf5db37d260bbdca4dc5979ce30a294011867df1bd9ab8fa
|
7
|
+
data.tar.gz: c9a0b9bddceca2562b93988a25609f4215e9fa4a2534cf7af7f07026916d23c4f7d923a0e6670c01e2e1e9df0a34b319bdf64e2760fe0313f86e56c7b58b8f8b
|
data/lib/facet/base.rb
CHANGED
@@ -1,9 +1,23 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative "concerns/record"
|
4
|
+
require_relative "concerns/paginate"
|
5
|
+
require_relative "concerns/filter"
|
6
|
+
require_relative "concerns/sort"
|
3
7
|
require_relative "concerns/core"
|
8
|
+
require_relative "concerns/collection"
|
9
|
+
require_relative "concerns/cache"
|
4
10
|
|
5
11
|
module Facet
|
6
12
|
class Base
|
13
|
+
include ShortCircuIt
|
14
|
+
|
15
|
+
include Facet::Record
|
16
|
+
include Facet::Paginate
|
17
|
+
include Facet::Filter
|
18
|
+
include Facet::Sort
|
7
19
|
include Facet::Core
|
20
|
+
include Facet::Collection
|
21
|
+
include Facet::Cache
|
8
22
|
end
|
9
23
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Facets are intended to work with Rails view (russian doll) caching which requires responding to `#cache_key`
|
4
|
+
module Facet
|
5
|
+
module Cache
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
memoize :cache_key
|
10
|
+
end
|
11
|
+
|
12
|
+
def cache_key
|
13
|
+
[ collection, current_page, filter_by, sort_by ]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Facets output the paginated filtered and sorted collection of data they represent
|
4
|
+
module Facet
|
5
|
+
module Collection
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
memoize :collection
|
10
|
+
memoize :filtered_collection
|
11
|
+
memoize :sorted_filtered_collection
|
12
|
+
|
13
|
+
# Supports transparent rendering with Rails of a facet in place of a collection
|
14
|
+
delegate :to_model, :to_partial_path, to: :collection
|
15
|
+
end
|
16
|
+
|
17
|
+
def collection
|
18
|
+
record_class.public_send(record_scope)
|
19
|
+
end
|
20
|
+
|
21
|
+
def output
|
22
|
+
return sorted_filtered_collection unless current_page.present? && current_page >= 0
|
23
|
+
|
24
|
+
sorted_filtered_collection.paginate(page: current_page)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Template rendering checks for this method to exist and calls it to get the actual data to render
|
28
|
+
alias_method :to_ary, :output
|
29
|
+
alias_method :to_a, :to_ary
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def sorted_filtered_collection
|
34
|
+
return filtered_collection unless sorted?
|
35
|
+
|
36
|
+
filtered_collection.public_send(sort_by)
|
37
|
+
end
|
38
|
+
|
39
|
+
def filtered_collection
|
40
|
+
return collection unless filtered?
|
41
|
+
|
42
|
+
collection.public_send(filter_by)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/facet/concerns/core.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
# Facets are the filter/sort/pagination parameters that represent a subset of an ActiveRecord collection.
|
3
4
|
module Facet
|
4
5
|
module Core
|
5
6
|
extend ActiveSupport::Concern
|
@@ -8,10 +9,38 @@ module Facet
|
|
8
9
|
attr_reader :current_page, :filter_by, :sort_by
|
9
10
|
end
|
10
11
|
|
11
|
-
def initialize(current_page:
|
12
|
-
@current_page = current_page
|
13
|
-
@filter_by = filter_by
|
14
|
-
@sort_by = sort_by
|
12
|
+
def initialize(current_page: nil, filter_by: nil, sort_by: nil, all: false, paginate: true)
|
13
|
+
@current_page = page_for(current_page, paginate)
|
14
|
+
@filter_by = filter_for(filter_by, all)
|
15
|
+
@sort_by = sort_for(sort_by)
|
16
|
+
end
|
17
|
+
|
18
|
+
def paginated?
|
19
|
+
current_page.present?
|
20
|
+
end
|
21
|
+
|
22
|
+
def filtered?
|
23
|
+
filter_by.present?
|
24
|
+
end
|
25
|
+
|
26
|
+
def sorted?
|
27
|
+
sort_by.present?
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def page_for(current_page, paginate)
|
33
|
+
return current_page.presence || default_page if paginate && pagination?
|
34
|
+
|
35
|
+
raise ArgumentError, "pagination is disabled for this facet." if current_page.present?
|
36
|
+
end
|
37
|
+
|
38
|
+
def filter_for(filter_by, all)
|
39
|
+
filter_by.presence || ((default_filter? && !all) ? default_filter : nil)
|
40
|
+
end
|
41
|
+
|
42
|
+
def sort_for(sort_by)
|
43
|
+
sort_by.presence || (default_sort? ? default_sort : nil)
|
15
44
|
end
|
16
45
|
end
|
17
46
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Filters allow you to pare down a collection of records into a meaningful subset.
|
4
|
+
module Facet
|
5
|
+
module Filter
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
delegate :default_filter, :default_filter?, to: :class
|
10
|
+
end
|
11
|
+
|
12
|
+
class_methods do
|
13
|
+
attr_reader :default_filter
|
14
|
+
|
15
|
+
def inherited(base)
|
16
|
+
base.__send__(:filter_default, default_filter) if default_filter?
|
17
|
+
super
|
18
|
+
end
|
19
|
+
|
20
|
+
def default_filter?
|
21
|
+
default_filter.present?
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def filter_default(value)
|
27
|
+
@default_filter = value
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Pagination allows you to segment records into a renderable subset.
|
4
|
+
module Facet
|
5
|
+
module Paginate
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
delegate :default_page, :default_page?, :pagination?, to: :class
|
10
|
+
end
|
11
|
+
|
12
|
+
class_methods do
|
13
|
+
def default_page
|
14
|
+
default_page? ? @default_page : 1
|
15
|
+
end
|
16
|
+
|
17
|
+
def inherited(base)
|
18
|
+
base.__send__(:page_default, @default_page) if default_page?
|
19
|
+
super
|
20
|
+
end
|
21
|
+
|
22
|
+
def default_page?
|
23
|
+
@default_page.present?
|
24
|
+
end
|
25
|
+
|
26
|
+
def pagination?
|
27
|
+
return true unless defined?(@default_page)
|
28
|
+
|
29
|
+
@default_page >= 1
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def disable_pagination!
|
35
|
+
page_default(-1)
|
36
|
+
end
|
37
|
+
|
38
|
+
def page_default(value)
|
39
|
+
@default_page = value
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Records are the collection source, with an optional scope that is always applied before filters.
|
4
|
+
module Facet
|
5
|
+
module Record
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
delegate :record_class, :record_scope, to: :class
|
10
|
+
end
|
11
|
+
|
12
|
+
class_methods do
|
13
|
+
def record_class
|
14
|
+
name.chomp("Facet").constantize
|
15
|
+
end
|
16
|
+
|
17
|
+
def record_scope
|
18
|
+
scoped? ? @record_scope : :all
|
19
|
+
end
|
20
|
+
|
21
|
+
def scoped?
|
22
|
+
@record_scope.present?
|
23
|
+
end
|
24
|
+
|
25
|
+
def inherited(base)
|
26
|
+
base.__send__(:scope, @record_scope) if scoped?
|
27
|
+
super
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def scope(value)
|
33
|
+
@record_scope = value
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Sorting allow you to control the display order of a collection or subset of records.
|
4
|
+
module Facet
|
5
|
+
module Sort
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
delegate :default_sort, :default_sort?, to: :class
|
10
|
+
end
|
11
|
+
|
12
|
+
class_methods do
|
13
|
+
attr_reader :default_sort
|
14
|
+
|
15
|
+
def inherited(base)
|
16
|
+
base.__send__(:sort_default, default_sort) if default_sort?
|
17
|
+
super
|
18
|
+
end
|
19
|
+
|
20
|
+
def default_sort?
|
21
|
+
default_sort.present?
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def sort_default(value)
|
27
|
+
@default_sort = value
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/facet/version.rb
CHANGED
data/lib/facet.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: facet
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.16.
|
4
|
+
version: 0.16.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eric Garside
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-08-
|
11
|
+
date: 2019-08-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -30,14 +30,28 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - '='
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 0.16.
|
33
|
+
version: 0.16.2
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - '='
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 0.16.
|
40
|
+
version: 0.16.2
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: short_circu_it
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.16.2
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.16.2
|
41
55
|
description: Create cacheable collections of filtered, sorted, and paginated ActiveRecord
|
42
56
|
objects
|
43
57
|
email:
|
@@ -50,7 +64,13 @@ files:
|
|
50
64
|
- README.md
|
51
65
|
- lib/facet.rb
|
52
66
|
- lib/facet/base.rb
|
67
|
+
- lib/facet/concerns/cache.rb
|
68
|
+
- lib/facet/concerns/collection.rb
|
53
69
|
- lib/facet/concerns/core.rb
|
70
|
+
- lib/facet/concerns/filter.rb
|
71
|
+
- lib/facet/concerns/paginate.rb
|
72
|
+
- lib/facet/concerns/record.rb
|
73
|
+
- lib/facet/concerns/sort.rb
|
54
74
|
- lib/facet/version.rb
|
55
75
|
homepage: https://github.com/Freshly/spicerack/tree/master/facet
|
56
76
|
licenses:
|
@@ -74,7 +94,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
74
94
|
- !ruby/object:Gem::Version
|
75
95
|
version: '0'
|
76
96
|
requirements: []
|
77
|
-
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 2.7.6
|
78
99
|
signing_key:
|
79
100
|
specification_version: 4
|
80
101
|
summary: A filterable, sortable, pageable, and Rails cacheable ActiveRecord::Relation
|