Class: Tito::Admin::CollectionProxy
- Inherits:
-
Object
- Object
- Tito::Admin::CollectionProxy
- Includes:
- Enumerable
- Defined in:
- lib/tito/admin/collection_proxy.rb
Instance Method Summary collapse
-
#build(**attrs) ⇒ Object
– Mutation helpers –.
- #create(**attrs) ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #expand(*fields) ⇒ Object
-
#find(id_or_slug) ⇒ Object
– Reading –.
-
#initialize(scope:, path_suffix: nil) ⇒ CollectionProxy
constructor
A new instance of CollectionProxy.
- #order(**fields) ⇒ Object
- #page(number) ⇒ Object
- #per(size) ⇒ Object
- #search(term) ⇒ Object
- #size ⇒ Object (also: #count, #length)
- #total_pages ⇒ Object
-
#where(**conditions) ⇒ Object
– Chainable query methods (return new proxy) –.
Constructor Details
#initialize(scope:, path_suffix: nil) ⇒ CollectionProxy
Returns a new instance of CollectionProxy.
6 7 8 9 10 11 12 |
# File 'lib/tito/admin/collection_proxy.rb', line 6 def initialize(scope:, path_suffix: nil) @scope = scope @path_suffix = path_suffix @query = QueryBuilder.new @loaded_pages = {} @meta = nil end |
Instance Method Details
#build(**attrs) ⇒ Object
– Mutation helpers –
81 82 83 |
# File 'lib/tito/admin/collection_proxy.rb', line 81 def build(**attrs) @scope.resource_class.new(_scope: @scope, **attrs) end |
#create(**attrs) ⇒ Object
85 86 87 |
# File 'lib/tito/admin/collection_proxy.rb', line 85 def create(**attrs) build(**attrs).tap(&:save) end |
#each(&block) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/tito/admin/collection_proxy.rb', line 48 def each(&block) return enum_for(:each) unless block_given? page_number = @query.page_number || 1 loop do page_data = fetch_page(page_number) records = page_data[@scope.resource_class.collection_name] || [] records.each { |attrs| block.call(instantiate(attrs)) } = page_data["meta"] || {} break if ["next_page"].nil? page_number = ["next_page"] end end |
#empty? ⇒ Boolean
75 76 77 |
# File 'lib/tito/admin/collection_proxy.rb', line 75 def empty? size == 0 end |
#expand(*fields) ⇒ Object
28 29 30 |
# File 'lib/tito/admin/collection_proxy.rb', line 28 def (*fields) dup_with { |q| q.(fields) } end |
#find(id_or_slug) ⇒ Object
– Reading –
42 43 44 45 46 |
# File 'lib/tito/admin/collection_proxy.rb', line 42 def find(id_or_slug) params = @query. data = @scope.request(:get, @scope.member_path(id_or_slug), params: params) instantiate(data[@scope.resource_class.resource_name] || data) end |
#order(**fields) ⇒ Object
20 21 22 |
# File 'lib/tito/admin/collection_proxy.rb', line 20 def order(**fields) dup_with { |q| q.add_order(fields) } end |
#page(number) ⇒ Object
32 33 34 |
# File 'lib/tito/admin/collection_proxy.rb', line 32 def page(number) dup_with { |q| q.set_page(number) } end |
#per(size) ⇒ Object
36 37 38 |
# File 'lib/tito/admin/collection_proxy.rb', line 36 def per(size) dup_with { |q| q.set_per_page(size) } end |
#search(term) ⇒ Object
24 25 26 |
# File 'lib/tito/admin/collection_proxy.rb', line 24 def search(term) dup_with { |q| q.add_search(term) } end |
#size ⇒ Object Also known as: count, length
63 64 65 66 |
# File 'lib/tito/admin/collection_proxy.rb', line 63 def size ensure_first_page_loaded @meta&.fetch("total_count", 0) || 0 end |
#total_pages ⇒ Object
70 71 72 73 |
# File 'lib/tito/admin/collection_proxy.rb', line 70 def total_pages ensure_first_page_loaded @meta&.fetch("total_pages", 0) || 0 end |
#where(**conditions) ⇒ Object
– Chainable query methods (return new proxy) –
16 17 18 |
# File 'lib/tito/admin/collection_proxy.rb', line 16 def where(**conditions) dup_with { |q| q.add_where(conditions) } end |