Skip to content

Instantly share code, notes, and snippets.

View amiel's full-sized avatar

Amiel Martin amiel

View GitHub Profile
require 'virtus'
class Foo
include Virtus
attribute :id, Integer
attribute :date, Date
attribute :foo, String
def initialize(attributes = {})
class Foo
extend AutoCoercions
coerce :date_gte, :date_lte, with: :to_date
coerce :trip_mode_eq, :trip_purpose_eq, :trip_count_gte, with: :to_i
coerce :select_random_winners, with: ->(v) { v == '1' }
coerce :random_winner_count, with: :to_i
def initialize(attributes = {})
@attributes = self.class.apply_coercions(attributes)
@amiel
amiel / foo.rb
Created November 8, 2012 21:55
ActiveAdmin icons
ActiveAdmin.register Foo do
collection_action :foo
end
@amiel
amiel / test.rb
Created November 7, 2012 18:26
Crazy not-parse-error failed like so
deliveries = ActionMailer::Base.deliveries. # <= note extra `.`
assert_equal I18n.t('user_mailer.bid_confirmation_email.subject'), deliveries.first.subject
# => NoMethodError: undefined method `first' for nil:NilClass
@amiel
amiel / en.yml
Created October 23, 2012 22:55
Using Stamp (https://github.com/jeremyw/stamp) with I18n.l
en:
date:
formats:
default: <%= Stamp.strftime_format("March 1, 1999") %>
short: <%= Stamp.strftime_format("Mon Jan 1") %>
ordinal: <%= Stamp.strftime_format("1st of Jan") %>
time:
formats:
default: etc...
controller do
def create
# Extend create to set the current user in your model here
create!
end
end
@amiel
amiel / engine.rb
Created October 10, 2012 21:37
Testing routes with Rails 3.2 mountable engines and RSpec 2
# your-engine/lib/magic/rails/engine.rb
module Magic
module Rails
module Engine
##
# Automatically append all of the current engine's routes to the main
# application's route set. This needs to be done for ALL functional tests that
# use engine routes, since the mounted routes don't work during tests.
#
@amiel
amiel / models.rb
Created October 9, 2012 22:45
mark as deleted idea
class Image
def mark_as_deleted(build = nil)
# If there's only one undeleted build left, we should only delete the one passed in
# otherwise, we're free to delete them all, and we should delete self as well.
if build && self.has_more_than_one_undeleted_build?
build.mark_as_deleted
else
build.each(&:mark_as_deleted)
self.update_column :deleted_at, Time.current
end
class Foo < ActiveRecord::Base
extend TimeScopes
# ...
end
{
"auto_complete_commit_on_tab": false,
"color_scheme": "Packages/Tomorrow-Theme/Tomorrow-Night-Eighties.tmTheme",
"ensure_newline_at_eof_on_save": true,
"font_face": "Inconsolata-dz",
"font_size": 10.0,
"highlight_line": true,
"rulers":
[
80,