Skip to content
This repository was archived by the owner on Dec 1, 2023. It is now read-only.
Open
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ Or install it yourself as:

Counter caches are configured on the models from the perspective of the child model to the parent that contains the counter.

#### Basic Counter with recalculation:
#### Basic Counter with recalculation and custom options:

```ruby
class Post
Expand All @@ -106,6 +106,7 @@ class Post
relation: :user,
relation_class_name: "User",
method: :calculate_posts_count, # This is a method on the user.
custom_options: { email: ->(user) { user.email }, foo: "bar" } # custom_options hash available for further processing in Worker.
end
```

Expand Down
11 changes: 7 additions & 4 deletions lib/counter/cache/counters/buffer_counter/enqueuer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@ module Counters
class BufferCounter
class Enqueuer < Struct.new(:options, :source_object_class_name, :relation_id, :relation_class, :counter_class_name)
def enqueue!(source_object)
create_and_enqueue(options.wait(source_object), options.cached?)
create_and_enqueue(options.recalculation_delay, false) if options.recalculation?
create_and_enqueue(options.wait(source_object), options.cached?, source_object)
create_and_enqueue(options.recalculation_delay, false, source_object) if options.recalculation?
end

private

def create_and_enqueue(delay, cached)
def create_and_enqueue(delay, cached, source_object)
options.worker_adapter.enqueue(delay,
source_object_class_name,
{ relation_class_name: relation_class,
relation_id: relation_id,
column: options.column,
method: options.method,
cache: cached,
counter: counter_class_name })
counter: counter_class_name,
custom_options: options.custom_options(source_object)
}
)
end
end
end
Expand Down
16 changes: 16 additions & 0 deletions lib/counter/cache/options_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ def wait(source_object)
end
end

def custom_options(source_object)
if options[:custom_options]
custom_options = {}
options[:custom_options].each do |option_name, option_value|
if option_value.respond_to?(:call)
custom_options[option_name] = option_value.call(source_object)
else
custom_options[option_name] = option_value
end
end
return custom_options
else
nil
end
end

def recalculation_delay
options[:recalculation_delay] || Counter::Cache.configuration.recalculation_delay
end
Expand Down