Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,7 @@ RSpec/ContextWording:

RSpec/DescribeClass:
Exclude:
- spec/integrations/**/*.rb
- spec/requests/**/*.rb

RSpec/DuplicatedMetadata:
Expand Down
2 changes: 2 additions & 0 deletions app/graphql/types/abstract_object_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module Types
class AbstractObjectType < GraphQL::Schema::Object
include Support::GraphQLAPI::Enhancements::AbstractObject

include GraphQL::FragmentCache::Object

def current_user_privileged?
context[:current_user].try(:has_global_admin_access?)
end
Expand Down
8 changes: 6 additions & 2 deletions app/graphql/types/template_entity_list_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class TemplateEntityListType < Types::BaseObject
TEXT
end

field :entities, ["::Types::EntityType", { null: false }], null: false, method: :valid_entities do
field :entities, ["::Types::EntityType", { null: false }], null: false do
description <<~TEXT
The actual entity records within this list.

Expand Down Expand Up @@ -75,8 +75,12 @@ class TemplateEntityListType < Types::BaseObject
TEXT
end

load_association! :entities
load_association! :cached_entity_list_items

load_association! :list_item_layout_instances, as: :list_item_layouts

def entities
cached_entity_list_items.then { _1.map(&:entity) }
end
end
end
2 changes: 2 additions & 0 deletions app/models/concerns/template_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module TemplateInstance
include Renderable

included do
self.filter_attributes = [:config, :slots]

attribute :config, Templates::Instances::Config.to_type

belongs_to :entity, polymorphic: true
Expand Down
17 changes: 15 additions & 2 deletions app/models/templates/cached_entity_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,27 @@ class CachedEntityList < ApplicationRecord
belongs_to :template_instance, polymorphic: true
belongs_to :entity, polymorphic: true

has_many :cached_entity_list_items, -> { in_default_order }, class_name: "Templates::CachedEntityListItem", inverse_of: :cached_entity_list, dependent: :delete_all
has_many :cached_entity_list_items, -> { in_default_order.includes(:entity) }, class_name: "Templates::CachedEntityListItem",
inverse_of: :cached_entity_list, dependent: :delete_all

has_many :entities, through: :cached_entity_list_items, source: :entity
has_many :list_item_layout_instances, through: :cached_entity_list_items, source: :list_item_layout_instance

# @note Cannot eager-load because of polymorphic associations.
def entities
# :nocov:
cached_entity_list_items.map(&:entity)
# :nocov:
end

# @return [Integer]
def prune_items!
cached_entity_list_items.beyond(count).delete_all
end

# @see Templates::EntityLists::RefreshCached
# @see Templates::EntityLists::CachedRefresher
monadic_operation! def refresh
call_operation("templates.entity_lists.refresh_cached", self)
end
end
end
10 changes: 10 additions & 0 deletions app/operations/templates/entity_lists/refresh_cached.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module Templates
module EntityLists
# @see Templates::EntityLists::CachedRefresher
class RefreshCached < Support::SimpleServiceOperation
service_klass Templates::EntityLists::CachedRefresher
end
end
end
4 changes: 3 additions & 1 deletion app/services/harvesting/metadata/drops/data_extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def ivar
def extract(data)
value = data.public_send(attr)

cast = cast_type[value]
result = cast_type.try(value).to_monad

cast = result.value_or(default)

cast.nil? ? default : cast
end
Expand Down
1 change: 1 addition & 0 deletions app/services/layouts/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def call
super
end

# @see Layouts::Instances::Processor
wrapped_hook! def process
yield layout_instance.process

Expand Down
33 changes: 33 additions & 0 deletions app/services/templates/entity_lists/cached_refresher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

module Templates
module EntityLists
# @see Templates::EntityLists::RefreshCached
class CachedRefresher < Support::HookBased::Actor
include Dry::Initializer[undefined: false].define -> do
param :cached_entity_list, Templates::Types::CachedEntityList
end

