├── .gitignore ├── .rspec ├── .ruby-version ├── Gemfile ├── Gemfile.lock ├── LICENSE ├── README.md ├── Rakefile ├── app ├── assets │ ├── config │ │ └── manifest.js │ ├── images │ │ └── .keep │ └── stylesheets │ │ ├── application.scss │ │ ├── components │ │ └── _artists.scss │ │ └── layout │ │ └── _base.scss ├── controllers │ ├── application_controller.rb │ └── artists_controller.rb ├── helpers │ └── application_helper.rb ├── mailers │ └── .keep ├── models │ ├── .keep │ ├── application_record.rb │ ├── artist.rb │ └── label.rb ├── queries │ ├── artist_query.rb │ ├── base_query.rb │ ├── chaining │ │ ├── artist_query.rb │ │ └── base_query.rb │ └── extending │ │ ├── artist_query.rb │ │ └── base_query.rb └── views │ ├── artists │ └── index.html.erb │ └── layouts │ └── application.html.erb ├── bin ├── bundle ├── rails ├── rake ├── setup ├── update └── yarn ├── config.ru ├── config ├── application.rb ├── boot.rb ├── cable.yml ├── database.yml ├── environment.rb ├── environments │ ├── development.rb │ ├── production.rb │ └── test.rb ├── initializers │ ├── application_controller_renderer.rb │ ├── assets.rb │ ├── backtrace_silencers.rb │ ├── content_security_policy.rb │ ├── cookies_serializer.rb │ ├── filter_parameter_logging.rb │ ├── inflections.rb │ ├── mime_types.rb │ ├── permissions_policy.rb │ ├── session_store.rb │ └── wrap_parameters.rb ├── locales │ └── en.yml ├── puma.rb ├── routes.rb ├── secrets.yml ├── spring.rb └── storage.yml ├── db ├── migrate │ ├── 20151104004615_create_artists.rb │ ├── 20151104072105_create_labels.rb │ ├── 20151104073745_add_label_reference_to_artists.rb │ └── 20170413053040_add_picture_url_to_artists.rb ├── schema.rb └── seeds.rb ├── lib ├── assets │ └── .keep └── tasks │ ├── .keep │ └── benchmark.rake ├── log └── .keep ├── public ├── 404.html ├── 422.html ├── 500.html ├── favicon.ico └── robots.txt ├── services.rb ├── spec ├── queries │ ├── artist_query_spec.rb │ ├── chaining │ │ └── artist_query_spec.rb │ └── extending │ │ └── artist_query_spec.rb ├── rails_helper.rb ├── spec_helper.rb └── support │ ├── shared_contexts │ └── query_object_models.rb │ └── shared_examples │ ├── query_object.rb │ └── query_object │ ├── base.rb │ └── chaining.rb └── vendor └── assets ├── javascripts └── .keep └── stylesheets └── .keep /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files for more about ignoring files. 2 | # 3 | # If you find yourself ignoring temporary files generated by your text editor 4 | # or operating system, you probably want to add a global ignore instead: 5 | # git config --global core.excludesfile '~/.gitignore_global' 6 | 7 | # Ignore bundler config. 8 | /.bundle 9 | 10 | # Ignore the default SQLite database. 11 | /db/*.sqlite3 12 | /db/*.sqlite3-journal 13 | 14 | # Ignore all logfiles and tempfiles. 15 | /log/* 16 | !/log/.keep 17 | /tmp 18 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --color 2 | --require spec_helper 3 | -------------------------------------------------------------------------------- /.ruby-version: -------------------------------------------------------------------------------- 1 | 3.2.2 2 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # default 4 | gem 'rails', '7.0.4' 5 | gem 'bootsnap' 6 | gem 'sqlite3' 7 | gem 'puma' 8 | 9 | # assets 10 | gem 'sass-rails', '~> 6.0' 11 | 12 | group :development, :test do 13 | gem 'pry-rails' 14 | gem 'debug' 15 | end 16 | 17 | group :development do 18 | gem 'benchmark-ips' 19 | gem 'web-console', '~> 2.0' 20 | end 21 | 22 | group :test do 23 | gem 'rspec-rails' 24 | end 25 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | actioncable (7.0.4) 5 | actionpack (= 7.0.4) 6 | activesupport (= 7.0.4) 7 | nio4r (~> 2.0) 8 | websocket-driver (>= 0.6.1) 9 | actionmailbox (7.0.4) 10 | actionpack (= 7.0.4) 11 | activejob (= 7.0.4) 12 | activerecord (= 7.0.4) 13 | activestorage (= 7.0.4) 14 | activesupport (= 7.0.4) 15 | mail (>= 2.7.1) 16 | net-imap 17 | net-pop 18 | net-smtp 19 | actionmailer (7.0.4) 20 | actionpack (= 7.0.4) 21 | actionview (= 7.0.4) 22 | activejob (= 7.0.4) 23 | activesupport (= 7.0.4) 24 | mail (~> 2.5, >= 2.5.4) 25 | net-imap 26 | net-pop 27 | net-smtp 28 | rails-dom-testing (~> 2.0) 29 | actionpack (7.0.4) 30 | actionview (= 7.0.4) 31 | activesupport (= 7.0.4) 32 | rack (~> 2.0, >= 2.2.0) 33 | rack-test (>= 0.6.3) 34 | rails-dom-testing (~> 2.0) 35 | rails-html-sanitizer (~> 1.0, >= 1.2.0) 36 | actiontext (7.0.4) 37 | actionpack (= 7.0.4) 38 | activerecord (= 7.0.4) 39 | activestorage (= 7.0.4) 40 | activesupport (= 7.0.4) 41 | globalid (>= 0.6.0) 42 | nokogiri (>= 1.8.5) 43 | actionview (7.0.4) 44 | activesupport (= 7.0.4) 45 | builder (~> 3.1) 46 | erubi (~> 1.4) 47 | rails-dom-testing (~> 2.0) 48 | rails-html-sanitizer (~> 1.1, >= 1.2.0) 49 | activejob (7.0.4) 50 | activesupport (= 7.0.4) 51 | globalid (>= 0.3.6) 52 | activemodel (7.0.4) 53 | activesupport (= 7.0.4) 54 | activerecord (7.0.4) 55 | activemodel (= 7.0.4) 56 | activesupport (= 7.0.4) 57 | activestorage (7.0.4) 58 | actionpack (= 7.0.4) 59 | activejob (= 7.0.4) 60 | activerecord (= 7.0.4) 61 | activesupport (= 7.0.4) 62 | marcel (~> 1.0) 63 | mini_mime (>= 1.1.0) 64 | activesupport (7.0.4) 65 | concurrent-ruby (~> 1.0, >= 1.0.2) 66 | i18n (>= 1.6, < 2) 67 | minitest (>= 5.1) 68 | tzinfo (~> 2.0) 69 | benchmark-ips (2.9.3) 70 | binding_of_caller (1.0.0) 71 | debug_inspector (>= 0.0.1) 72 | bootsnap (1.10.3) 73 | msgpack (~> 1.2) 74 | builder (3.2.4) 75 | coderay (1.1.3) 76 | concurrent-ruby (1.1.10) 77 | crass (1.0.6) 78 | debug (1.6.1) 79 | irb (>= 1.3.6) 80 | reline (>= 0.3.1) 81 | debug_inspector (1.1.0) 82 | diff-lcs (1.5.0) 83 | erubi (1.11.0) 84 | ffi (1.15.5) 85 | globalid (1.0.0) 86 | activesupport (>= 5.0) 87 | i18n (1.12.0) 88 | concurrent-ruby (~> 1.0) 89 | io-console (0.5.11) 90 | irb (1.4.1) 91 | reline (>= 0.3.0) 92 | loofah (2.19.0) 93 | crass (~> 1.0.2) 94 | nokogiri (>= 1.5.9) 95 | mail (2.7.1) 96 | mini_mime (>= 0.1.1) 97 | marcel (1.0.2) 98 | method_source (1.0.0) 99 | mini_mime (1.1.2) 100 | mini_portile2 (2.8.0) 101 | minitest (5.16.3) 102 | msgpack (1.4.5) 103 | net-imap (0.3.1) 104 | net-protocol 105 | net-pop (0.1.2) 106 | net-protocol 107 | net-protocol (0.1.3) 108 | timeout 109 | net-smtp (0.3.2) 110 | net-protocol 111 | nio4r (2.5.8) 112 | nokogiri (1.13.9) 113 | mini_portile2 (~> 2.8.0) 114 | racc (~> 1.4) 115 | pry (0.14.1) 116 | coderay (~> 1.1) 117 | method_source (~> 1.0) 118 | pry-rails (0.3.9) 119 | pry (>= 0.10.4) 120 | puma (6.0.0) 121 | nio4r (~> 2.0) 122 | racc (1.6.0) 123 | rack (2.2.4) 124 | rack-test (2.0.2) 125 | rack (>= 1.3) 126 | rails (7.0.4) 127 | actioncable (= 7.0.4) 128 | actionmailbox (= 7.0.4) 129 | actionmailer (= 7.0.4) 130 | actionpack (= 7.0.4) 131 | actiontext (= 7.0.4) 132 | actionview (= 7.0.4) 133 | activejob (= 7.0.4) 134 | activemodel (= 7.0.4) 135 | activerecord (= 7.0.4) 136 | activestorage (= 7.0.4) 137 | activesupport (= 7.0.4) 138 | bundler (>= 1.15.0) 139 | railties (= 7.0.4) 140 | rails-dom-testing (2.0.3) 141 | activesupport (>= 4.2.0) 142 | nokogiri (>= 1.6) 143 | rails-html-sanitizer (1.4.3) 144 | loofah (~> 2.3) 145 | railties (7.0.4) 146 | actionpack (= 7.0.4) 147 | activesupport (= 7.0.4) 148 | method_source 149 | rake (>= 12.2) 150 | thor (~> 1.0) 151 | zeitwerk (~> 2.5) 152 | rake (13.0.6) 153 | reline (0.3.1) 154 | io-console (~> 0.5) 155 | rspec-core (3.11.0) 156 | rspec-support (~> 3.11.0) 157 | rspec-expectations (3.11.0) 158 | diff-lcs (>= 1.2.0, < 2.0) 159 | rspec-support (~> 3.11.0) 160 | rspec-mocks (3.11.0) 161 | diff-lcs (>= 1.2.0, < 2.0) 162 | rspec-support (~> 3.11.0) 163 | rspec-rails (5.1.0) 164 | actionpack (>= 5.2) 165 | activesupport (>= 5.2) 166 | railties (>= 5.2) 167 | rspec-core (~> 3.10) 168 | rspec-expectations (~> 3.10) 169 | rspec-mocks (~> 3.10) 170 | rspec-support (~> 3.10) 171 | rspec-support (3.11.0) 172 | sass-rails (6.0.0) 173 | sassc-rails (~> 2.1, >= 2.1.1) 174 | sassc (2.4.0) 175 | ffi (~> 1.9) 176 | sassc-rails (2.1.2) 177 | railties (>= 4.0.0) 178 | sassc (>= 2.0) 179 | sprockets (> 3.0) 180 | sprockets-rails 181 | tilt 182 | sprockets (4.1.1) 183 | concurrent-ruby (~> 1.0) 184 | rack (> 1, < 3) 185 | sprockets-rails (3.4.2) 186 | actionpack (>= 5.2) 187 | activesupport (>= 5.2) 188 | sprockets (>= 3.0.0) 189 | sqlite3 (1.4.2) 190 | thor (1.2.1) 191 | tilt (2.0.10) 192 | timeout (0.3.0) 193 | tzinfo (2.0.5) 194 | concurrent-ruby (~> 1.0) 195 | web-console (2.3.0) 196 | activemodel (>= 4.0) 197 | binding_of_caller (>= 0.7.2) 198 | railties (>= 4.0) 199 | sprockets-rails (>= 2.0, < 4.0) 200 | websocket-driver (0.7.5) 201 | websocket-extensions (>= 0.1.0) 202 | websocket-extensions (0.1.5) 203 | zeitwerk (2.6.1) 204 | 205 | PLATFORMS 206 | ruby 207 | 208 | DEPENDENCIES 209 | benchmark-ips 210 | bootsnap 211 | debug 212 | pry-rails 213 | puma 214 | rails (= 7.0.4) 215 | rspec-rails 216 | sass-rails (~> 6.0) 217 | sqlite3 218 | web-console (~> 2.0) 219 | 220 | BUNDLED WITH 221 | 2.3.19 222 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 2 | Version 2, December 2004 3 | 4 | Copyright (C) 2004 Sam Hocevar 5 | 6 | Everyone is permitted to copy and distribute verbatim or modified 7 | copies of this license document, and changing it is allowed as long 8 | as the name is changed. 9 | 10 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 11 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 12 | 13 | 0. You just DO WHAT THE FUCK YOU WANT TO. 14 | 15 | 16 | This program is free software. It comes without any warranty, to 17 | the extent permitted by applicable law. You can redistribute it 18 | and/or modify it under the terms of the Do What The Fuck You Want 19 | To Public License, Version 2, as published by Sam Hocevar. See 20 | http://www.wtfpl.net/ for more details. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Query Objects - Example 2 | 3 | This is an example of a rails application that defines and uses Query Objects. 4 | 5 | ## Getting started 6 | 7 | Have a look at `app/queries/` folder. 8 | 9 | 3 implementations are provided: 10 | 11 | * Delegating to ActiveRecord::Relation (default one) 12 | * Delegating to ActiveRecord::Relation with chaining ActiveRecord conditions in between 13 | * Extending ActiveRecord::Relation 14 | 15 | ## Usage 16 | 17 | ### Defining a Query Object 18 | 19 | ```ruby 20 | class ArtistQuery < BaseQuery 21 | # defines the default model on which queries will be made 22 | def self.relation(base_relation=nil) 23 | super(base_relation, Artist) 24 | end 25 | 26 | # a first scope 27 | def available 28 | where(available: true) 29 | end 30 | 31 | # another scope 32 | def by_genre(genre) 33 | where(genre: genre) 34 | end 35 | end 36 | ``` 37 | 38 | ### Making queries 39 | 40 | ```ruby 41 | ArtistQuery.relation 42 | # => Returns all artists. 43 | # Based on `all` relation provided by `ActiveRecord`). 44 | 45 | ArtistQuery.relation.available 46 | # => Returns all available artists. 47 | # Based on `available` scope method provided by `ArtistQuery`. 48 | 49 | ArtistQuery.relation.available.by_genre('Metal') 50 | # => Returns all available Metal artists. 51 | # Based on `available` and `by_genre(name)` scope methods provided by `ArtistQuery`. 52 | 53 | ArtistQuery.relation.available.by_genre('Metal').order(:name) 54 | # => Returns all available metal artists ordered by name. 55 | # Based on `available` and `by_genre(name)` scope methods provided by `ArtistQuery` 56 | # and based on `order` method provided by `ActiveRecord`. 57 | 58 | awesome_label = Label.first 59 | ArtistQuery.relation(awesome_label.artists).available 60 | # => Returns all available artists for awesome label. 61 | # Based on the following association: `label` has many `artists`. 62 | ``` 63 | 64 | ## Chaining with ActiveRecord conditions in between 65 | 66 | By default, this feature is not provided. 67 | 68 | ```ruby 69 | # PROVIDED 70 | ArtistQuery.relation.available.order(:name) 71 | 72 | # NOT PROVIDED 73 | ArtistQuery.relation.order(:name).available 74 | # => NoMethodError: 75 | # undefined method `available' for # 76 | ``` 77 | 78 | To enable this feature - which isn't recommended - switch to the [second implementation](app/queries/chaining/base_query.rb). 79 | 80 | ### Why isn't it recommended? 81 | 82 | The purpose of query objects is to extract scopes from models and to have relevant queries. 83 | Thus introducing ActiveRecord conditions between relevant queries is an anti-pattern. Try to avoid this behaviour. 84 | 85 | ## Tests 86 | 87 | There are tests for the 3 implementations. To run the tests: 88 | 89 | ``` 90 | $ rspec 91 | ``` 92 | 93 | ## Benchmark 94 | 95 | A benchmark is provided between 2 implementations: delegator (default one) and extend. Just run the following command: 96 | 97 | ``` 98 | $ rake benchmark 99 | ``` 100 | 101 | ### Results 102 | 103 | ``` 104 | Warming up -------------------------------------- 105 | delegator -- without model 12.047k i/100ms 106 | delegator -- with model 15.510k i/100ms 107 | extend -- without model 8.431k i/100ms 108 | extend -- with model 8.397k i/100ms 109 | Calculating ------------------------------------- 110 | delegator -- without model 170.047k (± 3.8%) i/s - 855.337k in 5.037890s 111 | delegator -- with model 169.862k (± 3.5%) i/s - 853.050k in 5.029212s 112 | extend -- without model 77.498k (±12.7%) i/s - 379.395k in 5.009203s 113 | extend -- with model 81.004k (±14.7%) i/s - 394.659k in 5.005465s 114 | 115 | Comparison: 116 | delegator -- without model: 170047.0 i/s 117 | delegator -- with model: 169862.0 i/s - same-ish: difference falls within error 118 | extend -- with model: 81004.4 i/s - 2.10x slower 119 | extend -- without model: 77497.9 i/s - 2.19x slower 120 | ``` 121 | 122 | ## Running the app 123 | 124 | ### Requirements 125 | 126 | * Ruby 2.4+ 127 | * SQLite 128 | 129 | ### Installation 130 | 131 | ``` 132 | $ bundle install 133 | $ rake db:create db:migrate db:seed 134 | ``` 135 | 136 | ## Credits 137 | 138 | Thanks to [Bert Goethals](http://bertg.be/) for his help in optimizing the Query Objects. 139 | 140 | ## License 141 | 142 | This project is released under the WTFPL License. 143 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # Add your own tasks in files placed in lib/tasks ending in .rake, 2 | # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. 3 | 4 | require File.expand_path('../config/application', __FILE__) 5 | 6 | Rails.application.load_tasks 7 | -------------------------------------------------------------------------------- /app/assets/config/manifest.js: -------------------------------------------------------------------------------- 1 | //= link_tree ../images 2 | //= link_directory ../stylesheets .css 3 | -------------------------------------------------------------------------------- /app/assets/images/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cveneziani/query-objects-example/7ad463596a73f70eeef580d4d72f74115faedbd9/app/assets/images/.keep -------------------------------------------------------------------------------- /app/assets/stylesheets/application.scss: -------------------------------------------------------------------------------- 1 | @import 'layout/base'; 2 | @import 'components/artists'; 3 | -------------------------------------------------------------------------------- /app/assets/stylesheets/components/_artists.scss: -------------------------------------------------------------------------------- 1 | .artists-container { 2 | display: flex; 3 | flex-wrap: wrap; 4 | align-content: space-around; 5 | margin: 0 4em; 6 | min-height: 100vh; 7 | 8 | @media (max-width: 700px) { 9 | justify-content: center; 10 | } 11 | 12 | @media (min-width: 700px) { 13 | justify-content: space-around; 14 | } 15 | 16 | @media (min-width: 1250px) { 17 | justify-content: space-between; 18 | align-content: center; 19 | } 20 | } 21 | 22 | .artist { 23 | width: 250px; 24 | margin: 1em; 25 | } 26 | 27 | .artist-detais { 28 | ul { 29 | margin-bottom: 0; 30 | padding: 0; 31 | list-style-type: none; 32 | } 33 | 34 | strong { 35 | display: inline-block; 36 | min-width: 5.5em; 37 | } 38 | } 39 | 40 | .artist-picture { 41 | width: 100%; 42 | } 43 | -------------------------------------------------------------------------------- /app/assets/stylesheets/layout/_base.scss: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | } 4 | -------------------------------------------------------------------------------- /app/controllers/application_controller.rb: -------------------------------------------------------------------------------- 1 | class ApplicationController < ActionController::Base 2 | # Prevent CSRF attacks by raising an exception. 3 | # For APIs, you may want to use :null_session instead. 4 | protect_from_forgery with: :exception 5 | end 6 | -------------------------------------------------------------------------------- /app/controllers/artists_controller.rb: -------------------------------------------------------------------------------- 1 | class ArtistsController < ApplicationController 2 | def index 3 | @artists = Artist.all.order(:name) 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /app/helpers/application_helper.rb: -------------------------------------------------------------------------------- 1 | module ApplicationHelper 2 | end 3 | -------------------------------------------------------------------------------- /app/mailers/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cveneziani/query-objects-example/7ad463596a73f70eeef580d4d72f74115faedbd9/app/mailers/.keep -------------------------------------------------------------------------------- /app/models/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cveneziani/query-objects-example/7ad463596a73f70eeef580d4d72f74115faedbd9/app/models/.keep -------------------------------------------------------------------------------- /app/models/application_record.rb: -------------------------------------------------------------------------------- 1 | class ApplicationRecord < ActiveRecord::Base 2 | self.abstract_class = true 3 | end 4 | -------------------------------------------------------------------------------- /app/models/artist.rb: -------------------------------------------------------------------------------- 1 | class Artist < ApplicationRecord 2 | belongs_to :label 3 | end 4 | -------------------------------------------------------------------------------- /app/models/label.rb: -------------------------------------------------------------------------------- 1 | class Label < ApplicationRecord 2 | has_many :artists, dependent: :destroy 3 | end 4 | -------------------------------------------------------------------------------- /app/queries/artist_query.rb: -------------------------------------------------------------------------------- 1 | class ArtistQuery < BaseQuery 2 | def self.relation(base_relation=nil) 3 | super(base_relation, Artist) 4 | end 5 | 6 | def available 7 | where(available: true) 8 | end 9 | 10 | def by_genre(genre) 11 | where(genre: genre) 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /app/queries/base_query.rb: -------------------------------------------------------------------------------- 1 | require 'delegate' 2 | 3 | class BaseQuery < SimpleDelegator 4 | def self.relation(base_relation, base_model) 5 | base_relation ||= base_model.all 6 | new(base_relation) 7 | end 8 | 9 | def self.method_added(method_name) 10 | return if @redefining 11 | @redefining = true 12 | 13 | alias_method :"_#{method_name}", method_name 14 | 15 | define_method method_name do |*args, &block| 16 | result = send(:"_#{method_name}", *args, &block) 17 | 18 | if result.instance_of?(__getobj__.class) 19 | self.class.new(result) 20 | else 21 | result 22 | end 23 | end 24 | 25 | @redefining = false 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /app/queries/chaining/artist_query.rb: -------------------------------------------------------------------------------- 1 | module Chaining 2 | class ArtistQuery < Chaining::BaseQuery 3 | def self.relation(base_relation=nil) 4 | super(base_relation, Artist) 5 | end 6 | 7 | def available 8 | where(available: true) 9 | end 10 | 11 | def by_genre(genre) 12 | where(genre: genre) 13 | end 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /app/queries/chaining/base_query.rb: -------------------------------------------------------------------------------- 1 | require 'delegate' 2 | 3 | module Chaining 4 | class BaseQuery < SimpleDelegator 5 | def self.relation(base_relation, base_model) 6 | base_relation ||= base_model.all 7 | new(base_relation) 8 | end 9 | 10 | def method_missing(method_name, *args, &block) 11 | result = super(method_name, *args, &block) 12 | 13 | if result.instance_of?(__getobj__.class) 14 | self.class.new(result) 15 | else 16 | result 17 | end 18 | end 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /app/queries/extending/artist_query.rb: -------------------------------------------------------------------------------- 1 | module Extending 2 | # Part 2 - With refactoring 3 | class ArtistQuery < Extending::BaseQuery 4 | @model = Artist 5 | 6 | module Scopes 7 | def available 8 | where(available: true) 9 | end 10 | 11 | def by_genre(genre) 12 | where(genre: genre) 13 | end 14 | end 15 | end 16 | 17 | # # Part 1 - without refactoring 18 | # class ArtistQuery 19 | # attr_reader :relation 20 | 21 | # def initialize(base_relation=nil) 22 | # base_relation = Artist.all unless base_relation 23 | # @relation = base_relation.extending(Scopes) 24 | # end 25 | 26 | # module Scopes 27 | # def available 28 | # where(available: true) 29 | # end 30 | 31 | # def by_genre(genre) 32 | # where(genre: genre) 33 | # end 34 | # end 35 | # end 36 | end 37 | -------------------------------------------------------------------------------- /app/queries/extending/base_query.rb: -------------------------------------------------------------------------------- 1 | module Extending 2 | class BaseQuery 3 | @model = nil 4 | 5 | attr_reader :relation 6 | 7 | def initialize(base_relation=nil) 8 | base_relation = self.class.model.all unless base_relation 9 | @relation = base_relation.extending(self.class::Scopes) 10 | end 11 | 12 | def self.model 13 | @model 14 | end 15 | 16 | def self.relation(base_relation=nil) 17 | query = new(base_relation) 18 | query.relation 19 | end 20 | 21 | module Scopes 22 | # any new scope should be implemented there. 23 | end 24 | end 25 | end 26 | -------------------------------------------------------------------------------- /app/views/artists/index.html.erb: -------------------------------------------------------------------------------- 1 |
2 | <% @artists.each do |artist| %> 3 |
4 | <%= image_tag artist.picture_url, class: 'artist-picture' %> 5 |
6 |

