curtains 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +85 -0
- data/Rakefile +1 -0
- data/curtains.gemspec +23 -0
- data/lib/curtains.rb +35 -0
- data/lib/curtains/decorate_resources.rb +28 -0
- data/lib/curtains/decorator.rb +41 -0
- data/lib/curtains/version.rb +3 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 97d7f5bf6f31c730a42513db42d91d0f173651b8
|
4
|
+
data.tar.gz: bd15448ec98de9cb735e7a248b57f2305a51217a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2a31e5c9e620b071c5651b3560c891e66d23b498540b34d7f0eb8846a5fd9965cd65efcbe528a5024a9706fff40db55c6ef4d334d90ec9b93e1e200141c5a583
|
7
|
+
data.tar.gz: 70dc130b362e198a52bde61f35dae36292db4e3cfda1337ade44d6af55a5feeef8918099cf064eaf274b2a3ed60ecbf17b6316dadf88910e3e473b64632836f3
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Patrick Hogan
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
# Curtains
|
2
|
+
|
3
|
+
Curtains provides simple model decorators for Rails 4.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's `Gemfile`:
|
8
|
+
|
9
|
+
gem "curtains"
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install curtains
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
In either controllers or views you may call `decorate` to wrap a model in a decorator. The name of the decorator class is inferred from the name of the model, so for example, the `Address` model will be wrapped with `AddressDecorator`.
|
22
|
+
|
23
|
+
```erb
|
24
|
+
# app/views/addresses/show.html.erb
|
25
|
+
<%= decorate(@address).formatted_address_tag %>
|
26
|
+
```
|
27
|
+
|
28
|
+
The `decorate` helper also supports enumerable collections and will return an array of decorated objects:
|
29
|
+
|
30
|
+
```erb
|
31
|
+
# app/views/addresses/index.html.erb
|
32
|
+
<%= decorate(@addresses).each do |address| %>
|
33
|
+
<%= address.formatted_address_tag %>
|
34
|
+
<% end %>
|
35
|
+
```
|
36
|
+
|
37
|
+
Additionally, the `decorate` helper yields its result to a block if provided:
|
38
|
+
|
39
|
+
```erb
|
40
|
+
# app/views/addresses/show.html.erb
|
41
|
+
<%= decorate(@address) do |address| %>
|
42
|
+
<div class="name">
|
43
|
+
<%= address.formatted_name %>
|
44
|
+
</div>
|
45
|
+
<%= address.formatted_address_tag %>
|
46
|
+
<% end %>
|
47
|
+
```
|
48
|
+
|
49
|
+
These decorators should be placed in the `app/decorators` folder and should inherit from `Curtains::Decorator`. For example:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
# app/decorators/address_decorator.rb
|
53
|
+
class AddressDecorator < Curtains::Decorator
|
54
|
+
def formatted_address_tag
|
55
|
+
content_tag :address, address.lines.join("<br/>").html_safe
|
56
|
+
end
|
57
|
+
|
58
|
+
def formatted_name
|
59
|
+
return "N/A" if name.blank?
|
60
|
+
name
|
61
|
+
end
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
## Inherited Resources Compatibility
|
66
|
+
|
67
|
+
Call `decorate_resources` in your controller to add automatic decorating to the `resource`, `build_resource` and `collection` methods provided by Inherited Resources:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
# app/controllers/address_controller.rb
|
71
|
+
class AddressesController < InheritedResources::Base
|
72
|
+
decorate_resources
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
```erb
|
77
|
+
# app/views/addresses/show.html.erb
|
78
|
+
<%= resource.formatted_address_tag %>
|
79
|
+
```
|
80
|
+
|
81
|
+
## Meta
|
82
|
+
|
83
|
+
Handcrafted by Patrick Hogan [[github](http://github.com/pbhogan)]
|
84
|
+
|
85
|
+
Released under the [MIT License](http://www.opensource.org/licenses/mit-license.php).
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/curtains.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'curtains/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "curtains"
|
8
|
+
spec.version = Curtains::VERSION
|
9
|
+
spec.authors = ["Patrick Hogan"]
|
10
|
+
spec.email = ["pbhogan@gmail.com"]
|
11
|
+
spec.description = %q{Simple model decorators for Rails 4}
|
12
|
+
spec.summary = %q{Simple model decorators for Rails 4}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
data/lib/curtains.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require "rails/engine"
|
2
|
+
|
3
|
+
|
4
|
+
module Curtains
|
5
|
+
autoload :Decorator, "curtains/decorator"
|
6
|
+
autoload :DecorateResources, "curtains/decorate_resources"
|
7
|
+
autoload :VERSION, "curtains/version"
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
class ActionController::Base
|
12
|
+
|
13
|
+
helper_method :decorate
|
14
|
+
def decorate(model)
|
15
|
+
if model.respond_to? :each
|
16
|
+
decorated = model.map { |m| decorate(m) }
|
17
|
+
yield(decorated) if block_given?
|
18
|
+
decorated
|
19
|
+
else
|
20
|
+
decorator = "#{model.class}Decorator".constantize
|
21
|
+
decorated = decorator.new(model, view_context, decorator)
|
22
|
+
yield(decorated) if block_given?
|
23
|
+
decorated
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
def self.decorate_resources
|
29
|
+
self.class_eval do
|
30
|
+
include Curtains::DecorateResources
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Curtains
|
2
|
+
module DecorateResources
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
alias_method_chain :resource, :decoration
|
7
|
+
alias_method_chain :build_resource, :decoration
|
8
|
+
alias_method_chain :collection, :decoration
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
def resource_with_decoration
|
13
|
+
decorate(resource_without_decoration)
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
def build_resource_with_decoration
|
18
|
+
decorate(build_resource_without_decoration)
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
def collection_with_decoration
|
23
|
+
decorate(collection_without_decoration)
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require "delegate"
|
2
|
+
|
3
|
+
module Curtains
|
4
|
+
class Decorator < SimpleDelegator
|
5
|
+
undef_method :==
|
6
|
+
|
7
|
+
|
8
|
+
def initialize(base, view_context, decorator)
|
9
|
+
super(base)
|
10
|
+
@view_context = view_context
|
11
|
+
@decorator = decorator
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
def class
|
16
|
+
__getobj__.class
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
def h
|
21
|
+
@view_context
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
def html_escape(*args)
|
26
|
+
ERB::Util.html_escape(*args)
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
def decorated?
|
31
|
+
true
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
def decorated_with
|
36
|
+
@decorator
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: curtains
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Patrick Hogan
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
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
|
+
description: Simple model decorators for Rails 4
|
42
|
+
email:
|
43
|
+
- pbhogan@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- curtains.gemspec
|
54
|
+
- lib/curtains.rb
|
55
|
+
- lib/curtains/decorate_resources.rb
|
56
|
+
- lib/curtains/decorator.rb
|
57
|
+
- lib/curtains/version.rb
|
58
|
+
homepage: ''
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata: {}
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 2.1.10
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: Simple model decorators for Rails 4
|
82
|
+
test_files: []
|