delegate :template_instance, to: :cached_entity_list

standard_execution!

# @return [Dry::Monads::Result]
def call
run_callbacks :execute do
yield regenerate!
end

Success()
end

wrapped_hook! def regenerate
yield template_instance.cache_entity_list

cached_entity_list.reload

super
end
end
end
end
19 changes: 8 additions & 11 deletions app/services/templates/instances/entity_list_cacher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ class EntityListCacher < Support::HookBased::Actor

delegate :entity_id, :entity_type, :list_item_template?, to: :template_instance

# @return [String]
attr_reader :advisory_lock_key

# @return [Templates::CachedEntityList]
attr_reader :cached_entity_list

Expand All @@ -46,8 +43,6 @@ def call
end

wrapped_hook! def prepare
@advisory_lock_key = "templates/instances/entity_list_caching/#{template_instance.id}"

@entity_list = template_instance.entity_list

@hidden_by_entity_list = !list_item_template? && entity_list.empty?
Expand All @@ -67,12 +62,6 @@ def call
super
end

wrapped_hook! def update_template
template_instance.update_columns(hidden_by_entity_list:)

super
end

wrapped_hook! def build_list
tuple = {
template_instance_type:,
Expand Down Expand Up @@ -120,6 +109,14 @@ def call
wrapped_hook! def prune_items
cached_entity_list.prune_items!

cached_entity_list.touch(:refreshed_at, :updated_at)

super
end

wrapped_hook! def update_template
template_instance.update_columns(hidden_by_entity_list:)

super
end
end
Expand Down
9 changes: 9 additions & 0 deletions app/services/templates/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def call
yield persist_instance!

yield render!

yield process!
end

Success template_instance
Expand Down Expand Up @@ -61,6 +63,13 @@ def call

around_render :track_render!

# @see Templates::Instances::Processor
wrapped_hook! def process
yield template_instance.process

super
end

private

# @return [void]
Expand Down
2 changes: 2 additions & 0 deletions app/services/templates/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ module Types

Assigns = Hash.map(Coercible::String, Any)

CachedEntityList = ModelInstance("Templates::CachedEntityList")

Contribution = Instance(::Contribution)

Contributions = Coercible::Array.of(Contribution)
Expand Down
4 changes: 4 additions & 0 deletions config/initializers/960_gql.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# frozen_string_literal: true

GraphQL::FragmentCache.configure do |config|
config.cache_store = Rails.cache
end

ActiveSupport::Notifications.subscribe(/graphql/) do |event|
# :nocov:
name = event.name
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class AddRefreshedAtToCachedEntityLists < ActiveRecord::Migration[7.2]
def change
change_table :templates_cached_entity_lists do |t|
t.timestamp :refreshed_at, null: true

t.index :refreshed_at
end
end
end
11 changes: 10 additions & 1 deletion db/structure.sql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions lib/frozen_record/static_cached_columns.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19655,6 +19655,19 @@
default_function: CURRENT_TIMESTAMP
has_default: true
virtual: false
- id: Templates::CachedEntityList#refreshed_at
model_name: Templates::CachedEntityList
table_name: templates_cached_entity_lists
name: refreshed_at
type: datetime
'null': true
sql_type_metadata:
sql_type: timestamp without time zone
type: datetime
default:
default_function:
has_default: false
virtual: false
- id: Templates::CachedEntityListItem#id
model_name: Templates::CachedEntityListItem
table_name: templates_cached_entity_list_items
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
</contributor-list>
<ordering>
<background>light</background>
<ordering-identifier>issues</ordering-identifier>
<ordering-source>parent</ordering-source>
<selection-source>self</selection-source>
<slots>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
</descendant-list>
<ordering>
<background>light</background>
<ordering-identifier>volumes</ordering-identifier>
<ordering-source>parent</ordering-source>
<selection-source>self</selection-source>
<slots>
Expand Down
Loading
Loading