Skip to content

Instantly share code, notes, and snippets.

View sevos's full-sized avatar

Artur Roszczyk sevos

View GitHub Profile
@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
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 / 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)

@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
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 / 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
@sevos
sevos / department.rb
Created October 6, 2011 11:23
Semi-DCI Example
class Department << ActiveRecord::Base
end
!!!
%html
%head
%title
Zrzuta na...
= yield :title
= stylesheet_link_tag "application"
= javascript_include_tag "application"
= csrf_meta_tags
class DailyStatsReport < KPI::Report
def users
result 'Users', User.count, :description => 'Total users count'
end
def premium_users
result 'Premium users', User.premium.count
end
def premium_users_percentage
@import clearfix.sass
$grid_width: 1200px
$grid_glue: 10px
$grid_boxes: 24
=grid-container
+clearfix
padding:
left: 0px