<commit_before>Draper
================

This gem makes it easy to apply the decorator pattern to the models in a Rails application.

## Why use decorators?

Helpers, as they're commonly used, are a bit odd. In both Ruby and 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 or a data object and output presentation-ready results. We can do that job in an OO fashion with a decorator.

In general, a decorator wraps an object with presentation-related accessor methods. For instance, if you had an `Article` object, then a decorator might add instance methods like `.formatted_published_at` or `.formatted_title` that output actual HTML.

## How is it implemented?

To implement the pattern in Rails we can:

1. Write a wrapper class with the decoration methods
2. Wrap the data object
3. Utilize those methods within our view layer

## How do you utilize this gem in your application?

Here are the steps to utilizing this gem:

Add the dependency to your `Gemfile`:

```
gem "draper"
```

Run bundle:

```
bundle
```

Create a decorator for your model (ex: `Article`)

```
rails generate draper:model Article
```

Open the decorator model (ex: `app/decorators/article_decorator.rb`)

Add your new formatting methods as normal instance or class methods. You have access to the Rails helpers from the following classes:

```
ActionView::Helpers::TagHelper
ActionView::Helpers::UrlHelper
ActionView::Helpers::TextHelper
```

Use the new methods in your views like any other model method (ex: `@article.formatted_published_at`)

## Possible Decoration Methods

Here are some ideas of what you might do in decorator methods:

* Implement output formatting for `to_csv`, `to_json`, or `to_xml`
* Format dates and times using `strftime`
* Implement a commonly used representation of the data object like a `.name` method that combines `first_name` and `last_name` attributes<commit_msg>Added allows/denies to Readme<commit_after>Draper
================

This gem makes it easy to apply the decorator pattern to the models in a Rails application.

## Why use decorators?

Helpers, as they're commonly used, are a bit odd. In both Ruby and 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 or a data object and output presentation-ready results. We can do that job in an OO fashion with a decorator.

In general, a decorator wraps an object with presentation-related accessor methods. For instance, if you had an `Article` object, then a decorator might add instance methods like `.formatted_published_at` or `.formatted_title` that output actual HTML.

## How is it implemented?

To implement the pattern in Rails we can:

1. Write a wrapper class with the decoration methods
2. Wrap the data object
3. Utilize those methods within our view layer

## How do you utilize this gem in your application?

Here are the steps to utilizing this gem:

Add the dependency to your `Gemfile`:

```
gem "draper"
```

Run bundle:

```
bundle
```

Create a decorator for your model (ex: `Article`)

```
rails generate draper:model Article
```

Open the decorator model (ex: `app/decorators/article_decorator.rb`)

Add your new formatting methods as normal instance or class methods. You have access to the Rails helpers from the following classes:

```
ActionView::Helpers::TagHelper
ActionView::Helpers::UrlHelper
ActionView::Helpers::TextHelper
```

Use the new methods in your views like any other model method (ex: `@article.formatted_published_at`)

## An Interface with Allows/Denies

A proper interface defines a contract between two objects. One purpose of the decorator pattern is to define an interface between your data model and the view template.

You are provided class methods `allows` and `denies` to control exactly which of the subject's methods are available. By default, *all* of the subject's methods can be accessed.

For example, say you want to prevent access to the `:title` method. You'd use `denies` like this:

```ruby
  class ArticleDecorator < Draper::Base
    denies :title
  end
```

`denies` uses a blacklist approach. Note that, as of the current version, denying `:title` does not affect related methods like `:title=`, `:title?`, etc.

A better idea is a whitelist approach using `allows`:

```ruby
  class ArticleDecorator < Draper::Base
    allows :title, :body, :author
  end
```

Now only those methods and any defined in the decorator class itself can be accessed directly.

## Possible Decoration Methods

Here are some ideas of what you might do in decorator methods:

* Implement output formatting for `to_csv`, `to_json`, or `to_xml`
* Format dates and times using `strftime`
* Implement a commonly used representation of the data object like a `.name` method that combines `first_name` and `last_name` attributes
{'fraction_non_alphanumeric': 0.07414178346536665, 'fraction_numerical': 0.001218769043266301, 'mean_word_length': 4.718931475029036, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '4689803', 'n_tokens_mistral': 1359, 'n_tokens_neox': 1301, 'n_words': 701}