<ul>
<li>Budget: <%= company.display_budget %></li>
<li>Actual: <%= company.display_actual %></li>
<li>Percent Spent: <%= company.display_percent_budget_spent %></li>
</ul>
# app/decorators/company_decorator.rb
class CompanyDecorator < Draper::Decorator
delegate_all
def budget
h.number_with_precision(object.budget, precision: 2)
end
<%= simple_form_for company do |f| %>
<%= f.input :name %>
<%= f.input :budget, as: :string %>
<%= f.input :actual, as: :string %>
<%= f.submit %>
<% end %>
<ul>
<li>Budget: <%= company.budget %></li>
<li>Actual: <%= company.actual %></li>
<li>Percent Spent: <%= company.percent_budget_spent %></li>
</ul>
module CompaniesHelper
def company
@decorated_company ||= @company.decorate
end
end
# app/decorators/company_decorator.rb
class CompanyDecorator < Draper::Decorator
delegate_all
def budget
h.number_to_currency(object.budget, precision: 0)
end
<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>
<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>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |