<commit_before># Draper: View Models for Rails

## Quick Start

1. Add `gem 'draper'` to your `Gemfile` and `bundle`
2. Run `rails g draper:model YourModel`
3. Edit `app/decorators/your_model_decorator.rb` using:
  1. `h` to proxy to Rails/application helpers like `h.current_user`
  2. `model` to access the wrapped object
4. Wrap models in your controller with the decorator using:
  1. `.find` lookup: `ArticleDecorator.find(1)`
  2. `.decorate` method: `ArticleDecorator.decorate(Article.all)`
  3. `.new` method: `ArticleDecorator.new(Article.first)`
5. Call and output the methods in your view templates as normal

## Goals

This gem makes it easy to apply the decorator pattern to the data models in a Rails application. This gives you three wins:

1. Replace most helpers with an object-oriented approach
2. Filter data at the presentation level
3. Enforce an interface between your controllers and view templates.


### 1. Object Oriented Helpers

Why hate helpers? In Ruby/Rails we approach everything from an Object-Oriented perspective, then with helpers we get procedural.The job of a helper is to take in data and output a presentation-ready string. We can do that with a decorator.

A decorator wraps an object with presentation-related accessor methods. For instance, if you had an `Article` object, then the decorator could override `.published_at` to use formatted output like this:

```ruby
class ArticleDecorator < Draper::Base
  decorates :article
  def published_at
    date = h.content_tag(:span, model.published_at.strftime("%A, %B %e").squeeze(" "), :class => 'date')
    time = h.content_tag(:span, model.published_at.strftime("%l:%M%p"), :class => 'time').delete(" ")
    h.content_tag :span, date + time, :class => 'created_at'
  end
end<commit_msg>Tweaks to the quickstart<commit_after># Draper: View Models for Rails

## Quick Start

1. Add `gem 'draper'` to your `Gemfile` and `bundle`
2. Run `rails g draper:model YourModel`
3. Edit `app/decorators/your_model_decorator.rb` using:
  1. `h` to proxy to Rails/application helpers like `h.current_user`
  2. `model` to access the wrapped object like `model.created_at`
4. Wrap models in your controller with the decorator using:
  1. `.find` automatic lookup & wrap: `ArticleDecorator.find(1)`
  2. `.decorate` method with single object or collection: `ArticleDecorator.decorate(Article.all)`
  3. `.new` method with single object: `ArticleDecorator.new(Article.first)`
5. Call and output the methods in your view templates as normal

## Goals

This gem makes it easy to apply the decorator pattern to the data models in a Rails application. This gives you three wins:

1. Replace most helpers with an object-oriented approach
2. Filter data at the presentation level
3. Enforce an interface between your controllers and view templates.


### 1. Object Oriented Helpers

Why hate helpers? In Ruby/Rails we approach everything from an Object-Oriented perspective, then with helpers we get procedural.The job of a helper is to take in data and output a presentation-ready string. We can do that with a decorator.

A decorator wraps an object with presentation-related accessor methods. For instance, if you had an `Article` object, then the decorator could override `.published_at` to use formatted output like this:

```ruby
class ArticleDecorator < Draper::Base
  decorates :article
  def published_at
    date = h.content_tag(:span, model.published_at.strftime("%A, %B %e").squeeze(" "), :class => 'date')
    time = h.content_tag(:span, model.published_at.strftime("%l:%M%p"), :class => 'time').delete(" ")
    h.content_tag :span, date + time, :class => 'created_at'
  end
end
{'fraction_non_alphanumeric': 0.09622744669218151, 'fraction_numerical': 0.008201202843083653, 'mean_word_length': 4.510542168674699, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5167140', 'n_tokens_mistral': 1132, 'n_tokens_neox': 1081, 'n_words': 466}