
Security News
Rspack Introduces Rslint, a TypeScript-First Linter Written in Go
Rspack launches Rslint, a fast TypeScript-first linter built on typescript-go, joining in on the trend of toolchains creating their own linters.
Frecency is a Ruby on Rails gem that provides a way to sort models based on a combination of frequency and recency, also known as "frecency." This allows for more relevant sorting of records by considering both how often and how recently an item has been accessed or updated.
To install the frecency
gem, add it to your Gemfile in your Rails application:
gem 'frecency'
Then, run the following command to install the gem:
bundle install
You need to configure your frecency gem in an initializer. Create a file named frecency.rb
in your config/initializers
directory:
# config/initializers/frecency.rb
Frecency.configure do |config|
config.default_frequency_column = :views_count # Default column for frequency
config.default_recency_column = :updated_at # Default column for recency
config.default_custom_frequency_scope = nil # Default custom frequency scope (optional)
end
Next, configure the models that you want to utilize frecency sorting. You can do this by adding the necessary configuration to each model. Here’s how you can configure a model:
For basic configuration using default settings:
class Product < ApplicationRecord
configure_frecency(
frequency: :views_count, # Column calculating frequency
recency: :updated_at # Column calculating recency
)
end
If you need a custom frequency scope, you can specify it:
class Order < ApplicationRecord
belongs_to :product
configure_frecency(
frequency: :dynamic, # Use dynamic frequency calculation
recency: :created_at, # Column calculating recency
custom_frequency_scope: -> {
select("#{table_name}.*, COUNT(orders.id) as frequency")
.joins(:orders)
.group("#{table_name}.id")
}
)
end
After configuring the gem, you can use the frecency
method to fetch records sorted by frecency:
To retrieve top 10 products sorted by frecency:
top_products = Product.frecency(10)
You can define more complex configuration and filtering:
class CustomModel < ApplicationRecord
configure_frecency(
frequency: :custom_count, # Custom column for frequency
recency: :last_accessed_at, # Custom column for recency
custom_frequency_scope: -> {
select("#{table_name}.*, custom_logic() as frequency")
}
)
end
top_records = CustomModel.frecency(20)
This gem is available as open source under the terms of the MIT License.
FAQs
Unknown package
We found that frecency demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Rspack launches Rslint, a fast TypeScript-first linter built on typescript-go, joining in on the trend of toolchains creating their own linters.
Security News
Hacker Demonstrates How Easy It Is To Steal Data From Popular Password Managers
Security News
Oxlint’s new preview brings type-aware linting powered by typescript-go, combining advanced TypeScript rules with native-speed performance.