Skip to content

Instantly share code, notes, and snippets.

View sevos's full-sized avatar

Artur Roszczyk sevos

View GitHub Profile
@sevos
sevos / models.rb
Created October 6, 2011 11:29
Class-Oriented example
class Department < ActiveRecord::Base
has_one :stock
end
class Stock < ActiveRecord::Base
belongs_to :department
def update
update_attribute :value, Legacy::Department.find(department.id).stock_value
end
class ApplicationHelper
def present(object, klass = "#{object.class}Presenter".constantize)
presenter = klass.new(object, self)
yield presenter if block_given?
presenter
end
end
@sevos
sevos / base_presenter.rb
Created October 20, 2011 13:04
Base Presenter
class BasePresenter
def initialize(object, template)
@object = object
@template = template
end
private
def self.inherited(klass)
name = klass.name.match(/(\w+)Presenter/).captures.first.underscore
klass.instance_eval do
@sevos
sevos / cartrap.md
Created December 19, 2011 07:57
MVC isn't enough!

MVC is not enough for backend apps. It's time for car (t)rap!

  • C as Context (former controllers). Assigns roles to actors, runs the play.

  • A as Actor - user, system, external system

  • R as Role - DCI role injected to Actor by Context. User: [buyer, inviter], system: [shop, blog]

  • T as Template - html, json, xml, whatever. It's optional, sometimes Presenter does the job

  • R as Router - we need something parsing requests and passing them to proper context

  • A as Artifact - product of actor's action. Order, invoice, product, post, profile. Persistence goes here

  • P as Presenter (former helpers)

class User
def initialize(context)
@context = context
end
def profile
@context.system.collections.profiles.where(id: @context.session[:current_user_id]).first
end
end
@sevos
sevos / application_helper.rb
Created December 20, 2011 14:58
BasePresenter
module ApplicationHelper
def present(object, klass = nil)
klass ||= "#{object.class}Presenter".constantize
presenter = klass.new(object, self)
yield presenter if block_given?
presenter
end
end
module Inviter
def invite(invitee)
open_profile(me)
within ".header" do
fill_in "Invite", with: invitee.email
click_link "Invite"
end
def invite(invitee)
Factory(:invitation, inviter: me, invitee_email: invitee.email)
@sevos
sevos / README.md
Created January 16, 2012 13:23
Problems with OpenSSL in Ruby 1.9.3 on Lion

This gist show problem with Ruby 1.9.3 on my Mac OS X Lion (10.7.2).

I've tried RVM and Rbenv+ruby-build. I tried linking with system openssl, rvm-openssl and homebrew-openssl. Even if linking succeeded, the error when requiring digest/sha1 appeared.

Problem with requiring digest/sha1 causes bundler not working at all.

This gist contains:

  • test_1.9.2.txt - shows success on 1.9.2
  • test_1.9.3.txt - shows failure on 1.9.3
require 'spec_helper'
require 'timebacus/use_cases/report_activity'
describe Timebacus::ReportActivity do
context 'with valid data' do
let(:activity) { mock(id: 5, duration: 1800, description: 'remote work') }
mock_const Timebacus::ReportActivity, 'Activity' do |activity_class|
activity_class.stub(new: activity)
@sevos
sevos / value_object.rb
Last active December 28, 2015 16:39
This is just an idea of value objects API and dirty implementation for Ruby. A frankenstein created from merge of Struct's and OpenStruct's APIs. It supoprts mandatory and optional fields. It raises exceptions if API of value object is misused. Feel free to comment and refactor the implementation!
# TODO: ValueObject should be immutable. Setter should return new instance.
class ValueObject < Struct
def self.new(*fields)
all_fields, optional_fields = if fields.last.is_a?(Hash)
optional_fields = fields.pop
[fields + optional_fields.keys, optional_fields]
else
[fields, {}]
end