This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FilmRating < ApplicationRecord | |
self.primary_key = :id | |
def self.refresh | |
Scenic.database.refresh_materialized_view(table_name, concurrently: false, cascade: false) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreateFilmRatings < ActiveRecord::Migration[6.0] | |
def change | |
create_view :film_ratings, materialized: true | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT row_number() OVER () AS id, | |
AVG(rate) AS rate, | |
Films.id AS film_id, | |
Films.title AS title, | |
Films.genre AS genre | |
FROM Films | |
LEFT OUTER JOIN Reviews ON Reviews.film_id=Films.id | |
GROUP BY Films.id; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FilmsController < ApplicationController | |
def index | |
@films = serialized_films | |
render json: @films | |
end | |
private | |
def films_scope | |
Film.all |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FilmCarrier | |
delegate_missing_to :@film | |
def initialize(film, cached_stats: nil) | |
@film = film | |
@cached_ratings_stats = cached_stats || calculate_stats(film) | |
end | |
def avg_rating | |
@cached_ratings_stats[:avg_ratings][@film.id] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FilmsController < ApplicationController | |
def index | |
@films = serialized_films | |
render json: @films | |
end | |
private | |
def films_scope | |
Film.all |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FilmsController < ApplicationController | |
def index | |
@films = serialized_films | |
render json: @films | |
end | |
def serialized_films | |
Film.all.map do |film| | |
{ | |
title: film.title, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Medium | |
cattr_accessor :source | |
self.source = PostsSource::Remote | |
… | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
posts = Medium.new | |
posts.source = PostsSource::Fake | |
posts.user_posts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
posts = Medium.new('dhh') | |
posts.source = PostsSource::Remote | |
posts.user_posts |