<%= artist.name %>

7 |
    8 |
  • Genre <%= artist.genre %>
  • 9 |
  • Label <%= artist.label.name %>
  • 10 |
  • Available <%= artist.available ? 'Yep' : 'Nope' %>
  • 11 |
12 |
13 |
14 | <% end %> 15 | 16 |
17 | 18 |
19 |
20 | -------------------------------------------------------------------------------- /app/views/layouts/application.html.erb: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Music 5 | 6 | <%= favicon_link_tag 'https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/240/samsung/320/headphone_1f3a7.png' %> 7 | <%= stylesheet_link_tag 'application' %> 8 | <%= csrf_meta_tags %> 9 | 10 | 11 | 12 | <%= yield %> 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /bin/bundle: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__) 3 | load Gem.bin_path('bundler', 'bundle') 4 | -------------------------------------------------------------------------------- /bin/rails: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | APP_PATH = File.expand_path("../config/application", __dir__) 3 | require_relative "../config/boot" 4 | require "rails/commands" 5 | -------------------------------------------------------------------------------- /bin/rake: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require_relative "../config/boot" 3 | require "rake" 4 | Rake.application.run 5 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require "fileutils" 3 | 4 | # path to your application root. 5 | APP_ROOT = File.expand_path("..", __dir__) 6 | 7 | def system!(*args) 8 | system(*args) || abort("\n== Command #{args} failed ==") 9 | end 10 | 11 | FileUtils.chdir APP_ROOT do 12 | # This script is a way to set up or update your development environment automatically. 13 | # This script is idempotent, so that you can run it at any time and get an expectable outcome. 14 | # Add necessary setup steps to this file. 15 | 16 | puts "== Installing dependencies ==" 17 | system! "gem install bundler --conservative" 18 | system("bundle check") || system!("bundle install") 19 | 20 | # puts "\n== Copying sample files ==" 21 | # unless File.exist?("config/database.yml") 22 | # FileUtils.cp "config/database.yml.sample", "config/database.yml" 23 | # end 24 | 25 | puts "\n== Preparing database ==" 26 | system! "bin/rails db:prepare" 27 | 28 | puts "\n== Removing old logs and tempfiles ==" 29 | system! "bin/rails log:clear tmp:clear" 30 | 31 | puts "\n== Restarting application server ==" 32 | system! "bin/rails restart" 33 | end 34 | -------------------------------------------------------------------------------- /bin/update: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require 'fileutils' 3 | include FileUtils 4 | 5 | # path to your application root. 6 | APP_ROOT = File.expand_path('..', __dir__) 7 | 8 | def system!(*args) 9 | system(*args) || abort("\n== Command #{args} failed ==") 10 | end 11 | 12 | chdir APP_ROOT do 13 | # This script is a way to update your development environment automatically. 14 | # Add necessary update steps to this file. 15 | 16 | puts '== Installing dependencies ==' 17 | system! 'gem install bundler --conservative' 18 | system('bundle check') || system!('bundle install') 19 | 20 | # Install JavaScript dependencies if using Yarn 21 | # system('bin/yarn') 22 | 23 | puts "\n== Updating database ==" 24 | system! 'bin/rails db:migrate' 25 | 26 | puts "\n== Removing old logs and tempfiles ==" 27 | system! 'bin/rails log:clear tmp:clear' 28 | 29 | puts "\n== Restarting application server ==" 30 | system! 'bin/rails restart' 31 | end 32 | -------------------------------------------------------------------------------- /bin/yarn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | APP_ROOT = File.expand_path('..', __dir__) 3 | Dir.chdir(APP_ROOT) do 4 | yarn = ENV["PATH"].split(File::PATH_SEPARATOR). 5 | select { |dir| File.expand_path(dir) != __dir__ }. 6 | product(["yarn", "yarn.cmd", "yarn.ps1"]). 7 | map { |dir, file| File.expand_path(file, dir) }. 8 | find { |file| File.executable?(file) } 9 | 10 | if yarn 11 | exec yarn, *ARGV 12 | else 13 | $stderr.puts "Yarn executable was not detected in the system." 14 | $stderr.puts "Download Yarn at https://yarnpkg.com/en/docs/install" 15 | exit 1 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /config.ru: -------------------------------------------------------------------------------- 1 | # This file is used by Rack-based servers to start the application. 2 | 3 | require_relative "config/environment" 4 | 5 | run Rails.application 6 | Rails.application.load_server 7 | -------------------------------------------------------------------------------- /config/application.rb: -------------------------------------------------------------------------------- 1 | require_relative "boot" 2 | 3 | require "rails/all" 4 | 5 | # Require the gems listed in Gemfile, including any gems 6 | # you've limited to :test, :development, or :production. 7 | Bundler.require(*Rails.groups) 8 | 9 | module Zikbar 10 | class Application < Rails::Application 11 | # Initialize configuration defaults for originally generated Rails version. 12 | config.load_defaults 7.0 13 | 14 | # Configuration for the application, engines, and railties goes here. 15 | # 16 | # These settings can be overridden in specific environments using the files 17 | # in config/environments, which are processed later. 18 | # 19 | # config.time_zone = "Central Time (US & Canada)" 20 | # config.eager_load_paths << Rails.root.join("extras") 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /config/boot.rb: -------------------------------------------------------------------------------- 1 | ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__) 2 | 3 | require "bundler/setup" # Set up gems listed in the Gemfile. 4 | require "bootsnap/setup" # Speed up boot time by caching expensive operations. 5 | -------------------------------------------------------------------------------- /config/cable.yml: -------------------------------------------------------------------------------- 1 | development: 2 | adapter: async 3 | 4 | test: 5 | adapter: test 6 | 7 | production: 8 | adapter: redis 9 | url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %> 10 | channel_prefix: zikbar_production 11 | -------------------------------------------------------------------------------- /config/database.yml: -------------------------------------------------------------------------------- 1 | # SQLite version 3.x 2 | # gem install sqlite3 3 | # 4 | # Ensure the SQLite 3 gem is defined in your Gemfile 5 | # gem 'sqlite3' 6 | # 7 | default: &default 8 | adapter: sqlite3 9 | pool: 5 10 | timeout: 5000 11 | 12 | development: 13 | <<: *default 14 | database: db/development.sqlite3 15 | 16 | # Warning: The database defined as "test" will be erased and 17 | # re-generated from your development database when you run "rake". 18 | # Do not set this db to the same as development or production. 19 | test: 20 | <<: *default 21 | database: db/test.sqlite3 22 | -------------------------------------------------------------------------------- /config/environment.rb: -------------------------------------------------------------------------------- 1 | # Load the Rails application. 2 | require_relative "application" 3 | 4 | # Initialize the Rails application. 5 | Rails.application.initialize! 6 | -------------------------------------------------------------------------------- /config/environments/development.rb: -------------------------------------------------------------------------------- 1 | require "active_support/core_ext/integer/time" 2 | 3 | Rails.application.configure do 4 | # Settings specified here will take precedence over those in config/application.rb. 5 | 6 | # In the development environment your application's code is reloaded any time 7 | # it changes. This slows down response time but is perfect for development 8 | # since you don't have to restart the web server when you make code changes. 9 | config.cache_classes = false 10 | 11 | # Do not eager load code on boot. 12 | config.eager_load = false 13 | 14 | # Show full error reports. 15 | config.consider_all_requests_local = true 16 | 17 | # Enable server timing 18 | config.server_timing = true 19 | 20 | # Enable/disable caching. By default caching is disabled. 21 | # Run rails dev:cache to toggle caching. 22 | if Rails.root.join("tmp/caching-dev.txt").exist? 23 | config.action_controller.perform_caching = true 24 | config.action_controller.enable_fragment_cache_logging = true 25 | 26 | config.cache_store = :memory_store 27 | config.public_file_server.headers = { 28 | "Cache-Control" => "public, max-age=#{2.days.to_i}" 29 | } 30 | else 31 | config.action_controller.perform_caching = false 32 | 33 | config.cache_store = :null_store 34 | end 35 | 36 | # Store uploaded files on the local file system (see config/storage.yml for options). 37 | config.active_storage.service = :local 38 | 39 | # Don't care if the mailer can't send. 40 | config.action_mailer.raise_delivery_errors = false 41 | 42 | config.action_mailer.perform_caching = false 43 | 44 | # Print deprecation notices to the Rails logger. 45 | config.active_support.deprecation = :log 46 | 47 | # Raise exceptions for disallowed deprecations. 48 | config.active_support.disallowed_deprecation = :raise 49 | 50 | # Tell Active Support which deprecation messages to disallow. 51 | config.active_support.disallowed_deprecation_warnings = [] 52 | 53 | # Raise an error on page load if there are pending migrations. 54 | config.active_record.migration_error = :page_load 55 | 56 | # Highlight code that triggered database queries in logs. 57 | config.active_record.verbose_query_logs = true 58 | 59 | # Suppress logger output for asset requests. 60 | config.assets.quiet = true 61 | 62 | # Raises error for missing translations. 63 | # config.i18n.raise_on_missing_translations = true 64 | 65 | # Annotate rendered view with file names. 66 | # config.action_view.annotate_rendered_view_with_filenames = true 67 | 68 | # Uncomment if you wish to allow Action Cable access from any origin. 69 | # config.action_cable.disable_request_forgery_protection = true 70 | end 71 | -------------------------------------------------------------------------------- /config/environments/production.rb: -------------------------------------------------------------------------------- 1 | require "active_support/core_ext/integer/time" 2 | 3 | Rails.application.configure do 4 | # Settings specified here will take precedence over those in config/application.rb. 5 | 6 | # Code is not reloaded between requests. 7 | config.cache_classes = true 8 | 9 | # Eager load code on boot. This eager loads most of Rails and 10 | # your application in memory, allowing both threaded web servers 11 | # and those relying on copy on write to perform better. 12 | # Rake tasks automatically ignore this option for performance. 13 | config.eager_load = true 14 | 15 | # Full error reports are disabled and caching is turned on. 16 | config.consider_all_requests_local = false 17 | config.action_controller.perform_caching = true 18 | 19 | # Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"] 20 | # or in config/master.key. This key is used to decrypt credentials (and other encrypted files). 21 | # config.require_master_key = true 22 | 23 | # Disable serving static files from the `/public` folder by default since 24 | # Apache or NGINX already handles this. 25 | config.public_file_server.enabled = ENV["RAILS_SERVE_STATIC_FILES"].present? 26 | 27 | # Compress CSS using a preprocessor. 28 | # config.assets.css_compressor = :sass 29 | 30 | # Do not fallback to assets pipeline if a precompiled asset is missed. 31 | config.assets.compile = false 32 | 33 | # Enable serving of images, stylesheets, and JavaScripts from an asset server. 34 | # config.asset_host = "http://assets.example.com" 35 | 36 | # Specifies the header that your server uses for sending files. 37 | # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for Apache 38 | # config.action_dispatch.x_sendfile_header = "X-Accel-Redirect" # for NGINX 39 | 40 | # Store uploaded files on the local file system (see config/storage.yml for options). 41 | config.active_storage.service = :local 42 | 43 | # Mount Action Cable outside main process or domain. 44 | # config.action_cable.mount_path = nil 45 | # config.action_cable.url = "wss://example.com/cable" 46 | # config.action_cable.allowed_request_origins = [ "http://example.com", /http:\/\/example.*/ ] 47 | 48 | # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. 49 | # config.force_ssl = true 50 | 51 | # Include generic and useful information about system operation, but avoid logging too much 52 | # information to avoid inadvertent exposure of personally identifiable information (PII). 53 | config.log_level = :info 54 | 55 | # Prepend all log lines with the following tags. 56 | config.log_tags = [ :request_id ] 57 | 58 | # Use a different cache store in production. 59 | # config.cache_store = :mem_cache_store 60 | 61 | # Use a real queuing backend for Active Job (and separate queues per environment). 62 | # config.active_job.queue_adapter = :resque 63 | # config.active_job.queue_name_prefix = "zikbar_production" 64 | 65 | config.action_mailer.perform_caching = false 66 | 67 | # Ignore bad email addresses and do not raise email delivery errors. 68 | # Set this to true and configure the email server for immediate delivery to raise delivery errors. 69 | # config.action_mailer.raise_delivery_errors = false 70 | 71 | # Enable locale fallbacks for I18n (makes lookups for any locale fall back to 72 | # the I18n.default_locale when a translation cannot be found). 73 | config.i18n.fallbacks = true 74 | 75 | # Don't log any deprecations. 76 | config.active_support.report_deprecations = false 77 | 78 | # Use default logging formatter so that PID and timestamp are not suppressed. 79 | config.log_formatter = ::Logger::Formatter.new 80 | 81 | # Use a different logger for distributed setups. 82 | # require "syslog/logger" 83 | # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new "app-name") 84 | 85 | if ENV["RAILS_LOG_TO_STDOUT"].present? 86 | logger = ActiveSupport::Logger.new(STDOUT) 87 | logger.formatter = config.log_formatter 88 | config.logger = ActiveSupport::TaggedLogging.new(logger) 89 | end 90 | 91 | # Do not dump schema after migrations. 92 | config.active_record.dump_schema_after_migration = false 93 | end 94 | -------------------------------------------------------------------------------- /config/environments/test.rb: -------------------------------------------------------------------------------- 1 | require "active_support/core_ext/integer/time" 2 | 3 | # The test environment is used exclusively to run your application's 4 | # test suite. You never need to work with it otherwise. Remember that 5 | # your test database is "scratch space" for the test suite and is wiped 6 | # and recreated between test runs. Don't rely on the data there! 7 | 8 | Rails.application.configure do 9 | # Settings specified here will take precedence over those in config/application.rb. 10 | 11 | # Turn false under Spring and add config.action_view.cache_template_loading = true. 12 | config.cache_classes = true 13 | 14 | # Eager loading loads your whole application. When running a single test locally, 15 | # this probably isn't necessary. It's a good idea to do in a continuous integration 16 | # system, or in some way before deploying your code. 17 | config.eager_load = ENV["CI"].present? 18 | 19 | # Configure public file server for tests with Cache-Control for performance. 20 | config.public_file_server.enabled = true 21 | config.public_file_server.headers = { 22 | "Cache-Control" => "public, max-age=#{1.hour.to_i}" 23 | } 24 | 25 | # Show full error reports and disable caching. 26 | config.consider_all_requests_local = true 27 | config.action_controller.perform_caching = false 28 | config.cache_store = :null_store 29 | 30 | # Raise exceptions instead of rendering exception templates. 31 | config.action_dispatch.show_exceptions = false 32 | 33 | # Disable request forgery protection in test environment. 34 | config.action_controller.allow_forgery_protection = false 35 | 36 | # Store uploaded files on the local file system in a temporary directory. 37 | config.active_storage.service = :test 38 | 39 | config.action_mailer.perform_caching = false 40 | 41 | # Tell Action Mailer not to deliver emails to the real world. 42 | # The :test delivery method accumulates sent emails in the 43 | # ActionMailer::Base.deliveries array. 44 | config.action_mailer.delivery_method = :test 45 | 46 | # Print deprecation notices to the stderr. 47 | config.active_support.deprecation = :stderr 48 | 49 | # Raise exceptions for disallowed deprecations. 50 | config.active_support.disallowed_deprecation = :raise 51 | 52 | # Tell Active Support which deprecation messages to disallow. 53 | config.active_support.disallowed_deprecation_warnings = [] 54 | 55 | # Raises error for missing translations. 56 | # config.i18n.raise_on_missing_translations = true 57 | 58 | # Annotate rendered view with file names. 59 | # config.action_view.annotate_rendered_view_with_filenames = true 60 | end 61 | -------------------------------------------------------------------------------- /config/initializers/application_controller_renderer.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # ActiveSupport::Reloader.to_prepare do 4 | # ApplicationController.renderer.defaults.merge!( 5 | # http_host: 'example.org', 6 | # https: false 7 | # ) 8 | # end 9 | -------------------------------------------------------------------------------- /config/initializers/assets.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Version of your assets, change this if you want to expire all your assets. 4 | Rails.application.config.assets.version = "1.0" 5 | 6 | # Add additional assets to the asset load path. 7 | # Rails.application.config.assets.paths << Emoji.images_path 8 | 9 | # Precompile additional assets. 10 | # application.js, application.css, and all non-JS/CSS in the app/assets 11 | # folder are already added. 12 | # Rails.application.config.assets.precompile += %w( admin.js admin.css ) 13 | -------------------------------------------------------------------------------- /config/initializers/backtrace_silencers.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces. 4 | # Rails.backtrace_cleaner.add_silencer { |line| /my_noisy_library/.match?(line) } 5 | 6 | # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code 7 | # by setting BACKTRACE=1 before calling your invocation, like "BACKTRACE=1 ./bin/rails runner 'MyClass.perform'". 8 | Rails.backtrace_cleaner.remove_silencers! if ENV["BACKTRACE"] 9 | -------------------------------------------------------------------------------- /config/initializers/content_security_policy.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Define an application-wide content security policy. 4 | # See the Securing Rails Applications Guide for more information: 5 | # https://guides.rubyonrails.org/security.html#content-security-policy-header 6 | 7 | # Rails.application.configure do 8 | # config.content_security_policy do |policy| 9 | # policy.default_src :self, :https 10 | # policy.font_src :self, :https, :data 11 | # policy.img_src :self, :https, :data 12 | # policy.object_src :none 13 | # policy.script_src :self, :https 14 | # policy.style_src :self, :https 15 | # # Specify URI for violation reports 16 | # # policy.report_uri "/csp-violation-report-endpoint" 17 | # end 18 | # 19 | # # Generate session nonces for permitted importmap and inline scripts 20 | # config.content_security_policy_nonce_generator = ->(request) { request.session.id.to_s } 21 | # config.content_security_policy_nonce_directives = %w(script-src) 22 | # 23 | # # Report violations without enforcing the policy. 24 | # # config.content_security_policy_report_only = true 25 | # end 26 | -------------------------------------------------------------------------------- /config/initializers/cookies_serializer.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Specify a serializer for the signed and encrypted cookie jars. 4 | # Valid options are :json, :marshal, and :hybrid. 5 | Rails.application.config.action_dispatch.cookies_serializer = :json 6 | -------------------------------------------------------------------------------- /config/initializers/filter_parameter_logging.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Configure parameters to be filtered from the log file. Use this to limit dissemination of 4 | # sensitive information. See the ActiveSupport::ParameterFilter documentation for supported 5 | # notations and behaviors. 6 | Rails.application.config.filter_parameters += [ 7 | :passw, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn 8 | ] 9 | -------------------------------------------------------------------------------- /config/initializers/inflections.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Add new inflection rules using the following format. Inflections 4 | # are locale specific, and you may define rules for as many different 5 | # locales as you wish. All of these examples are active by default: 6 | # ActiveSupport::Inflector.inflections(:en) do |inflect| 7 | # inflect.plural /^(ox)$/i, "\\1en" 8 | # inflect.singular /^(ox)en/i, "\\1" 9 | # inflect.irregular "person", "people" 10 | # inflect.uncountable %w( fish sheep ) 11 | # end 12 | 13 | # These inflection rules are supported but not enabled by default: 14 | # ActiveSupport::Inflector.inflections(:en) do |inflect| 15 | # inflect.acronym "RESTful" 16 | # end 17 | -------------------------------------------------------------------------------- /config/initializers/mime_types.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Add new mime types for use in respond_to blocks: 4 | # Mime::Type.register "text/richtext", :rtf 5 | -------------------------------------------------------------------------------- /config/initializers/permissions_policy.rb: -------------------------------------------------------------------------------- 1 | # Define an application-wide HTTP permissions policy. For further 2 | # information see https://developers.google.com/web/updates/2018/06/feature-policy 3 | # 4 | # Rails.application.config.permissions_policy do |f| 5 | # f.camera :none 6 | # f.gyroscope :none 7 | # f.microphone :none 8 | # f.usb :none 9 | # f.fullscreen :self 10 | # f.payment :self, "https://secure.example.com" 11 | # end 12 | -------------------------------------------------------------------------------- /config/initializers/session_store.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | Rails.application.config.session_store :cookie_store, key: '_zikbar_session' 4 | -------------------------------------------------------------------------------- /config/initializers/wrap_parameters.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # This file contains settings for ActionController::ParamsWrapper which 4 | # is enabled by default. 5 | 6 | # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array. 7 | ActiveSupport.on_load(:action_controller) do 8 | wrap_parameters format: [:json] 9 | end 10 | 11 | # To enable root element in JSON for ActiveRecord objects. 12 | # ActiveSupport.on_load(:active_record) do 13 | # self.include_root_in_json = true 14 | # end 15 | -------------------------------------------------------------------------------- /config/locales/en.yml: -------------------------------------------------------------------------------- 1 | # Files in the config/locales directory are used for internationalization 2 | # and are automatically loaded by Rails. If you want to use locales other 3 | # than English, add the necessary files in this directory. 4 | # 5 | # To use the locales, use `I18n.t`: 6 | # 7 | # I18n.t 'hello' 8 | # 9 | # In views, this is aliased to just `t`: 10 | # 11 | # <%= t('hello') %> 12 | # 13 | # To use a different locale, set it with `I18n.locale`: 14 | # 15 | # I18n.locale = :es 16 | # 17 | # This would use the information in config/locales/es.yml. 18 | # 19 | # The following keys must be escaped otherwise they will not be retrieved by 20 | # the default I18n backend: 21 | # 22 | # true, false, on, off, yes, no 23 | # 24 | # Instead, surround them with single quotes. 25 | # 26 | # en: 27 | # 'true': 'foo' 28 | # 29 | # To learn more, please read the Rails Internationalization guide 30 | # available at https://guides.rubyonrails.org/i18n.html. 31 | 32 | en: 33 | hello: "Hello world" 34 | -------------------------------------------------------------------------------- /config/puma.rb: -------------------------------------------------------------------------------- 1 | # Puma can serve each request in a thread from an internal thread pool. 2 | # The `threads` method setting takes two numbers: a minimum and maximum. 3 | # Any libraries that use thread pools should be configured to match 4 | # the maximum value specified for Puma. Default is set to 5 threads for minimum 5 | # and maximum; this matches the default thread size of Active Record. 6 | # 7 | max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 } 8 | min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count } 9 | threads min_threads_count, max_threads_count 10 | 11 | # Specifies the `worker_timeout` threshold that Puma will use to wait before 12 | # terminating a worker in development environments. 13 | # 14 | worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development" 15 | 16 | # Specifies the `port` that Puma will listen on to receive requests; default is 3000. 17 | # 18 | port ENV.fetch("PORT") { 3000 } 19 | 20 | # Specifies the `environment` that Puma will run in. 21 | # 22 | environment ENV.fetch("RAILS_ENV") { "development" } 23 | 24 | # Specifies the `pidfile` that Puma will use. 25 | pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" } 26 | 27 | # Specifies the number of `workers` to boot in clustered mode. 28 | # Workers are forked web server processes. If using threads and workers together 29 | # the concurrency of the application would be max `threads` * `workers`. 30 | # Workers do not work on JRuby or Windows (both of which do not support 31 | # processes). 32 | # 33 | # workers ENV.fetch("WEB_CONCURRENCY") { 2 } 34 | 35 | # Use the `preload_app!` method when specifying a `workers` number. 36 | # This directive tells Puma to first boot the application and load code 37 | # before forking the application. This takes advantage of Copy On Write 38 | # process behavior so workers use less memory. 39 | # 40 | # preload_app! 41 | 42 | # Allow puma to be restarted by `rails restart` command. 43 | plugin :tmp_restart 44 | -------------------------------------------------------------------------------- /config/routes.rb: -------------------------------------------------------------------------------- 1 | Rails.application.routes.draw do 2 | root to: 'artists#index' 3 | end 4 | -------------------------------------------------------------------------------- /config/secrets.yml: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Your secret key is used for verifying the integrity of signed cookies. 4 | # If you change this key, all old signed cookies will become invalid! 5 | 6 | # Make sure the secret is at least 30 characters and all random, 7 | # no regular words or you'll be exposed to dictionary attacks. 8 | # You can use `rails secret` to generate a secure secret key. 9 | 10 | # Make sure the secrets in this file are kept private 11 | # if you're sharing your code publicly. 12 | 13 | development: 14 | secret_key_base: e4b125c43e7703a9ddc39f093e77c1a896537510db23554d80f2bf63aadb9e8d237d1a3872a7015a41539a99cbe7f253ea28fae74390188f77eb6c4da9f3b09e 15 | 16 | test: 17 | secret_key_base: 01a338f493ab76d22c49c72dd1c64e88176c46e80aeeb7f62ab48c8fa6ec9d0f2d8cec8d754f6feb46f7d7accf559117c09d9328394317078d7c033e9e3cba08 18 | 19 | # Do not keep production secrets in the repository, 20 | # instead read values from the environment. 21 | production: 22 | secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> 23 | -------------------------------------------------------------------------------- /config/spring.rb: -------------------------------------------------------------------------------- 1 | %w( 2 | .ruby-version 3 | .rbenv-vars 4 | tmp/restart.txt 5 | tmp/caching-dev.txt 6 | ).each { |path| Spring.watch(path) } 7 | -------------------------------------------------------------------------------- /config/storage.yml: -------------------------------------------------------------------------------- 1 | test: 2 | service: Disk 3 | root: <%= Rails.root.join("tmp/storage") %> 4 | 5 | local: 6 | service: Disk 7 | root: <%= Rails.root.join("storage") %> 8 | 9 | # Use rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key) 10 | # amazon: 11 | # service: S3 12 | # access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %> 13 | # secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %> 14 | # region: us-east-1 15 | # bucket: your_own_bucket 16 | 17 | # Remember not to checkin your GCS keyfile to a repository 18 | # google: 19 | # service: GCS 20 | # project: your_project 21 | # credentials: <%= Rails.root.join("path/to/gcs.keyfile") %> 22 | # bucket: your_own_bucket 23 | 24 | # Use rails credentials:edit to set the Azure Storage secret (as azure_storage:storage_access_key) 25 | # microsoft: 26 | # service: AzureStorage 27 | # storage_account_name: your_account_name 28 | # storage_access_key: <%= Rails.application.credentials.dig(:azure_storage, :storage_access_key) %> 29 | # container: your_container_name 30 | 31 | # mirror: 32 | # service: Mirror 33 | # primary: local 34 | # mirrors: [ amazon, google, microsoft ] 35 | -------------------------------------------------------------------------------- /db/migrate/20151104004615_create_artists.rb: -------------------------------------------------------------------------------- 1 | class CreateArtists < ActiveRecord::Migration[5.1] 2 | def change 3 | create_table :artists do |t| 4 | t.string :name 5 | t.string :genre 6 | t.boolean :available, default: true 7 | 8 | t.timestamps null: false 9 | end 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /db/migrate/20151104072105_create_labels.rb: -------------------------------------------------------------------------------- 1 | class CreateLabels < ActiveRecord::Migration[5.1] 2 | def change 3 | create_table :labels do |t| 4 | t.string :name 5 | 6 | t.timestamps null: false 7 | end 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /db/migrate/20151104073745_add_label_reference_to_artists.rb: -------------------------------------------------------------------------------- 1 | class AddLabelReferenceToArtists < ActiveRecord::Migration[5.1] 2 | def change 3 | add_reference :artists, :label, index: true, foreign_key: true 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /db/migrate/20170413053040_add_picture_url_to_artists.rb: -------------------------------------------------------------------------------- 1 | class AddPictureUrlToArtists < ActiveRecord::Migration[5.1] 2 | def change 3 | add_column :artists, :picture_url, :string 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /db/schema.rb: -------------------------------------------------------------------------------- 1 | # This file is auto-generated from the current state of the database. Instead 2 | # of editing this file, please use the migrations feature of Active Record to 3 | # incrementally modify your database, and then regenerate this schema definition. 4 | # 5 | # This file is the source Rails uses to define your schema when running `bin/rails 6 | # db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to 7 | # be faster and is potentially less error prone than running all of your 8 | # migrations from scratch. Old migrations may fail to apply correctly if those 9 | # migrations use external dependencies or application code. 10 | # 11 | # It's strongly recommended that you check this file into your version control system. 12 | 13 | ActiveRecord::Schema[7.0].define(version: 2017_04_13_053040) do 14 | create_table "artists", force: :cascade do |t| 15 | t.string "name" 16 | t.string "genre" 17 | t.boolean "available", default: true 18 | t.datetime "created_at", precision: nil, null: false 19 | t.datetime "updated_at", precision: nil, null: false 20 | t.integer "label_id" 21 | t.string "picture_url" 22 | t.index ["label_id"], name: "index_artists_on_label_id" 23 | end 24 | 25 | create_table "labels", force: :cascade do |t| 26 | t.string "name" 27 | t.datetime "created_at", precision: nil, null: false 28 | t.datetime "updated_at", precision: nil, null: false 29 | end 30 | 31 | add_foreign_key "artists", "labels" 32 | end 33 | -------------------------------------------------------------------------------- /db/seeds.rb: -------------------------------------------------------------------------------- 1 | puts 'Cleaning database...' 2 | Artist.destroy_all 3 | Label.destroy_all 4 | 5 | puts 'Creating labels...' 6 | columbia = Label.create!(name: 'Columbia') 7 | mute_records = Label.create!(name: 'Mute Records') 8 | roadrunner = Label.create!(name: 'Roadrunner') 9 | 10 | puts 'Creating artists...' 11 | # Rock 12 | Artist.create!( 13 | name: 'Porcupine Tree', genre: 'Rock', label: roadrunner, available: false, 14 | picture_url: 'https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftse4.mm.bing.net%2Fth%3Fid%3DOIP.T-rm9YxujpebsABCloZgCQHaHa%26pid%3DApi&f=1' 15 | ) 16 | # Metal 17 | Artist.create!( 18 | name: 'Opeth', genre: 'Metal', label: roadrunner, available: true, 19 | picture_url: 'https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftse4.mm.bing.net%2Fth%3Fid%3DOIP.6G3Qb59qmRAcF8UTyQ4iEAHaHa%26pid%3DApi&f=1' 20 | ) 21 | Artist.create!( 22 | name: 'Iron Maiden', genre: 'Metal',label: columbia, available: false, 23 | picture_url: 'https://s-media-cache-ak0.pinimg.com/originals/02/86/ab/0286ab666e7e709a34ed9c3a553436df.jpg' 24 | ) 25 | # Electronica 26 | Artist.create!( 27 | name: 'Daft Punk', genre: 'Electronica', label: columbia, available: true, 28 | picture_url: 'https://a4-images.myspacecdn.com/images03/2/85a286a4bbe84b56a6d57b1e5bd03ef4/300x300.jpg' 29 | ) 30 | Artist.create!( 31 | name: 'Moderat', genre: 'Electronica', label: mute_records, available: true, 32 | picture_url: 'https://www.residentadvisor.net/images/reviews/2016/moderat-iii.jpg' 33 | ) 34 | 35 | puts 'Finished!' 36 | -------------------------------------------------------------------------------- /lib/assets/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cveneziani/query-objects-example/7ad463596a73f70eeef580d4d72f74115faedbd9/lib/assets/.keep -------------------------------------------------------------------------------- /lib/tasks/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cveneziani/query-objects-example/7ad463596a73f70eeef580d4d72f74115faedbd9/lib/tasks/.keep -------------------------------------------------------------------------------- /lib/tasks/benchmark.rake: -------------------------------------------------------------------------------- 1 | desc 'Benchmark query object implementations' 2 | task benchmark: :environment do 3 | Benchmark.ips do |x| 4 | x.report('delegator -- without model') { ArtistQuery.relation } 5 | x.report('delegator -- with model') { ArtistQuery.relation(Artist.all) } 6 | x.report('extend -- without model') { Extending::ArtistQuery.relation } 7 | x.report('extend -- with model') { Extending::ArtistQuery.relation(Artist.all) } 8 | 9 | x.compare! 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /log/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cveneziani/query-objects-example/7ad463596a73f70eeef580d4d72f74115faedbd9/log/.keep -------------------------------------------------------------------------------- /public/404.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | The page you were looking for doesn't exist (404) 5 | 6 | 55 | 56 | 57 | 58 | 59 |
60 |
61 |

