Skip to content

Instantly share code, notes, and snippets.

View maciejsmolinski's full-sized avatar

Maciej Smolinski maciejsmolinski

View GitHub Profile
require 'spec_helper'
describe Battle do
context "new" do
let!(:battle) { Battle.new }
let!(:events) { [:can_open?, :can_close_no_opponent?, :can_start?, :can_close_no_video?, :can_close_no_videos?, :can_close_no_votes?, :can_close_ex_equo?, :can_close_more_votes?, :can_close?] }
it "should have default :pending status" do
class Battle < ActiveRecord::Base
include Workflow
default_value_for :state, "pending"
after_save :update_users_states
workflow_column :state
workflow do
state :pending do # valid for 2 weeks
class BattleObserver < ActiveRecord::Observer
def after_save(battle)
BattleComputation.new(battle).update_users_states
end
end
module Battlejam
class Application < Rails::Application
...
config.active_record.observers = :battle_observer
end
end
module ClassHelpers
def new_user
User.new
end
def new_valid_user
User.new(valid_user_params)
end
describe User do
include ClassHelpers
context "new" do
it "should create user with role_id = 2 (registered) by default" do
new_valid_user.role_id.should == 2
new_valid_user.registered?.should be_true
end
@maciejsmolinski
maciejsmolinski / max2.sjs
Last active August 29, 2015 13:56
Math.max taking arrays implemented in sweet.js
/* sweet.js (sjs) markup, can be compiled on http://sweetjs.org/browser/editor.html */
let Math.max = macro {
rule { ([$x ...]) } => { Math.max ($x ...) }
rule { ($x ...) } => { Math.max ($x ...) }
}
// 1. Regular
Math.max(1,2) // => Math.max(1,2)
Math.max(3,4,5) // => Math.max(3,4,5)
@maciejsmolinski
maciejsmolinski / repository.es6
Created February 19, 2015 22:47
ES6 Repo + DI
class CoreRepository {
static inject () {
return [];
}
static instance () {
var deps = this.inject();
return new this(...deps);
}
}
@maciejsmolinski
maciejsmolinski / wallet.js
Created February 20, 2015 09:06
Wallet.js (PoC)
app.service('Wallet', function () {
function Wallet () {
this.total = 0;
}
Wallet.prototype.add = function (amount) {
this.total += Number(amount) || 0;
return this.total;
};
@maciejsmolinski
maciejsmolinski / wallet.es6
Created February 20, 2015 13:29
ES6 AOP Wallet
class AOP {
static extend(target) {
Object.assign(target, AOP.prototype);
}
}
AOP.prototype.before = function (property, fn) {
let oldProperty = this[property];
this[property] = function () {
fn.apply(this, arguments);