Skip to content

Instantly share code, notes, and snippets.

@burningTyger
burningTyger / user.rb
Created Aug 13, 2011 — forked from namelessjon/user.rb
Example user with a BCrypt password
View user.rb
require 'bcrypt'
class User
include DataMapper::Resource
attr_accessor :password, :password_confirmation, :password_reset
timestamps :at
property :id, Serial
@burningTyger
burningTyger / gist:1204454
Created Sep 8, 2011
useful sinatra template route snippet
View gist:1204454
# use this snippet for routes that are partly dynamic, partly static.
# For example your jquery.js is static. With this route Sinatra will check
# your public dir and if it finds it there, jquery.js will be served.
# if not, the views dir will be searched. If found it's served as compiuled coffee script
# if not you will get a 404. Seems useful to me...
# btw, namelessjohn remined me that checking for the file first is good because then you
# won't create expensive and possibly leaking symbols of files that don't exist.
# also, we set last_modified which is good for caching.
get '/assets/js/:file.js' do
@burningTyger
burningTyger / gist:1230371
Created Sep 20, 2011
get rid of empty sinatra params
View gist:1230371
# use this line to delete all keys that are empty:
params.delete_if { |k, v| v.empty? }
# now you can simply check if a key exists:
do_something if params[:some_key]
@burningTyger
burningTyger / fix_19_spec.md
Created Oct 15, 2011
Rubinius: Fixing a failing spec for 1.9 mode. A simple introduction.
View fix_19_spec.md

Last week I decided to fix some failing specs for Rubinius 1.9 mode. If you haven't done that before continue reading you will be surprised how easy that can be.

To get started you need a working clone of Rubinius. You can find instructions in the docs.

Now let's see what specs are failing. I consult the failing tags which can be found in:

rubinius/spec/tags/19/ruby/core

There are many specs still failing so I go through some and find something interesting in:

@burningTyger
burningTyger / gist:1301958
Created Oct 20, 2011
how to remove short vowels from Arabic strings
View gist:1301958

How to remove short vowels from Arabic strings

sometimes you might want to search Arabic text. Don't just let the user decide on the search string. You never know how your target string is spelled (short vowels are optional).

One way of doing it is to keep a devowelized string of the target and whenever a user wants to search it, strip all short vowels again.

To do so use this method:

@burningTyger
burningTyger / sencond_deploy.md
Last active Aug 30, 2017
How to deploy your openshift app to a second instance
View sencond_deploy.md

##How to deploy your openshift app to a second instance

I set up Moodle for myself on Openshift which I explained here: https://github.com/burningTyger/openshift_moodle

Now I wanted to install Moodle for a friend who has his own user and his own domain on openshift. Since I'm the admin why bother creating a second app which is basically the exact same app as mine and will most cetainly need the exact same updates. DRY!

So I decided to find out how I can use the app repo on two different instances of openshift. This is how it works (there might be better solutions, let me know):

You log in as the second user and create a new app. This will give you a git URL. Copy this URL and head over to you app repo (Moodle used in this example):

@burningTyger
burningTyger / authentication.rb
Created Dec 7, 2011 — forked from jnunemaker/authentication.rb
a starting point for authentication with mongomapper and rails
View authentication.rb
# include this in application controller
module Authentication
protected
# Inclusion hook to make #current_user and #signed_in?
# available as ActionView helper methods.
def self.included(base)
base.send :helper_method, :current_user, :signed_in?, :authorized? if base.respond_to? :helper_method
end
# Returns true or false if the user is signed in.
@burningTyger
burningTyger / README.md
Created Dec 27, 2011
Mongomapper many to many associations with variable key names
View README.md

If you're not familiar with Rails and ActiveRecord in general or with ActiveModel in particular then MongoMapper can be difficult to cope with at times. Even though the docs are fine and in detail some parts seem to be missing that are probably obvious for those coming from Rails. I'm not a Rails guy so I post my experience with many to many associations that might help one or the other save some precious debugging hours.

Take this code:

class User
  include MongoMapper::Document
  key :name, String, :required => true
  key :email, String

  key :post_ids, Array
@burningTyger
burningTyger / remove_collection.md
Created Dec 27, 2011
Mongomapper remove a collection
View remove_collection.md

You have a collection of objects in you MongoDB like this here:

class Student
  include MongoMapper::Document
  key :name, String, :required => true
  key :firstname, String, :required => true
  key :course, String
end

To clean up your database you can either do this:

@burningTyger
burningTyger / mm_ed_validation.md
Created Jan 1, 2012
MongoMapper and validation of EmbeddedDocuments
View mm_ed_validation.md

Mongomapper can use EmbeddedDocuments inside its Documents. There are some advances and disadvantages to it. This post is about validations for EmbeddedDocuments. If you have two classes like this:

class Address
	include MongoMapper::EmbeddedDocument
	key :address, String, :required => true
end

class Person
	include MongoMapper::Document
	many :addresses
You can’t perform that action at this time.