songtianlun
905ec35fd8
- Integrate PhotoSwipe library for enhanced image viewing - Create PhotoSwipeLightBoxController to manage images - Register lightbox controller in Stimulus framework - Update views to include lightbox functionality - Modify styles to accommodate new design elements This commit introduces a new way for users to view images with PhotoSwipe, improving the interactivity of the photo gallery. It also includes adjustments to the layout and styles for better presentation and user experience.
59 lines
2.2 KiB
Ruby
59 lines
2.2 KiB
Ruby
class WeatherArt < ApplicationRecord
|
|
extend FriendlyId
|
|
friendly_id :weather_date, use: :slugged
|
|
|
|
belongs_to :city
|
|
has_one_attached :image
|
|
has_one_attached :image_with_watermark
|
|
|
|
has_many :visits, class_name: "Ahoy::Visit", foreign_key: :weather_art_id
|
|
has_many :events, class_name: "Ahoy::Event", foreign_key: :weather_art_id
|
|
|
|
validates :weather_date, presence: true
|
|
validates :city_id, presence: true
|
|
|
|
scope :by_popularity, -> {
|
|
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'")
|
|
.group("weather_arts.id")
|
|
.select("weather_arts.*, COUNT(ahoy_events.id) as visit_count")
|
|
.order("visit_count DESC")
|
|
else
|
|
joins("LEFT JOIN ahoy_events ON (ahoy_events.properties::jsonb->>'weather_art_id')::integer = weather_arts.id
|
|
AND ahoy_events.properties::jsonb->>'event_type' = 'weather_art_view'")
|
|
.group("weather_arts.id")
|
|
.select("weather_arts.*, COUNT(ahoy_events.id) as visit_count")
|
|
.order("visit_count DESC")
|
|
end
|
|
}
|
|
|
|
def should_generate_new_friendly_id?
|
|
weather_date_changed? || city_id_changed? || super
|
|
end
|
|
|
|
def to_s
|
|
"#{city.name} - #{weather_date.strftime('%Y-%m-%d')}"
|
|
end
|
|
|
|
def self.ransackable_associations(auth_object = nil)
|
|
[ "city", "image_attachment", "image_blob" ]
|
|
end
|
|
|
|
def self.ransackable_attributes(auth_object = nil)
|
|
[ "city_id", "cloud", "created_at", "description", "feeling_temp", "humidity", "id", "id_value", "precipitation", "pressure", "prompt", "temperature", "updated_at", "visibility", "weather_date", "wind_scale", "wind_speed" ]
|
|
end
|
|
|
|
def view_count
|
|
if ActiveRecord::Base.connection.adapter_name.downcase == "sqlite"
|
|
Ahoy::Event.where("json_extract(properties, '$.event_type') = 'weather_art_view' AND json_extract(properties, '$.weather_art_id') = ?", self.id).count
|
|
else
|
|
Ahoy::Event.where("properties::jsonb->>'event_type' = 'weather_art_view' AND (properties::jsonb->>'weather_art_id')::integer = ?", self.id).count
|
|
end
|
|
end
|
|
|
|
def image_url
|
|
image.attached? ? image.blob : nil
|
|
end
|
|
end
|