This file contains hidden or 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
| #Routes | |
| map.resources :posts | |
| map.resources :posts, :member => {:unpublish => :post} | |
| #Posts controller | |
| def unpublish | |
| @post = Post.find(params[:id]) | |
| @post.current_state = "unpublished" | |
| @post.save | |
| respond_to do |format| |
This file contains hidden or 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
| # Module w/ common states | |
| module CommonStates | |
| def answering_phones | |
| state :answering_phones do | |
| transitions_to :going_home, :if => :after_five?, :trigger => :shutdown_computer | |
| end | |
| end | |
| end | |
| # In your actual class |
This file contains hidden or 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
| $.ajax({url: "http://www.cnn.com/asdfasdfasdf", success: function () { console.log("hi"); }}) |
This file contains hidden or 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
| # Rails style test definitions | |
| class Test::Unit::TestCase | |
| def self.setup(&block) | |
| define_method(:setup, &block) | |
| end | |
| def self.teardown(&block) | |
| define_method(:teardown, &block) | |
| end |
This file contains hidden or 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
| # Add this to your test helper to make it easy to add a debugger statement anywhere. | |
| class Object | |
| def debug | |
| require 'ruby-debug'; debugger | |
| end | |
| end |
This file contains hidden or 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 LabeledFormBuilder < ActionView::Helpers::FormBuilder | |
| %w[text_field password_field select file_field text_area].each do |method_name| | |
| alias_method("#{method_name}_without_randys_awesome", method_name) | |
| define_method(method_name) do |field_name, *args| | |
| content = '' | |
| error = object.errors.on(field_name) | |
| row_classes = 'form-row' | |
| row_classes += ' error' unless error.blank? | |
| label_classes = 'form-label' | |
| label_classes += ' align' if %w|file_field select|.include?(method_name) |
This file contains hidden or 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
| # The following two commands (inserted into the correct files) will let you | |
| # easily tell what commits are on your current branch that are not on the target branch. | |
| # Usage: | |
| # git checkout master | |
| # git diff-branch origin/master # Shows all unpushed changes | |
| # git checkout my-new-feature | |
| # git diff-branch master # Shows all commits on my-new-feature that are not on master | |
| # In ~/.bashrc or ~/.profile | |
| function parse_git_branch { |
This file contains hidden or 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
| # usage: ruby convert-html-to-json.rb PATH_TO_FILE | |
| require 'rubygems' | |
| require 'hpricot' | |
| require 'activesupport' | |
| def node_to_s(node, indent_level=0) | |
| return nil unless node.elem? | |
| ret = "" |
This file contains hidden or 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 Hash | |
| def ref_with_destructuring(*args) | |
| ret = args.map { |a| ref_without_destructuring(a) } | |
| ret.size == 1 ? ret.first : ret | |
| end | |
| alias_method :ref_without_destructuring, :[] | |
| alias_method :[], :ref_with_destructuring | |
| end |
This file contains hidden or 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 the Cookie to its string representation. | |
| def to_s | |
| buf = "" | |
| buf += @name + '=' | |
| if @value.kind_of?(String) | |
| buf += CGI::escape(@value) | |
| else | |
| buf += @value.collect{|v| CGI::escape(v) }.join("&") | |
| end |