Skip to content

Instantly share code, notes, and snippets.

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

Dmitry Gorodnichy dgorodnichy

🏠
Working from home
  • Krasnodar
View GitHub Profile
@dgorodnichy
dgorodnichy / facade.rb
Last active October 29, 2021 14:03
Facade pattern
class Dashboard
def initialize(filters)
@filters = filters
end
def call
users = FetchUsers.new(@filters).call
activities = FetchActivities.new(@filters).call
match_activities(users, activities
@dgorodnichy
dgorodnichy / view_object.rb
Created October 26, 2021 07:49
ViewObject example
class DateViewObject
attr_reader :date
def initialize(date)
@date = date
end
def us
date.strftime('%m/%d/%y')
end

Advertisement :)

  • pica - high quality and fast image resize in browser.
  • babelfish - developer friendly i18n with plurals support and easy syntax.

You will like those projects!

class NotifyPostAuthorAboutLikeActionScript
Result = Struct.new(:success?, :errors)
def initialize(post)
@post = post
end
def perform
notification = Notification.new(
user: @post.author,
# frozen_string_literal: true
require 'test_helper'
class NotifyPostAuthorAboutLikeActionScriptTest < ActiveSupport::TestCase
def setup
@user = users(:john_doe)
@post = posts(:lorem_ipsum)
end
module Api
class LikesController < ApplicationController
def update
@user = User.find(params["user_id"])
@post = Post.find(params["post_id"])
result = ToggleLikeActionScript.new(@user, @post).perform
if result.success?
render json: { isLiked: result.value }
class ToggleLikeActionScript
Result = Struct.new(:success?, :errors, :value)
def initialize(user, post)
@user = user
@post = post
end
def perform
like = @post.likes.find_or_initialize_by(user: @user)
require 'test_helper'
class ToggleLikeActionScriptTest < ActiveSupport::TestCase
def setup
@user = users(:john_doe)
@post = posts(:lorem_ipsum)
end
def test_it_creates_like
result = ToggleLikeActionScript.new(@user, @post).perform
module Api
class LikesController < ApplicationController
def update
@user = User.find(params['user_id'])
@post = Post.find(params['post_id'])
like = @post.likes.find_or_initialize_by(user: @user)
if like.persisted?
like.destroy!
# frozen_string_literal: true
module SqlView
class FilmsController < ApplicationController
def index
render json: find_films
end
private