Skip to content

Commit

Permalink
Add ability to set a custom date
Browse files Browse the repository at this point in the history
  • Loading branch information
Michaelvilleneuve committed Dec 14, 2023
1 parent 3cc1117 commit 1b92ff3
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -6,3 +6,4 @@
/pkg/
/spec/reports/
/tmp/
*.gem
2 changes: 1 addition & 1 deletion Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
statisfy (0.0.10)
statisfy (0.0.11)

GEM
remote: https://rubygems.org/
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -180,6 +180,7 @@ This method is used to define a counter.
| `decrement_if` | lambda, optional | Boolean. If it returns true, then the counter will be decremented instead of incremented |
| `uniq_by` | lambda, optional | Won't count the same value twice. By default it is uniq on the `id`. Example use case: if a `Post` is written by an `author_id`, you could set `-> { post.author_id}` to know how many authors wrote an article this month |
| `decrement_on_destroy` | Boolean, optional | Default is true. The counter will be decremented when the model instance is destroyed. |
| `date_override` | DateTime, optional | Default is `instance.created_at`. |

### aggregate

Expand Down
7 changes: 2 additions & 5 deletions lib/statisfy/counter.rb
Expand Up @@ -45,6 +45,7 @@ def apply_default_counter_options(args)
define_method(:decrement?, args[:decrement_if] || -> { false })
define_method(:should_run?, args[:if] || -> { true })
define_method(:decrement_on_destroy?, -> { args[:decrement_on_destroy] != false })
define_method(:month_to_set, args[:date_override] || -> { params["created_at"] })
end

#
Expand Down Expand Up @@ -172,10 +173,6 @@ def scopes_with_global
scopes.flatten.compact << nil
end

def month_to_set
params["created_at"].to_date.strftime("%Y-%m")
end

def process_event
return decrement if can_decrement_on_destroy?
return unless if_async
Expand All @@ -200,7 +197,7 @@ def value
# (in general the Department(s) and Organisation(s) for both the current month and the global counter)
#
def all_counters
[month_to_set, nil].each do |month|
[month_to_set.to_date.strftime("%Y-%m"), nil].each do |month|
scopes_with_global.each do |scope|
yield self.class.key_for(scope:, month:)
end
Expand Down
2 changes: 1 addition & 1 deletion statisfy.gemspec
Expand Up @@ -2,7 +2,7 @@

Gem::Specification.new do |s|
s.name = "statisfy"
s.version = "0.0.10"
s.version = "0.0.11"
s.required_ruby_version = ">= 3.2.0"
s.date = "2023-12-13"
s.summary = "A performant and flexible counter solution"
Expand Down

0 comments on commit 1b92ff3

Please sign in to comment.