Skip to content

Instantly share code, notes, and snippets.

View thedanielhanke's full-sized avatar

Daniel Hanke thedanielhanke

  • RE: GmbH
  • Cologne, Germany
View GitHub Profile
@thedanielhanke
thedanielhanke / gist:3188217
Created July 27, 2012 14:09
spaming via iMessage
MessagesApplication *imessage = [SBApplication applicationWithURL:[NSURL URLWithString:@"file:///Applications/Messages.app"]];
MessagesTextChat *target = nil;
NSString *targetid = @"iMessage;-;"; //append phone or email ..
for( MessagesTextChat *chat in [[imessage chats] get])
{
if([[chat.id description] isEqualToString:targetid])
{
target = chat;
}
}
@thedanielhanke
thedanielhanke / authorization_adapter.rb
Created August 9, 2012 09:31
RailsAdmin CanCan AuthorizationAdapter Override for per-Model abilities
module RailsAdmin
module Extensions
module CanCan
class AuthorizationAdapter
private
module ControllerExtension
def current_ability
@current_ability ||= @ability.new(_current_user, self.params["model_name"])
end
end
@thedanielhanke
thedanielhanke / parse_query.rb
Created February 2, 2016 21:26 — forked from aliang/parse_query.rb
search query string parser, google-ish
# via http://jablan.radioni.ca/post/4982485974/parsing-search-query-in-ruby
def parse_query s
s.scan(/((\S+)\:\s?)?([+-])?(("(.+?)")|(\S+))/).map{|match|
Hash[
[nil, :prefix, :plusminus, nil, nil, :phrase, :word].zip(match).select(&:all?)
]
}
end
@thedanielhanke
thedanielhanke / gist:6e0a067e1c2dc1e6cb23
Created February 23, 2016 14:28 — forked from jeffsheets/gist:5292986
Hibernate DetachedCriteria using subqueries exists clause, while eager fetching the lazy collection that is in the exists clause, and running via Spring HibernateTemplate findByCriteria, and limit results to max of 2000.
/*
* HQL would look something like this:
*
* from Person p join fetch p.addresses address
* where exists (
* from Address addr left join addr.state st
* where addr.personId = p.id
* and st.abbreviation = :abbreviation
* )
*/
# RSpec matcher to spec delegations.
#
# Usage:
#
# describe Post do
# it 'delegates name to Author' do
# expect subject.to delegate(:name).to(:author).with_prefix # post.author_name
# end
# it 'delegates month to creation date' do
# expect subject.to delegate(:month).to :created_at
@thedanielhanke
thedanielhanke / attributes.rb
Created March 26, 2016 00:57
from outside immutable class attributes
module Attributes
def has_attributes(*key_names)
key_names.each do |key_name|
define_method key_name do
instance_variable_get("@attributes")[key_name]
end
define_method "#{key_name}=" do |value|
instance_variable_get("@attributes")[key_name] = value
end
end
@thedanielhanke
thedanielhanke / nginx.conf
Created July 24, 2016 22:15 — forked from plentz/nginx.conf
Best nginx configuration for improved security(and performance). Complete blog post here http://tautt.com/best-nginx-configuration-for-security/
# to generate your dhparam.pem file, run in the terminal
openssl dhparam -out /etc/nginx/ssl/dhparam.pem 2048
@thedanielhanke
thedanielhanke / embed.rb
Created November 28, 2017 19:23 — forked from iblue/embed.rb
ActiveRecord embedding
module ActiveRecord
# Allows embedding of ActiveRecord models.
#
# Embedding other ActiveRecord models is a composition of the two
# and leads to the following behaviour:
#
# - Nested attributes are accepted on the parent without the _attributes suffix
# - Mass assignment security allows the embedded attributes
# - Embedded models are destroyed with the parent when not appearing in an update again
# - Embedded documents appears in the JSON output
@thedanielhanke
thedanielhanke / evacuation.rb
Created January 29, 2018 22:10
Example of how to do a live streaming Rails 4 model and controller using postgresql pubsub. + event module for formatting SSE.
class Evacuation < ActiveRecord::Base
def self.notify_person_found
connection.execute "NOTIFY evac, #{connection.quote 'found person'}"
end
def self.on_person_found
@thedanielhanke
thedanielhanke / evacuation.rb
Created January 29, 2018 22:10
Example of how to do a live streaming Rails 4 model and controller using postgresql pubsub. + event module for formatting SSE.
class Evacuation < ActiveRecord::Base
def self.notify_person_found
connection.execute "NOTIFY evac, #{connection.quote 'found person'}"
end
def self.on_person_found