Skip to content

Instantly share code, notes, and snippets.

Avatar

Tyler Rick TylerRick

  • USA
View GitHub Profile
@TylerRick
TylerRick / form_helper_extensions.rb
Last active Dec 18, 2015
date_field helper for HTML5 type="date" inputs! And a version of value_before_type_cast that returns a string in ISO-8601 format as the value for Dates, as specified by http://www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#date-state-(type=date)
View form_helper_extensions.rb
# Extensions to gems/actionpack-3.2.13/lib/action_view/helpers/form_helper.rb
=begin
A version of InstanceTag.value_before_type_cast that returns a string in ISO-8601 format for Date columns.
The original behavior of value_before_type_cast was to return the Date object itself. But the default to_s on a Date returns a string like "June 5, 2013".
However, HTML5 date input fields expect their values to be in ISO-8601 format ("2013-06-05") and won't recognize the value if it is in some other format.
This makes it so you can simply do this:
@TylerRick
TylerRick / delegate_to_all.rb
Last active Mar 29, 2020
DelegateToAll. Like delegate.rb from Ruby's std lib but lets you have multiple target/delegate objects.
View delegate_to_all.rb
# DelegateToAll. Like delegate.rb from Ruby's std lib but lets you have multiple target/delegate objects.
require 'delegate'
class DelegatorToAll < Delegator
# Pass in the _obj_ to delegate method calls to. All methods supported by
# _obj_ will be delegated to.
#
def initialize(*targets)
__setobj__(targets)
@TylerRick
TylerRick / open_txmt_link_in_vim.desktop
Last active Dec 13, 2015
For opening links from better_error and RailsPanel in vim on Ubuntu
View open_txmt_link_in_vim.desktop
@TylerRick
TylerRick / example.rb
Last active May 2, 2016
Allows you to define non-persisted attributes in an ActiveRecord model, the same way you can do in a regular ActiveModel model. This lets you use conveniences like my_method_changed? from ActiveModel::Dirty, for example, on your non-persisted attributes. Tested with Rails 3.2.11
View example.rb
# Example usage:
require 'active_record/nonpersisted_attribute_methods'
class Node < ActiveRecord::Base
include ActiveRecord::NonPersistedAttributeMethods
define_nonpersisted_attribute_methods [:bar]
# Example of using with ancestry and getting :parent to show up in changes hash.
has_ancestry
define_nonpersisted_attribute_methods [:parent]
View enumerable_extensions.rb
module Enumerable
# Returns an array of arrays, each inner array containing a cluster of contiguous elements from
# self.
#
# [-1, 0, 1, 3, 4, 7, 8].clusters_of_contiguous
# => [[-1, 0, 1], [3, 4], [7, 8]]
#
# Based off of Enumerable#cluster (from facets gem)
#
def clusters_of_contiguous
@TylerRick
TylerRick / nested_attributes_setter_can_be_overridden.rb
Created Aug 17, 2012
Monkey patch for Rails 3.2 that allows Nested attribute setters to be overridden
View nested_attributes_setter_can_be_overridden.rb
module ActiveRecord
module NestedAttributes #:nodoc:
ClassMethods.class_eval do
def accepts_nested_attributes_for(*attr_names)
options = { :allow_destroy => false, :update_only => false }
options.update(attr_names.extract_options!)
options.assert_valid_keys(:allow_destroy, :reject_if, :limit, :update_only)
options[:reject_if] = REJECT_ALL_BLANK_PROC if options[:reject_if] == :all_blank
attr_names.each do |association_name|
View backtrace 1 - for its and to_s bug.txt
> bundle exec rspec --backtrace spec/rspec/core/subject_spec.rb
Run options:
include {:focus=>true}
exclude {:ruby=>#<Proc:./spec/spec_helper.rb:84>}
All examples were filtered out; ignoring {:focus=>true}
...FF.........................
Failures:
View showing_how_long_it_took_and_how_many_retries_with_workaround_in_place.txt
> rake cucumber
WARNING: Possible conflict with Rake extension: String#ext already exists
WARNING: Possible conflict with Rake extension: String#pathmap already exists
/home/tyler/.rvm/rubies/ruby-1.9.3-p125/bin/ruby -S bundle exec cucumber --profile default
Using the default profile...
Feature: Cucumber step definitions
@javascript
Scenario: When I do a capybara search that triggers the nested synchronize bug # features/reproduce_bug.feature:3
====================================================================================================
View showing_which_method_calls_are_creating_nested_synchronize.txt
These are the 3 places that together caused the nested calls to synchronize:
/home/tyler/.rvm/gems/ruby-1.9.3-p125/bundler/gems/capybara-555008c74751/lib/capybara/node/finders.rb:29:in `find'
def find(*args)
query = query(*args)
query.find = true
synchronize do
results = resolve(query)
query.verify!(results)
results.first
View showing_how_long_it_took_and_how_many_retries.txt
> rake cucumber
WARNING: Possible conflict with Rake extension: String#ext already exists
WARNING: Possible conflict with Rake extension: String#pathmap already exists
/home/tyler/.rvm/rubies/ruby-1.9.3-p125/bin/ruby -S bundle exec cucumber --profile default
Using the default profile...
Feature: Cucumber step definitions
@javascript
Scenario: When I do a capybara search that triggers the nested synchronize bug # features/reproduce_bug.feature:3
0 retries=40 e=#<Capybara::ElementNotFound: Unable to find css ".some_div_that_doesnt_exist">