Skip to content

Instantly share code, notes, and snippets.

@clyfe
clyfe / modules.coffee
Created July 16, 2011 14:32
include Module pattern: copy VS prototype chain injection
_ = require 'underscore'
includeCopy = (to, module) ->
proto = to::
for name, property of module
proto[name] = property
includeInter = (to, module) ->
m = _.clone(module)
originalPrototype = to.prototype
@clyfe
clyfe / _list_record.html.erb
Created July 14, 2011 17:10
ActionLinks#member should also include nested columns links
<%# custom view for reporting %>
<%
record = list_record_report if list_record_report # compat with render :partial :collection
columns ||= list_columns
tr_class = cycle("", "even-record")
tr_class += " #{list_row_class(record)}" if respond_to? :list_row_class
url_options = params_for(:action => :list, :id => record.id)
# TODO: refactor, pull to Volker
@clyfe
clyfe / behavior_state.coffee
Created July 13, 2011 09:02
Behavior with state attached VS State with behavior attached
# Behavior with state attached
func = do ->
variable = 0 # state
-> console.log variable++ # behavior
func() # 0
func() # 1
# State with behavior attached
@clyfe
clyfe / active_record.coffee
Created June 16, 2011 20:23
CoffeeScript on a Pony
class Base
@include: (module) ->
@::[k] = v for k, v of module::
@has_many: (rel, opts) ->
@::[rel] = -> console.log rel
@tableName: ->
@_tableName ?= @toString().match(/function ([^\(]+)/)[1]
@find: (id) ->
console.log "SELECT * FROM #{@tableName()} WHERE id = #{id}"
save: (opts = null) ->
@clyfe
clyfe / cancan.rb
Created June 7, 2011 14:16
Monkey patch for cancan issue #327
# monkey-patch https://github.com/ryanb/cancan/issues/327
# put in Rails.root/config/initializers/cancan.rb
module CanCan
module ModelAdapters
class ActiveRecordAdapter
private
# fix nested imbrication
def merge_conditions(sql, conditions_hash, behavior)
if conditions_hash.blank?
@clyfe
clyfe / cancan_with_col.rb
Created March 8, 2011 11:07
CanCan with column
# is usually called with :crud_type and :column, or :action
# {:crud_type=>:update, :column=>"some_colum_name"}
# {:action=>"edit"}
# to allow access cancan must allow both :crud_type and :action
# if cancan says "no", it delegates to default AS behavior
def authorized_for_with_cancan?(options = {})
raise InvalidArgument if options[:crud_type].blank? and options[:action].blank?
if current_ability.present?
crud_type_result = options[:crud_type].nil? ? true : current_ability.can?(options[:crud_type], self)
action_result = options[:action].nil? ? true : current_ability.can?(options[:action].to_sym, self)
@clyfe
clyfe / Keys.c
Created March 2, 2011 13:33
rsa crypto
/* Copyright(C) by William Estrada Jul 16, 2008, All rights reserved *
* Mr_Umunhum@Mt-Umunhum-Wireless.net */
#define _GNU_SOURCE
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <openssl/rsa.h>
@clyfe
clyfe / _horizontal_subform.html.erb
Created March 1, 2011 23:06
How to wire record for AS cancan bridge
<table cellpadding="0" cellspacing="0">
<% @record = column.association.klass.new -%>
<%
case parent_record
when DocumentItem then @record.project_id = parent_record.management_process.project_id
when Project then @record.project_id = parent_record.project_id
end
%>
<%= render :partial => 'horizontal_subform_header', :locals => {:parent_record => parent_record} %>
@clyfe
clyfe / _horizontal_subform.html.erb
Created March 1, 2011 22:44
Never finished work on AS cancan bridge record autowiring
<table cellpadding="0" cellspacing="0">
<%
begin
remote_controller = active_scaffold_controller_for(column.association.klass).new
remote_request_env = {}
request.env.select {|key, value| key == key.upcase || key == 'rack.input'}.each {|item| remote_request_env[item[0]] = item[1]}
remote_request_env["warden"] = request.env["warden"] if (request.env.has_key?("warden"))
remote_request = ActionDispatch::Request.new(remote_request_env)
# config/initializers/as_i92.rb
# ========================================================================
# resolution for https://github.com/vhochstein/active_scaffold/issues/92 #
# currently monkeypatching the bug, to be deleted on official fix #
# ========================================================================
module ActiveScaffold::Actions
module Update
protected