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
https://groups.google.com/forum/#!forum/rubyonrails-security |
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
defmodule MacroExp do | |
defmacro attr_accessor(atom) do | |
getter = String.to_atom("get_#{atom}") | |
setter = String.to_atom("set_#{atom}") | |
quote do | |
def unquote(getter)(data) do | |
data |> Map.from_struct |> Map.get(unquote(atom)) | |
end | |
def unquote(setter)(data, value) do | |
data |> Map.put(unquote(atom), value) |
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
items = %w{foo bar baz goo gar gaz hoo har haz} | |
def recurse_array(ary, &block) | |
head, *tail = ary | |
head and block.call(head) | |
tail.any? and recurse_array(tail, &block) | |
end | |
puts "First the items in order:\n" | |
recurse_array items do |item| |
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
# Call scopes directly from your URL params: | |
# | |
# @products = Product.filter(params.slice(:status, :location, :starts_with)) | |
module Filterable | |
extend ActiveSupport::Concern | |
module ClassMethods | |
# Call the class methods with the same name as the keys in <tt>filtering_params</tt> | |
# with their associated values. Most useful for calling named scopes from |
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
# http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html | |
# need to specify id for an update: | |
class Member < ActiveRecord::Base | |
has_one :avatar | |
accepts_nested_attributes_for :avatar | |
end | |
params = { member: { name: 'Jack', avatar_attributes: { icon: 'smiling' } } } |
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
// http://stackoverflow.com/questions/3291712/is-it-possible-to-open-a-popup-with-javascript-and-then-detect-when-the-user-clo | |
// The window.opener property refers to the page that spawned this popup. | |
// For example, if I wrote a function named callingPageFunction on my original page, I would call it from the popup like this: | |
$(window).unload(function() { | |
window.opener.callingPageFunction() | |
}); | |
var wnd = window.open("file.html", "youruniqueid", "width=400, height=300"); |
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
/** | |
* Simple event dispatcher | |
* | |
* Example | |
* | |
* var MyConstructor = function () { | |
* var self = this | |
* var count = 0 | |
* setInterval(function () { | |
* self.emit('tick', {count: count}) |
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 ActiveSupport::IntegrationCase < ActiveSupport::TestCase | |
include Capybara | |
include Rails.application.routes.url_helpers | |
self.use_transactional_fixtures = false | |
# Checks for missing translations after each test | |
teardown do | |
unless source.blank? |
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
# Natively, Enumerators get JSONized like "#<Enumerator::Lazy:0x007f8714807080>", or they explode, either of which is a problem. | |
# We want them to make an array, and do it lazily so we don't have to keep the items in memory! | |
class Enumerator | |
def to_json(state) | |
state.depth += 1 | |
string = "[\n" | |
first_item = true | |
self.each do |item| |
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
require "tempfile" | |
# Captures the given stream and returns it: | |
# | |
# stream = capture(:stdout) { puts 'notice' } | |
# stream # => "notice\n" | |
# | |
# stream = capture(:stderr) { warn 'error' } | |
# stream # => "error\n" |