Created
May 24, 2011 18:57
-
-
Save jrust/989391 to your computer and use it in GitHub Desktop.
Ruby & Rails & LP idioms
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
# @method LpConfig | |
# @desc Set variables for use by javascript files | |
# @file taxonomies/views/index.html.erb | |
# Non-idiom | |
=begin erb code | |
<% javascript_tag do %> | |
$.post('<%= new_taxonomy_path %>'); | |
<% end %> | |
=end | |
# Idiomatic! | |
# @file index.html.haml | |
=begin haml code | |
- @js_config[:new_taxonomy_path] = new_taxonomy_path | |
= end | |
# @file taxonomies_admin.js | |
=begin js code | |
$.post(LpConfig.new_taxonomy_path); | |
= end | |
# -- | |
# @method LpJavascriptModule | |
# @desc Encapsulate javascript functions inside of a javascript object that is namespaced. | |
# @file taxonomies_admin.js | |
# Non-idiom | |
=begin js code | |
global_var = 'hello'; | |
function foo() { | |
bar(); | |
} | |
function bar() { | |
console.log(global_var); | |
} | |
=end | |
# Idiomatic! | |
=begin js code | |
var LpFoo = { | |
class_var = 'hello', | |
foo: function() { | |
LpFoo.bar(); | |
}, | |
bar: function() { | |
console.log(LpFoo.class_var); | |
} | |
} | |
=end | |
#-- | |
# @method returning | |
# @desc Returns value after yielding value to the block. This simplifies the process of constructing an object, performing work on the object, and then returning the object from a method. | |
# @file query.rb | |
# Non-idiom | |
def taxonomy_report(term, page) | |
queries = self.narrow(term, :page => page, :filter_long_terms => false, :select_slug => false, :include_quoted_term => false) | |
queries.each do |queries| | |
queries.num_worksheets = some_num | |
end | |
queries | |
# Idiomatic! | |
def taxonomy_report(term, page) | |
returning self.narrow(term, :page => page, :filter_long_terms => false, :select_slug => false, :include_quoted_term => false) do |queries| | |
queries.num_worksheets = 5 | |
end | |
end | |
#-- | |
# @method included, alias_method_chain | |
# @desc A way to manipulate classes without needing to modify the original files, including private methods | |
# @file payflow_common_api.rb | |
module ActiveMerchant #:nodoc: | |
module Billing #:nodoc: | |
module PayflowCommonAPI | |
def parse(data) | |
xml = REXML::Document.new(data) | |
... | |
end | |
end | |
end | |
end | |
# @file lib/payflow_charset_conversion.rb | |
# PayPal doesn't specify a character set for their XML and sometimes it comes | |
# back with characters that are not latin1, so we convert it to utf8 | |
module ActiveMerchant | |
module Billing | |
module PayflowCharsetConversion | |
private | |
def self.included(receiver) | |
# don't alias twice or we overwrite parse_without_latin1_conversion, leading to 'stack level too deep' | |
return if receiver.private_instance_methods.include? 'parse_without_latin1_conversion' | |
receiver.alias_method_chain :parse, :latin1_conversion | |
end | |
def parse_with_latin1_conversion(data) | |
data = Iconv.conv('UTF-8', 'LATIN1', data) | |
parse_without_latin1_conversion(data) | |
end | |
end | |
end | |
end | |
# @file initializers/payflow_charset.rb | |
# Include our override into the payflow class | |
ActiveMerchant::Billing::PayflowGateway.send(:include, ActiveMerchant::Billing::PayflowCharsetConversion) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment