Skip to content

Instantly share code, notes, and snippets.

View kermit-klein's full-sized avatar
🏠
Working from home

Ali Erbay kermit-klein

🏠
Working from home
View GitHub Profile
@kermit-klein
kermit-klein / application.rb
Last active July 30, 2020 21:51
cypress_application
class Application < Rails::Application
# [...] other code...
if Rails.env.test?
config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: %i[post]
end
end
end
describe("User can", () => {
beforeEach(() => {
cy.deleteAndSeedDatabase();
});
afterEach(() => {
cy.deleteDatabase();
});
it("visits home page and see list of places", () => {
@kermit-klein
kermit-klein / query.rb
Last active August 7, 2020 00:55
scopes_1
@albums = Album
.includes(:user)
.order("created_at DESC")
.where(category:"rock")
.where("minutes_length>55")
@kermit-klein
kermit-klein / album.rb
Last active August 6, 2020 10:41
scopes_2
#app/models/album.rb
class Album < ApplicationRecord
# [...] other code
scope :rock , -> { where(category:"rock")}
end
#app/models/album.rb
class Album < ApplicationRecord
# [...] other code
def self.rock
where(category:"rock")
end
end
@kermit-klein
kermit-klein / album_3.rb
Last active August 6, 2020 11:27
scopes_4
#app/models/album.rb
class Album < ApplicationRecord
# [...] other code
scope :rock , -> { where(category:"rock")}
scope :long , -> {where("song_count">11)}
end
@kermit-klein
kermit-klein / album_4.rb
Last active August 6, 2020 17:40
scopes_5
#app/models/album.rb
class Album < ApplicationRecord
# [...] other code
scope :genre , -> (category) { where(category:category) }
end
#app/controllers/albums_controller.rb
class AlbumsController < ActionController::Base
def index
@albums = Album.genre(params[:category])
end
end
class Article < ActiveRecord::Base
default_scope { where(published: true) }
end
Article.all # => SELECT * FROM articles WHERE published = true
RSpec.describe Album, type: :model do
let!(:album1) { create(:album, category: 'rock') }
let!(:album2) { create(:album, category: 'rock') }
let!(:album3) { create(:album, category: 'pop') }
let!(:album4) { create(:album, category: 'jazz') }
describe '.genre' do
it 'includes albums with category specified' do
expect(Album.genre('rock')).to include(album1, album2)
end