little_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 +90 -0
- data/lib/helpers/little_decorator_helper.rb +11 -0
- data/lib/little_decorator.rb +31 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 97f62e2eafec5e0d0e22153528ca392aaf34a4ec
|
4
|
+
data.tar.gz: 99fdb46542f2870961e2530463f896b9f1d172a1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2f45e99d3ada64a464a7bcca2f24f20e81009112bff0850b23ea2f1b85763f3886c71f78ca14b2aa22a6d2b204fd2fffb061dfc6b656a70b316455cae41b162c
|
7
|
+
data.tar.gz: 597c1af108ff2bce6a43945a1b58bf691cda207e42e87b74fc0bb8816d84181b756fc4f6b52ddd132fe685bb943dcf90ae8c90ce435a2f12dda707d8cdabd4f3
|
data/README.md
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
[](https://codeclimate.com/github/vicramon/interior_decorator)
|
2
|
+
|
3
|
+

|
4
|
+
|
5
|
+
Ultra lightweight decorator for Rails models. LittleDecorator has just 42 actionable lines of code.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Include it in your Gemfile.
|
10
|
+
|
11
|
+
```
|
12
|
+
gem 'little_decorator'
|
13
|
+
```
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
### Create Your Decorator
|
18
|
+
|
19
|
+
Add your decorator in `app/decorators`:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
# app/decorators/user_decorator.rb
|
23
|
+
class UserDecorator < LittleDecorator
|
24
|
+
|
25
|
+
def full_name
|
26
|
+
"#{first_name} #{last_name}"
|
27
|
+
end
|
28
|
+
|
29
|
+
def updated_at
|
30
|
+
model.updated_at.strftime("%A, %B %e, %Y")
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
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.
|
37
|
+
|
38
|
+
Call model methods with `model` when you want to override a method but still get to the original model.
|
39
|
+
|
40
|
+
You can access helper methods and route helpers in your decorators.
|
41
|
+
|
42
|
+
### Decorate Your Objects
|
43
|
+
|
44
|
+
You can call `decorate` on an object or a collection in both controllers and views. Examples below.
|
45
|
+
|
46
|
+
#### In Controllers
|
47
|
+
|
48
|
+
Just call `decorate`:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
decorate(user)
|
52
|
+
```
|
53
|
+
|
54
|
+
#### In Views
|
55
|
+
|
56
|
+
Just call `decorate`:
|
57
|
+
|
58
|
+
```erb
|
59
|
+
<%= decorate(user) %>
|
60
|
+
```
|
61
|
+
|
62
|
+
#### On Collections
|
63
|
+
|
64
|
+
Just call `decorate`. You'll get an array of decorated objects
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
decorate(users)
|
68
|
+
```
|
69
|
+
|
70
|
+
```erb
|
71
|
+
<%= decorate(users) %>
|
72
|
+
```
|
73
|
+
|
74
|
+
## Vim Projections
|
75
|
+
|
76
|
+
For use with Rails.vim. Place in `config/projections.json`.
|
77
|
+
|
78
|
+
```json
|
79
|
+
{
|
80
|
+
"app/decorators/*_decorator.rb": {
|
81
|
+
"command": "decorator",
|
82
|
+
"alternate": "spec/decorators/%s_decorator_spec.rb",
|
83
|
+
"template": "class %SDecorator < LittleDecorator\nend"
|
84
|
+
}
|
85
|
+
}
|
86
|
+
```
|
87
|
+
|
88
|
+
## Contribute
|
89
|
+
|
90
|
+
Pull requests are welcome, but I want to keep this gem simple.
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module LittleDecoratorHelper
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
included { helper_method :decorate, :d }
|
4
|
+
|
5
|
+
def decorate(item)
|
6
|
+
klass = Array(item).first.class
|
7
|
+
decorator = "#{klass}Decorator".constantize
|
8
|
+
decorator.decorate(item, view_context)
|
9
|
+
end
|
10
|
+
alias_method :d, :decorate
|
11
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'helpers/little_decorator_helper'
|
2
|
+
ActiveSupport.on_load(:action_controller) { include LittleDecoratorHelper }
|
3
|
+
|
4
|
+
class LittleDecorator
|
5
|
+
|
6
|
+
attr_reader :record, :view
|
7
|
+
|
8
|
+
def self.decorate(record_or_collection, view)
|
9
|
+
if record_or_collection.respond_to?(:map)
|
10
|
+
record_or_collection.map { |record| new(record, view) }
|
11
|
+
else
|
12
|
+
new(record_or_collection, view)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize(record, view)
|
17
|
+
@record, @view = record, view
|
18
|
+
end
|
19
|
+
alias_method :model, :record
|
20
|
+
|
21
|
+
def method_missing(method_name, *args, &block)
|
22
|
+
[record, view].each do |item|
|
23
|
+
return item.public_send(method_name, *args, &block) if item.respond_to?(method_name)
|
24
|
+
end
|
25
|
+
super
|
26
|
+
end
|
27
|
+
|
28
|
+
def respond_to_missing?(method_name, include_private=false)
|
29
|
+
[record, view].any? { |item| item.respond_to?(method_name, include_private) }
|
30
|
+
end
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: little_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-11 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
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec-rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Ultra lightweight decorator for Rails models.
|
56
|
+
email:
|
57
|
+
- vic@vicramon.com.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- README.md
|
63
|
+
- lib/helpers/little_decorator_helper.rb
|
64
|
+
- lib/little_decorator.rb
|
65
|
+
homepage:
|
66
|
+
licenses:
|
67
|
+
- MIT
|
68
|
+
metadata: {}
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 1.9.3
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 2.2.1
|
86
|
+
signing_key:
|
87
|
+
specification_version: 4
|
88
|
+
summary: Ultra lightweight decorator for Rails models.
|
89
|
+
test_files: []
|