Skip to content

Instantly share code, notes, and snippets.

View dpickett's full-sized avatar

Dan Pickett dpickett

View GitHub Profile
@dpickett
dpickett / attribute_filter.rb
Created March 4, 2012 18:56
attribute filter concept for mass assignment
class UserAttributeFilter < AttributeFilter::Filter
strategy :whitelist,
attributes: [:first_name, :last_name, :login, :password, :password_confirmation]
end
u = User.new({protected_attribute: "blah", first_name: "John"}, filter: UserAttributeFilter)
# u.attributes => {first_name: "John"}
@dpickett
dpickett / going_off_the_rails.md
Created February 18, 2012 20:23
going_off_the_rails.md

Going Off of the Rails With Your Business Objects

With great power comes great responsibility. ActiveRecord gives us a wonderful mechanism for writing to a database, but Ruby itself gives us a universe of options for writing business logic on a layer above persistence.

What if you could write smaller business objects that are easier to test and are more maintainable? Let's discuss how we can do better by going back to basics with object oriented design, and by separating our business objects from our persistence.

Topics Covered Will Include:

  • The Single Responsibility Principle and its importance to object oriented design
  • ActiveModel and its suite of mixins that help us to separate concerns
SERIALIZE_OPTIONS = {
methods: :creation_date,
include: {user: {avatar: {}}}
}
def serializable_hash(options = {})
super(options.merge(SERIALIZE_OPTIONS))
end
(function($) {
$.fn.hint = function(options) {
this.each(function() {
new FieldHint(this, options);
});
};
})(jQuery);
function FieldHint(field, options) {
this.$input = $(field),
@dpickett
dpickett / link_like_button.scss
Created December 31, 2011 19:55
link_like_button.scss
namespace :deploy do
task :run do
`export RELEASE='true'`
`bundle`
`git add Gemfile Gemfile.lock`
`git commit -m 'kong setup for deploy'`
`git push heroku master`
`export RELEASE=''`
`bundle`
end
"/Users/dpickett/work/kong/config/locales/devise.en.yml",
"/Users/dpickett/work/kong/config/locales/en.yml",
"/Users/dpickett/work/kong/config/locales/flashes.en.yml",
"/Users/dpickett/work/kong/config/locales/simple_form.en.yml",
"/Users/dpickett/.rvm/gems/ruby-1.9.2-p180-patched@crowdly/gems/kaminari-0.12.4/config/locales/kaminari.yml",
"/Users/dpickett/.rvm/gems/ruby-1.9.2-p180-patched@crowdly/gems/devise-1.4.9/config/locales/en.yml",
"/Users/dpickett/work/kong/spec/dummy/config/locales/en.yml"
def start_date=(date_string = "")
if date_string.blank?
@start_date = nil
else
if date_string.is_a?(String) && !date_string.blank?
begin
@start_date = Time.strptime(date_string, "%m/%d/%Y")
rescue ArgumentError => e
#the date failed to parse
@start_date = nil
@dpickett
dpickett / devise_mailer.rb
Created October 8, 2011 19:25
devise hack with resque mailer
##HACK - dmp - we must return an unserialized version of resource
##so copy devise's method and use a finder for resource
require "devise"
require Rails.root.join("lib/resque/mailer")
Devise::Mailers::Helpers.send(:alias_method, :old_initialize_from_record, :initialize_from_record)
module Devise
module Mailers
module Helpers
@dpickett
dpickett / bad_controller.rb
Created September 27, 2011 15:18
exceptions for flow control is bad
class BadController < ApplicationController
def create
begin
some_object.save!
rescue Error => e
render :json => { :error => e.message }, :status => :unprocessable_entity
end
end
end