The page you were looking for doesn't exist.

62 |

You may have mistyped the address or the page may have moved.

63 |
64 |

If you are the application owner check the logs for more information.

65 |
66 | 67 | 68 | -------------------------------------------------------------------------------- /public/422.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | The change you wanted was rejected (422) 5 | 6 | 55 | 56 | 57 | 58 | 59 |
60 |
61 |

The change you wanted was rejected.

62 |

Maybe you tried to change something you didn't have access to.

63 |
64 |

If you are the application owner check the logs for more information.

65 |
66 | 67 | 68 | -------------------------------------------------------------------------------- /public/500.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | We're sorry, but something went wrong (500) 5 | 6 | 55 | 56 | 57 | 58 | 59 |
60 |
61 |

We're sorry, but something went wrong.

62 |
63 |

If you are the application owner check the logs for more information.

64 |
65 | 66 | 67 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cveneziani/query-objects-example/7ad463596a73f70eeef580d4d72f74115faedbd9/public/favicon.ico -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # See http://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file 2 | # 3 | # To ban all spiders from the entire site uncomment the next two lines: 4 | # User-agent: * 5 | # Disallow: / 6 | -------------------------------------------------------------------------------- /services.rb: -------------------------------------------------------------------------------- 1 | module Service 2 | extend ActiveSupport::Concern 3 | 4 | included do 5 | attr_reader :params 6 | end 7 | 8 | class_methods do 9 | def call(*args) 10 | new(*args).call 11 | end 12 | end 13 | 14 | def initialize(params = {}) 15 | @params = params 16 | 17 | params.each do |key, value| 18 | next if respond_to?(key, true) 19 | 20 | define_singleton_method(key) { value } 21 | end 22 | end 23 | end 24 | 25 | module Groups 26 | class Create 27 | include Service 28 | 29 | def call 30 | #do_something 31 | end 32 | 33 | end 34 | end 35 | 36 | 37 | 38 | 39 | def call 40 | group = Group.new attributes 41 | contract = GroupContract.new(group) 42 | 43 | fail ValidationError.new(contract) unless contract.validate 44 | 45 | album.save 46 | album 47 | end 48 | 49 | @form = SongForm.new(Song.new) 50 | So you need to know your entity. Where the initialization of the entity should be done on the service 51 | 52 | At the end I return the album to avoid the true/false pb and when I create an object I like to have it in response. 53 | 54 | 55 | def create 56 | Groups::Create.call attributes: group_attributes 57 | rescue ValidationError 58 | render :new 59 | end 60 | -------------------------------------------------------------------------------- /spec/queries/artist_query_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rails_helper' 2 | require 'support/shared_examples/query_object' 3 | 4 | describe 'ArtistQuery' do 5 | subject(:query) { ArtistQuery.relation } 6 | subject(:association_query) { ArtistQuery.relation(awesome_label.artists) } 7 | 8 | it_behaves_like 'query object - base' 9 | it_behaves_like 'query object - not chaining with AR contidions in between' 10 | end 11 | -------------------------------------------------------------------------------- /spec/queries/chaining/artist_query_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rails_helper' 2 | require 'support/shared_examples/query_object' 3 | 4 | describe 'Chaining::ArtistQuery' do 5 | subject(:query) { Chaining::ArtistQuery.relation } 6 | subject(:association_query) { Chaining::ArtistQuery.relation(awesome_label.artists) } 7 | 8 | it_behaves_like 'query object - base' 9 | it_behaves_like 'query object - chaining with AR contidions in between' 10 | end 11 | -------------------------------------------------------------------------------- /spec/queries/extending/artist_query_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rails_helper' 2 | require 'support/shared_examples/query_object' 3 | 4 | describe 'Extending::ArtistQuery' do 5 | subject(:query) { Extending::ArtistQuery.relation } 6 | subject(:association_query) { Extending::ArtistQuery.relation(awesome_label.artists) } 7 | 8 | it_behaves_like 'query object - base' 9 | it_behaves_like 'query object - chaining with AR contidions in between' 10 | end 11 | -------------------------------------------------------------------------------- /spec/rails_helper.rb: -------------------------------------------------------------------------------- 1 | # This file is copied to spec/ when you run 'rails generate rspec:install' 2 | ENV["RAILS_ENV"] ||= 'test' 3 | require 'spec_helper' 4 | require File.expand_path("../../config/environment", __FILE__) 5 | require 'rspec/rails' 6 | # Add additional requires below this line. Rails is not loaded until this point! 7 | 8 | # Requires supporting ruby files with custom matchers and macros, etc, in 9 | # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are 10 | # run as spec files by default. This means that files in spec/support that end 11 | # in _spec.rb will both be required and run as specs, causing the specs to be 12 | # run twice. It is recommended that you do not name files matching this glob to 13 | # end with _spec.rb. You can configure this pattern with the --pattern 14 | # option on the command line or in ~/.rspec, .rspec or `.rspec-local`. 15 | # 16 | # The following line is provided for convenience purposes. It has the downside 17 | # of increasing the boot-up time by auto-requiring all files in the support 18 | # directory. Alternatively, in the individual `*_spec.rb` files, manually 19 | # require only the support files necessary. 20 | # 21 | # Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } 22 | 23 | # Checks for pending migrations before tests are run. 24 | # If you are not using ActiveRecord, you can remove this line. 25 | ActiveRecord::Migration.maintain_test_schema! 26 | 27 | RSpec.configure do |config| 28 | # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures 29 | config.fixture_path = "#{::Rails.root}/spec/fixtures" 30 | 31 | # If you're not using ActiveRecord, or you'd prefer not to run each of your 32 | # examples within a transaction, remove the following line or assign false 33 | # instead of true. 34 | config.use_transactional_fixtures = true 35 | 36 | # RSpec Rails can automatically mix in different behaviours to your tests 37 | # based on their file location, for example enabling you to call `get` and 38 | # `post` in specs under `spec/controllers`. 39 | # 40 | # You can disable this behaviour by removing the line below, and instead 41 | # explicitly tag your specs with their type, e.g.: 42 | # 43 | # RSpec.describe UsersController, :type => :controller do 44 | # # ... 45 | # end 46 | # 47 | # The different available types are documented in the features, such as in 48 | # https://relishapp.com/rspec/rspec-rails/docs 49 | config.infer_spec_type_from_file_location! 50 | end 51 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # This file was generated by the `rails generate rspec:install` command. Conventionally, all 2 | # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. 3 | # The generated `.rspec` file contains `--require spec_helper` which will cause this 4 | # file to always be loaded, without a need to explicitly require it in any files. 5 | # 6 | # Given that it is always loaded, you are encouraged to keep this file as 7 | # light-weight as possible. Requiring heavyweight dependencies from this file 8 | # will add to the boot time of your test suite on EVERY test run, even for an 9 | # individual file that may not need all of that loaded. Instead, consider making 10 | # a separate helper file that requires the additional dependencies and performs 11 | # the additional setup, and require it from the spec files that actually need it. 12 | # 13 | # The `.rspec` file also contains a few flags that are not defaults but that 14 | # users commonly want. 15 | # 16 | # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration 17 | RSpec.configure do |config| 18 | # rspec-expectations config goes here. You can use an alternate 19 | # assertion/expectation library such as wrong or the stdlib/minitest 20 | # assertions if you prefer. 21 | config.expect_with :rspec do |expectations| 22 | # This option will default to `true` in RSpec 4. It makes the `description` 23 | # and `failure_message` of custom matchers include text for helper methods 24 | # defined using `chain`, e.g.: 25 | # be_bigger_than(2).and_smaller_than(4).description 26 | # # => "be bigger than 2 and smaller than 4" 27 | # ...rather than: 28 | # # => "be bigger than 2" 29 | expectations.include_chain_clauses_in_custom_matcher_descriptions = true 30 | end 31 | 32 | # rspec-mocks config goes here. You can use an alternate test double 33 | # library (such as bogus or mocha) by changing the `mock_with` option here. 34 | config.mock_with :rspec do |mocks| 35 | # Prevents you from mocking or stubbing a method that does not exist on 36 | # a real object. This is generally recommended, and will default to 37 | # `true` in RSpec 4. 38 | mocks.verify_partial_doubles = true 39 | end 40 | 41 | # The settings below are suggested to provide a good initial experience 42 | # with RSpec, but feel free to customize to your heart's content. 43 | =begin 44 | # These two settings work together to allow you to limit a spec run 45 | # to individual examples or groups you care about by tagging them with 46 | # `:focus` metadata. When nothing is tagged with `:focus`, all examples 47 | # get run. 48 | config.filter_run :focus 49 | config.run_all_when_everything_filtered = true 50 | 51 | # Limits the available syntax to the non-monkey patched syntax that is recommended. 52 | # For more details, see: 53 | # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax 54 | # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ 55 | # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching 56 | config.disable_monkey_patching! 57 | 58 | # Many RSpec users commonly either run the entire suite or an individual 59 | # file, and it's useful to allow more verbose output when running an 60 | # individual spec file. 61 | if config.files_to_run.one? 62 | # Use the documentation formatter for detailed output, 63 | # unless a formatter has already been configured 64 | # (e.g. via a command-line flag). 65 | config.default_formatter = 'doc' 66 | end 67 | 68 | # Print the 10 slowest examples and example groups at the 69 | # end of the spec run, to help surface which specs are running 70 | # particularly slow. 71 | config.profile_examples = 10 72 | 73 | # Run specs in random order to surface order dependencies. If you find an 74 | # order dependency and want to debug it, you can fix the order by providing 75 | # the seed, which is printed after each run. 76 | # --seed 1234 77 | config.order = :random 78 | 79 | # Seed global randomization in this process using the `--seed` CLI option. 80 | # Setting this allows you to use `--seed` to deterministically reproduce 81 | # test failures related to randomization by passing the same `--seed` value 82 | # as the one that triggered the failure. 83 | Kernel.srand config.seed 84 | =end 85 | end 86 | -------------------------------------------------------------------------------- /spec/support/shared_contexts/query_object_models.rb: -------------------------------------------------------------------------------- 1 | RSpec.shared_context 'query object models' do 2 | let(:awesome_label) { Label.create!(name: "Awesome Records") } 3 | let(:flawless_label) { Label.create!(name: "Flawless-R") } 4 | 5 | let(:opeth) { Artist.create!(name: "Opeth", genre: "Metal", available: true, label: awesome_label) } 6 | let(:iron_maiden) { Artist.create!(name: "Iron Maiden", genre: "Metal", available: false, label: awesome_label) } 7 | let(:daft_punk) { Artist.create!(name: "Daft Punk", genre: "Electronics", available: true, label: flawless_label) } 8 | let(:britney_spears) { Artist.create!(name: "Britney Spears", genre: "Pop", available: false, label: flawless_label) } 9 | 10 | let!(:artists) do 11 | [opeth, iron_maiden, daft_punk, britney_spears] 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /spec/support/shared_examples/query_object.rb: -------------------------------------------------------------------------------- 1 | require 'support/shared_examples/query_object/base' 2 | require 'support/shared_examples/query_object/chaining' 3 | -------------------------------------------------------------------------------- /spec/support/shared_examples/query_object/base.rb: -------------------------------------------------------------------------------- 1 | require 'support/shared_contexts/query_object_models' 2 | 3 | RSpec.shared_examples 'query object - base' do 4 | include_context 'query object models' 5 | 6 | describe '#all' do 7 | it 'returns all artists' do 8 | expect(query.all).to eq(artists) 9 | end 10 | end 11 | 12 | describe '#available' do 13 | let(:expected_artists) do 14 | [opeth, daft_punk] 15 | end 16 | 17 | it 'returns only available artists' do 18 | expect(query.available).to eq(expected_artists) 19 | end 20 | end 21 | 22 | describe '#by_genre' do 23 | let(:expected_artists) do 24 | [opeth, iron_maiden] 25 | end 26 | 27 | it 'returns only artists matching given genre' do 28 | expect(query.by_genre('Metal')).to eq(expected_artists) 29 | end 30 | end 31 | 32 | describe '[associations] available artists belonging to a given label' do 33 | let(:expected_artists) do 34 | [opeth] 35 | end 36 | 37 | it 'returns only available artists for the label' do 38 | expect(association_query.available).to eq(expected_artists) 39 | end 40 | end 41 | 42 | describe '[chaining] scopes from the query object: #available and #by_genre' do 43 | let(:expected_artists) do 44 | [opeth] 45 | end 46 | 47 | it 'returns only available artists matching given genre' do 48 | expect(query.available.by_genre('Metal')).to eq(expected_artists) 49 | end 50 | end 51 | 52 | describe '[chaining] with ActiveRecord conditions: available sorted by name' do 53 | let(:expected_artists) do 54 | [daft_punk, opeth] 55 | end 56 | 57 | it 'returns only available artists and sorted by name' do 58 | expect(query.available.order(:name)).to eq(expected_artists) 59 | end 60 | end 61 | end 62 | -------------------------------------------------------------------------------- /spec/support/shared_examples/query_object/chaining.rb: -------------------------------------------------------------------------------- 1 | require 'support/shared_contexts/query_object_models' 2 | 3 | RSpec.shared_examples 'query object - chaining with AR contidions in between' do 4 | include_context 'query object models' 5 | 6 | describe '[chaining] with ActiveRecord conditions in between: sorted by name and available' do 7 | let(:expected_artists) do 8 | [daft_punk, opeth] 9 | end 10 | 11 | it 'returns only available artists and sorted by name' do 12 | expect(query.order(:name).available).to eq(expected_artists) 13 | end 14 | end 15 | end 16 | 17 | RSpec.shared_examples 'query object - not chaining with AR contidions in between' do 18 | include_context 'query object models' 19 | 20 | describe '[chaining] with ActiveRecord conditions in between: sorted by name and available' do 21 | let(:expected_artists) do 22 | [daft_punk, opeth] 23 | end 24 | 25 | it 'returns only available artists and sorted by name' do 26 | expect { query.order(:name).available }.to raise_error(NoMethodError, /undefined method `available' for #