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 / decorators_codeblock_7.md
Created August 8, 2016 15:48
Decorators Codeblock 7
<ul>
  <li>Budget: <%= company.display_budget %></li>
  <li>Actual: <%= company.display_actual %></li>
  <li>Percent Spent: <%= company.display_percent_budget_spent %></li>
</ul>
@francirp
francirp / decorators_codeblock_6.md
Created August 8, 2016 15:47
Decorators Codeblock 6
# app/decorators/company_decorator.rb

class CompanyDecorator < Draper::Decorator
  delegate_all

  def budget
    h.number_with_precision(object.budget, precision: 2)
  end
@francirp
francirp / decorators_codeblock_5.md
Created August 8, 2016 15:40
Decorators Codeblock 5
<%= simple_form_for company do |f| %>
  <%= f.input :name %>
  <%= f.input :budget, as: :string %>
  <%= f.input :actual, as: :string %>
  <%= f.submit %>
<% end %>
@francirp
francirp / decorators_codeblock_4.md
Created August 8, 2016 15:39
Decorators Codeblock 4
<ul>
  <li>Budget: <%= company.budget %></li>
  <li>Actual: <%= company.actual %></li>
  <li>Percent Spent: <%= company.percent_budget_spent %></li>
</ul>
@francirp
francirp / decorators_codeblock_3.md
Created August 8, 2016 15:36
Decorators Codeblock 3
module CompaniesHelper
  def company
    @decorated_company ||= @company.decorate
  end
end
@francirp
francirp / decorators_codeblock_2.md
Created August 8, 2016 15:35
Decorators Codeblock 2
# app/decorators/company_decorator.rb

class CompanyDecorator < Draper::Decorator
  delegate_all

  def budget
    h.number_to_currency(object.budget, precision: 0)
  end
@francirp
francirp / decorators_codeblock_1.md
Created August 8, 2016 15:22
Decorators - Code Block 1
<ul>
  <li>Budget: <%= number_to_currency(@company.budget, precision: 0) %></li>
  <li>Actual: <%= number_to_currency(@company.actual, precision: 0) %></li>
  <li>Percent Spent: <%= number_to_percentage(@company.percent_budget_spent * 100, precision: 1) %></li>
</ul>
@francirp
francirp / codeblock_1.md
Last active August 8, 2016 15:12
How Decorators Saved My Rails App
<ul>
  <li>Budget: <%= number_to_currency(@company.budget, precision: 0) %></li>
  <li>Actual: <%= number_to_currency(@company.actual, precision: 0) %></li>
  <li>Percent Spent: <%= number_to_percentage(@company.percent_budget_spent * 100, precision: 1) %></li>
</ul>
@francirp
francirp / bottles.rb
Created July 31, 2016 15:59
My 99bottles solution
class Bottles
attr_reader :bottle_number
def verse(bottle_number)
@bottle_number = bottle_number
return plural if bottle_number > 2
return two if bottle_number == 2
return one if bottle_number == 1
return zero if bottle_number == 0
end
@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