Skip to content

Instantly share code, notes, and snippets.

View francirp's full-sized avatar

Ryan Francis francirp

  • LaunchPad Lab
  • Chicago, IL
View GitHub Profile
@francirp
francirp / gist:b6676ab405f7f7dced0c
Last active December 5, 2023 02:04
Sublime Text Shortcuts for Mac

Critical Sublime Text Shortcuts (Mac)

Editing:

  • Copy: Command + C
  • Cut: Command + X
  • Paste: Command + V
  • Cursor to end of line: Command + Right Arrow
  • Cursor to beginning of line: Command + Left Arrow
@francirp
francirp / gist:3ecf93fe57c45f3fd524
Created April 24, 2015 19:42
General Mac Shortcuts

Critical General Mac Shortcuts

  • Copy: Command + C
  • Cut: Command + X
  • Switch to recently used application: Command + Tab
  • See list of recently used applications: Command + Tab (Release tab and continue holding command. Select an app with left / right arrow)
  • Next window in current application: Command + ~
  • Open Spotlight: Command + Space
@francirp
francirp / nested_attributes_uniqueness_validator.rb
Last active May 21, 2022 13:27
Validate Uniqueness of Nested Attributes in Rails - Custom Validator
class NestedAttributesUniquenessValidator < ActiveModel::EachValidator
def validate_each(record, attribute, items)
items = items.reject(&:_destroy) # let's ignore the items to be destroyed here
unless items.map(&options[:field]).to_a.uniq.size == items.size
record.errors[attribute] << "must be unique"
field = options[:field]
values = items.map {|item| item.send(field) }
duplicates = items.find_all {|item| values.count(item.send(field)) > 1 && item.id.nil? }
duplicates.each { |obj| obj.errors[field] << "has already been taken" }
end
@francirp
francirp / decorators_codeblock_17.md
Created August 8, 2016 16:25
Decorators Codeblock 17
def self.percent(*keys)
  keys.each do |key|    
    define_method(key) do
      h.number_with_precision(object.send(key) * 100.0, precision: 1)
    end

    define_method("display_#{key}") do
      h.number_to_percentage(object.send(column_name) * 100.0, precision: 1)
 end
@francirp
francirp / decorators_codeblock_16.md
Created August 8, 2016 16:24
Decorators Codeblock 16
class CompanyDecorator < BaseDecorator
  delegate_all

  currency :budget, :actual  
end
@francirp
francirp / decorators_codeblock_15.md
Last active August 8, 2016 16:23
Decorators Codeblock 15
class BaseDecorator < Draper::Decorator
  delegate_all

  def self.currency(*keys)
    keys.each do |key|
      define_method(key) do
        h.number_with_precision(object.send(key), precision: 2)
      end
@francirp
francirp / decorators_codeblock_14.md
Created August 8, 2016 16:22
Decorators Codeblock 14
class BaseDecorator < Draper::Decorator
  delegate_all

  def self.currency(*keys)
    keys.each do |key|
      define_method(key) do
      end

 define_method("display_#{key}") do
@francirp
francirp / decorators_codeblock_13.md
Created August 8, 2016 16:21
Decorators Codeblock 13
class BaseDecorator < Draper::Decorator
  delegate_all

  def self.currency(*keys)
  end
end
@francirp
francirp / decorators_codeblock_12.md
Created August 8, 2016 16:20
Decorators Codeblock 12
class CompanyDecorator < BaseDecorator
  delegate_all

  def budget
    h.number_with_precision(object.budget, precision: 2)
  end

  def display_budget
 h.number_to_currency(object.budget, precision: 0)
@francirp
francirp / decorators_codeblock_11.md
Created August 8, 2016 16:20
Decorators Codeblock 11
class CompanyDecorator < BaseDecorator
  delegate_all

  currency :budget
end