Skip to content

Instantly share code, notes, and snippets.

@orbanbotond
orbanbotond / meta_programming.rb
Created June 18, 2013 08:58
meta programming
# List only the methods defined in that specific class
self.methods - self.class.ancestors.map{|x|x.methods}.flatten
@orbanbotond
orbanbotond / state_machine_states
Created July 23, 2013 13:51
scopes for state_machine states
def Price.generate_scopes_for_state_machines
state_machines.each_pair do |machine_name, that_machine|
that_machine.states.map do |state|
# puts "will create these scopes: #{machine_name}_#{state.name} state: #{state.value} "
# puts "will create these scopes: #{machine_name}_#{state.name} state: #{state.name.to_s} "
# Price.scope "#{machine_name}_#{state.name}", :conditions => { machine_name => state.name.to_s }
Price.scope "#{machine_name}_#{state.name}", :conditions => { machine_name => state.value }
end
end
end
require File.expand_path('../boot', __FILE__)
# Pick the frameworks you want:
# require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
# require "active_resource/railtie"
require "sprockets/railtie"
require "mongoid"
# require "rails/test_unit/railtie"
@orbanbotond
orbanbotond / exercise: proc & lambda
Created September 17, 2013 18:29
exercise: proc & lambda
a = -> { puts "lambda" }
a.class
def negyedik
puts "negyedik"
yield
end
def harmadik(pr)
puts "harmadik"
# http://sinonjs.org/docs/#spies
# https://github.com/pivotal/jasmine/wiki/Spies
# http://pivotal.github.io/jasmine/
#
class PubSub
subscribe: (cb) ->
@cb = cb
publish: (arg1, arg2)->
@cb(arg1, arg2)
@orbanbotond
orbanbotond / mocks.js.coffee
Created September 18, 2013 13:47
sinon mock
# http://sinonjs.org/docs/#mocks
# http://pivotal.github.io/jasmine/
#
class PubSub
subscribers: ->
@listeners ||= []
@listeners
subscribe: (cb) ->
@subscribers().push cb
publish: (arg1, arg2)->
@orbanbotond
orbanbotond / serializer.rb
Created September 20, 2013 08:53
Serializer Usage
u = User.find '5209df7c2e21a971fd000027'
params = u.active_model_serializer.new(u, {}).as_json
roles = params[:roles]
roles.push( {"organization_id" => "5209df7c523f8fd8816767b3", "role" => :manager})
u.update_attributes params
class UserSerializer < ActiveModel::Serializer
attributes :_id, :name, :email, :timezone, :mobile, :password, :password_confirmation, :country_calling_prefix
@orbanbotond
orbanbotond / mongoid where chaining
Created October 31, 2013 09:40
mongoid where chaining
# ---
# Analytics
term = 'dolor'
term = 'harmadik'
report = Analysis.where( {:'$and' => [{
:'$or' => [
{'widgets.text' => /#{term}/i},
{'title' => /#{term}/i}
]}
]}
@orbanbotond
orbanbotond / mongoid_observers.rb
Created November 6, 2013 15:03
mongoid observers
module MyObservers
module Document
module ClassMethods
def observers= (*observers)
declared_observers.replace(observers.flatten)
end
def declared_observers
@declared_observers ||= []
end
@orbanbotond
orbanbotond / rails_routes_tricks.rb
Created November 6, 2013 16:39
rails routes tricks
get '(/o/:organization/)search/:q' => "search#show", as: :search
app.search_path q:'boti'
=> "search/boti"
2.0.0p0 :003 > app.search_path q:'boti', organization: 'Masik'
=> "/o/Masik/search/boti"