Skip to content

Instantly share code, notes, and snippets.

View DamirSvrtan's full-sized avatar
🎖️

Damir Svrtan DamirSvrtan

🎖️
View GitHub Profile
require "rails_helper"
RSpec.describe PokemonsController do
describe "GET #index" do
it "renders the index template" do
get :index
expect(response.status).to eq(200)
expect(response).to render_template('index)
end
end

Hey everyone,

For a long time I've been writing boilerplate classes that have basically the same structure of initialization:

class UserRegistrationForm  
  def initialize(access_code)
    @access_code = access_code
  end
 

I save 10 seconds per class creation

This is how I usually write use-cases/service objects:

class UserRegistrationForm

  def self.call(access_code)
    new(access_code).call
  end
class MemberEligibleForTierRule
attr_reader :member, :tier
def initialize(member:, tier:)
@member, @tier = member, tier
end
def satisfied?
(flew_enough_miles? || flew_enough_segments?) && spent_enough_money?
end
module Workflows
 class ProductionEntity < BaseEntity
  attribute :id, AttrType::Strict::Integer
  attribute :title, AttrType::Strict::String
  attribute :logline, AttrType::Strict::String.optional
 end
end
class ProductionRepo < BaseRepo
entity_class ProductionEntity
def initialize(data_source: MovieProduction)
super
end
def find_by_id(id)
wrap(data_source.find_by_id(id))
end
class MovieProduction < ApplicationRecord
def self.search_by_title(title)
where('title ILIKE ?', "#{sanitize_sql_like(title)}%") }
end
end
class MovieProductionAPI
def self.search_by_title(title)
SwaggerClient::MovieProductionsApi.advanced_search(
search_param: title,
headers: { ... },
...
)
end
end
# using the default data source
repo = ProductionRepo.new
# swapping for a REST API data source
repo = ProductionRepo.new(data_source: MovieProductionAPI)
module Workflows
class OnboardProduction
include NetflixInteractor
def self.call(production_id:, repo: ProductionRepo.new)
super
end
def call
# validate inputs to check if it's okay to onboard a production?