From afcb9c6cd8beef5111209f65dea02898c57024ce Mon Sep 17 00:00:00 2001 From: songtianlun Date: Wed, 12 Feb 2025 18:00:30 +0800 Subject: [PATCH] refactor: simplify scope limit parameters - Change `limit:` to `limit =` in the scopes for `latest` and `by_popularity`. - This allows for a default value of 100 to be used if no argument is provided, making the code more user-friendly and consistent. This refactor does not alter the behavior of the existing functionality but streamlines the syntax. --- app/models/weather_art.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/weather_art.rb b/app/models/weather_art.rb index 2156576..1eea6e5 100644 --- a/app/models/weather_art.rb +++ b/app/models/weather_art.rb @@ -12,11 +12,11 @@ class WeatherArt < ApplicationRecord validates :weather_date, presence: true validates :city_id, presence: true - scope :latest, ->(limit: 100) { + scope :latest, ->(limit = 100) { order(created_at: :desc).limit(limit) } - scope :by_popularity, ->(limit: 100) { + scope :by_popularity, ->(limit = 100) { if ActiveRecord::Base.connection.adapter_name.downcase == "sqlite" joins("LEFT JOIN ahoy_events ON json_extract(ahoy_events.properties, '$.weather_art_id') = weather_arts.id AND json_extract(ahoy_events.properties, '$.event_type') = 'weather_art_view'")