View active_record_sand_boxed.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# rollback whatever was done in this block. | |
class ActiveRecord::Base | |
def self.sand_boxed | |
transaction do | |
yield | |
raise ActiveRecord::Rollback | |
end | |
end | |
View attributes_for.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# convert results of calling methods into attributes hash. | |
# e.g. Date.today.attributes_for(:day, :month, :year) would return | |
# {:day => 13, :month => 3, :year => 2010} | |
class Object | |
def attributes_for(*method_names) | |
method_names.inject({}) do |hash, method_name| | |
hash[method_name] = send(method_name) | |
hash | |
end |
View rails_april_fools.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# application_controller.rb | |
before_filter :april_fool | |
def april_fool | |
unless logged_in? && session[:april_fooled] | |
session[:april_fooled] = true | |
flash.now[:april_fool] = 'error 10.4.1: you may have corrupted data. contact administrator immediately.' | |
# get it? 2010.4.1! | |
end |
View gist:655953
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/helpers/application_helper.rb | |
def update_flash | |
[:notice, :error].each do |type| | |
message = flash[type] | |
concat "$('#flash_#{type}').html('message')" | |
end | |
end |
View gist:655961
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// app/views/articles/create.js.erb | |
<% update_flash %> | |
// javascript code... |
View gist:655962
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- app/views/layouts/application.html.erb. --> | |
<!-- inside <head> --> | |
<script type="text/javascript"> | |
<% update_flash %> | |
</script> |
View gist:655966
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/controllers/articles_controller.rb | |
respond_with :js | |
def create | |
flash[:notice] = 'Hello World' | |
end |
View gist:655967
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// app/views/layouts/application.js.erb | |
alert(<%= flash[:notice]) %>) |
View gist:655968
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// app/views/layouts/application.js.erb | |
<% update_flash %> | |
<%= yield %> |
View ruby_love.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Ruby | |
class << self | |
define_method :'<3' do | |
puts 'ZOMG I love you!' | |
end | |
end | |
end | |
Ruby.send('<3') |
OlderNewer