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 / 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
# 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 / 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
* )
*/
@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 / 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 / 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;
}
}