interior_decorator 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +89 -0
- data/lib/helpers/interior_decorator_helper.rb +9 -0
- data/lib/interior_decorator.rb +33 -0
- metadata +61 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 28eef2f2708febda5f867b4d79988824529bf048
|
4
|
+
data.tar.gz: 31671ce34ed9e986684c815536905a055d25672d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e3f5cdb0f9fb10fc8ca0047d81e93c0e419f4a2d9089bad281a52946b594c89436ae7360fe6961b839db0619f9d2b50c55bec0ec81ca9abf80b700d27dbdedbf
|
7
|
+
data.tar.gz: e6fd4a82d35a3097399e64e9dc779e21556c28ad79b41e3c5484f1a751d03b111287b0d661df7be5600340a5e028495d869b8b29d21cdf3338fd7381e1f78998
|
data/README.md
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
# Interior Decorator
|
2
|
+
|
3
|
+
[](https://codeclimate.com/github/vicramon/interior_decorator)
|
4
|
+
|
5
|
+
Ultra-lightweight decorator for Rails models. Only 42 actionable lines of code!
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Include it in your Gemfile.
|
10
|
+
|
11
|
+
```
|
12
|
+
gem 'interior_decorator'
|
13
|
+
```
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
### Create Your Decorator
|
18
|
+
|
19
|
+
Add your decorator in `app/decorators`:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
class UserDecorator < InteriorDecorator
|
23
|
+
|
24
|
+
def full_name
|
25
|
+
"#{first_name} #{last_name}"
|
26
|
+
end
|
27
|
+
|
28
|
+
def updated_at
|
29
|
+
model.updated_at.strftime("%A, %B %e, %Y")
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
Method calls are sent to the model via `method_missing`, so you can call model methods directly as in the `full_name` method defined above.
|
36
|
+
|
37
|
+
If there's going to be a method name collision then call model methods with `model.`.
|
38
|
+
|
39
|
+
You can access helper methods with `helper.`, or `h.` for short.
|
40
|
+
|
41
|
+
### Decorate Your Objects
|
42
|
+
|
43
|
+
#### In Controllers
|
44
|
+
|
45
|
+
Just call decorate:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
UserDecorator.decorate(user)
|
49
|
+
```
|
50
|
+
|
51
|
+
#### In Views
|
52
|
+
|
53
|
+
Just call decorate:
|
54
|
+
|
55
|
+
```erb
|
56
|
+
<%= decorate(user) %>
|
57
|
+
```
|
58
|
+
|
59
|
+
Or `d` for short.
|
60
|
+
|
61
|
+
#### On Collections
|
62
|
+
|
63
|
+
Just call decorate. You'll get an array of decorated objects.
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
UserDecorator.decorate(users)
|
67
|
+
```
|
68
|
+
|
69
|
+
```erb
|
70
|
+
<%= decorate(users) %>
|
71
|
+
```
|
72
|
+
|
73
|
+
## Vim Projections
|
74
|
+
|
75
|
+
For use with Rails.vim. Place in `config/projections.json`.
|
76
|
+
|
77
|
+
```json
|
78
|
+
{
|
79
|
+
"app/decorators/*_decorator.rb": {
|
80
|
+
"command": "decorator",
|
81
|
+
"alternate": "spec/decorators/%s_decorator_spec.rb",
|
82
|
+
"template": "class %SDecorator < InteriorDecorator\nend"
|
83
|
+
}
|
84
|
+
}
|
85
|
+
```
|
86
|
+
|
87
|
+
## Contribute
|
88
|
+
|
89
|
+
Pull requests are welcome, but let's keep this thing simple.
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'helpers/interior_decorator_helper'
|
2
|
+
class InteriorDecorator
|
3
|
+
attr_reader :model
|
4
|
+
|
5
|
+
def self.decorate(item)
|
6
|
+
if item.respond_to?(:map)
|
7
|
+
item.map { |object| new(object) }
|
8
|
+
else
|
9
|
+
new(item)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(model)
|
14
|
+
@model = model
|
15
|
+
end
|
16
|
+
|
17
|
+
def helper
|
18
|
+
ActionController::Base.helpers
|
19
|
+
end
|
20
|
+
alias_method :h, :helper
|
21
|
+
|
22
|
+
def method_missing(method_name, *args, &block)
|
23
|
+
if model.respond_to?(method_name)
|
24
|
+
model.public_send(method_name, *args, &block)
|
25
|
+
else
|
26
|
+
super
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def respond_to_missing?(method_name, include_private = false)
|
31
|
+
model.respond_to?(method_name, include_private)
|
32
|
+
end
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: interior_decorator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Vic Ramon
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Ultra-lightweight decorator for Rails models.
|
28
|
+
email:
|
29
|
+
- vic@vicramon.com.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- README.md
|
35
|
+
- lib/helpers/interior_decorator_helper.rb
|
36
|
+
- lib/interior_decorator.rb
|
37
|
+
homepage:
|
38
|
+
licenses:
|
39
|
+
- MIT
|
40
|
+
metadata: {}
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: 1.9.3
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 2.2.1
|
58
|
+
signing_key:
|
59
|
+
specification_version: 4
|
60
|
+
summary: Ultra-lightweight decorator for Rails models.
|
61
|
+
test_files: []
|