Skip to content

Instantly share code, notes, and snippets.

View maxim's full-sized avatar
🛠️

Max Chernyak maxim

🛠️
View GitHub Profile
# Weird ruby behavior
>> class Foo; end
=> nil
>> defined?(Foo)
=> "constant"
>> defined?("Foo".constantize)
=> "method"
#
# When you have duplication like this
def base_price(options = {})
PriceFormatter::Formatter.format(super, options)
end
def price(options = {})
PriceFormatter::Formatter.format(base_price * quantity, options)
end
def sample_file(name = "most_used_file.png")
File.new("test/fixtures/files/#{name}")
end
@maxim
maxim / git.rake
Created September 12, 2009 07:25
Rake tasks for managing git plugins with submodules.
# Rake tasks for managing git plugins with submodules.
#
# These tasks aim to make life simpler by automating all the boring work.
# What you get:
# - complete git integration (all you need to know is install, uninstall and update)
# - complete github integration (only use author name + plugin name)
# - rails plugin hooks (install.rb/uninstall.rb) are taken care of
#
# Available commands:
#
# Install
#
# Move this file into config/preinitializer.rb
#
# Usage
#
# This line:
# config.github_gem "mislav-will_paginate"
# is equivalent to this line:
# config.gem "mislav-will_paginate", :lib => "will_paginate", :source => "http://gem.github.com"
def has_role?(role, object)
if object.has_association?(role)
if has_many?
object.roles.find(self.id)
else
object.role.id == self.id
end
else
self.roles.include?(role)
end
context "with associations" do
setup do
fake_association = Struct.new(:macro, :name, :object)
fake_object_with_associations = Struct.new(:associations) do
def reflect_on_all_associations
associations
end
def method_missing(meth, *args, &blk)
association = associations.find{|a| a.name == meth}
context "with associations" do
setup do
fake_association = Struct.new(:macro, :name, :object)
klass_with_associations = proc{|associations|
Class.new do
self.class.send(:define_method, :reflect_on_all_associations) do
associations
end
define_method(:method_missing) do |meth|
def title(page_title, show_title = true)
content_for(:title) { t('.title', :default => page_title) }
@show_title = show_title
end
def show_title?
@show_title
end
@maxim
maxim / gist:223992
Created November 2, 2009 06:12
gem update fail
projects/lab/freight[devise]% sudo gem update rr
Password:
Updating installed gems
Updating acts_as_ferret
Successfully installed acts_as_ferret-0.4.4
Gems updated: acts_as_ferret
Installing RDoc documentation for acts_as_ferret-0.4.4...