Skip to content

Instantly share code, notes, and snippets.

View frostyblok's full-sized avatar
🏠
Working from home

Oluwakunle Fakorede frostyblok

🏠
Working from home
  • Andela
  • Lagos
View GitHub Profile
@frostyblok
frostyblok / subsriptions_controller.rb
Created May 27, 2020 19:59
Subscription Controller
module Admins
class SubscriptionsContoller < ApplicationController
# This is left empty because the instruction clearly says the subscriptions.html.erb
def index; end
# eager loading the subscription entity to include the plan associated data
def subscriptions
@subcriptions = Subscription.order(created_at: :desc).includes(:plan).page(params[:page]).per(100)
end
end
@frostyblok
frostyblok / subscriptions.css
Last active May 27, 2020 19:51
Subscription Display
/****** app/assets/stylesheets/admins/subscriptions.css ****/
body {
@media (min-width: 768px) {
.subscription {
display: grid;
}
}
@media (min-width: 1024px) {
The problem right now is due to N+1 queries, which means, our database is being flooded with 1, 000, 000 + 1 queries (Since we have a million subscriptions currently)—this is because Ruby on Rails lazy loading enabled by default. So to improve this, we eager load all the subscription entity with ‘includes()’. This allows to preload the plan associated data and thereby saving us some time. There are also other ways to eager load subscription entity with ‘eager_load()’ and ‘preload()’.
# lib/scripts/output_subscribers.rb

puts "email,monthly_price"

Subscription.includes(:plan).where.not(plans: {id: nil}).find_each do |subscription|

puts "#{subscription.email},#{subscription.plan.monthly_price}"

end
@frostyblok
frostyblok / objectRanker.js
Last active July 9, 2019 13:06
This gist returns objects in an array in order of their ranks, and also returns the average rank for each of object in an array
const testArr = [
{
name: 'Ade',
ranking: 1
},
{
name: 'Luqman',
ranking: 2
}
];
@frostyblok
frostyblok / objectRanker.js
Created July 9, 2019 13:05
This gist returns objects in an array in order of their ranks, and also returns the average rank for each of object in an array
const testArr = [
{
name: 'Ade',
ranking: 1
},
{
name: 'Luqman',
ranking: 2
}
];
import chai from 'chai';
import chaiHttp from 'chai-http';
import app from '../../../server';
describe('Get no article', () => {
it('should not get an article that does not exist', async () => {
const res = await chai.req(app)
.get('/api/v1/articles/234');
expect(res.status).to.be.equal(404);
expect(res.body.message).to.be.deep.equals('Article not found');
import chai from 'chai';
import chaiHttp from 'chai-http';
import app from '../../../server';
import data from '../model/ariticles';
describe('Get no article', () => {
it('should not get an article that does not exit', async () => {
const res = await chai.request(app)
.get('/api/v1/articles/234');
expect(res.body.status).to.be.equal(404);