Skip to content

Instantly share code, notes, and snippets.

View jnicklas's full-sized avatar

Jonas Nicklas jnicklas

View GitHub Profile
# Generators
# If we load in the generator this way it will *always* be loaded
scope "merb-gen" do
Merb.add_generators File.join(File.dirname(__FILE), 'lib', 'generators', 'model')
end
migration_test:(master) $ merb-gen model --migration Feed id:Serial url:string description:text
/usr/lib/ruby/gems/1.8/gems/merb_datamapper-0.9.4/lib/generators/model.rb:24: undefined local variable or method `migration' for Merb::Generators::DataMapperModelGenerator:Class (NameError)
from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
from /usr/lib/ruby/gems/1.8/gems/merb-gen-0.9.4/lib/merb-gen.rb:34
from /usr/lib/ruby/gems/1.8/gems/merb-gen-0.9.4/lib/merb-gen.rb:33:in `each'
from /usr/lib/ruby/gems/1.8/gems/merb-gen-0.9.4/lib/merb-gen.rb:33
from /usr/lib/ruby/gems/1.8/gems/merb-core-0.9.4/lib/merb-core/bootloader.rb:550:in `call'
from /usr/lib/ruby/gems/1.8/gems/merb-core-0.9.4/lib/merb-core/bootloader.rb:550:in `run'
from /usr/lib/ruby/gems/1.8/gems/merb-core-0.9.4/lib/merb-core/bootloader.rb:550:in `each'
class Posts < Application
get '/'
@post = Post.all
end
get '/:id' do |id|
@post = Post.first(id)
end
Named Routes
merb_gen_y_index: /generate/
Anonymous Routes
/generate/:controller(/:action(/:id)?)?(\.:format)?
# An uploader for merb, stored in app/uploaders/my_uploader.rb
#
# Usage:
#
# identifier = "some_string_probably_filename.png"
# uploader = MyUploader.new(identifier)
#
# Storing files to the filesystem
# uploader.storage = :file
#
class SomeClass
class << self
def hooks
@hooks ||= Hash.new([])
end
def add_hook(kind, &block)
self.hooks[kind] << block
end
## Model
class Author
class << self
def search(conditions={})
conditions = conditions.reject { |k,v| v.blank? }
conditions[:books] = { :title => conditions.delete(:book_name) }
export PROJDIR="$HOME/Projects"
p() {
cd $PROJDIR/$1/
}
_projectcomplete() {
COMPREPLY=($(compgen -W '$(`which ls` $PROJDIR/)' -- ${COMP_WORDS[COMP_CWORD]}))
return 0
}
# Consistent API so we can change the underpinnings without breakage
# Everything done inside the block will adopt :activerecord as trait
Rails.generator(:model).add(:activerecord) do
# this will be handled as a binary file
action 'file.png'
# as a template
action 'some_ruby_file.rb'
class Controller
def index
<%= partial 'index' %>
end
end