decorate_resource 1.0.0
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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +58 -0
- data/Rakefile +2 -0
- data/decorate_resource.gemspec +15 -0
- data/lib/decorate_resource.rb +46 -0
- metadata +52 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Jason Coene
|
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,58 @@
|
|
1
|
+
# DecorateResource
|
2
|
+
|
3
|
+
It decorates your idiomatic Rails controller resources by running them through a decorator.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'decorate_resource'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install decorate_resource
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Your controllers will gain a decorate_resource macro that will take your instanciated variables and run them through an appropriately named decorator. Here's a before and after:
|
22
|
+
|
23
|
+
**Before:**
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class UsersController < ApplicationController
|
27
|
+
load_resource
|
28
|
+
|
29
|
+
def index
|
30
|
+
@users # [User, User, ...]
|
31
|
+
end
|
32
|
+
|
33
|
+
def show
|
34
|
+
@user # User
|
35
|
+
end
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
**After**
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
class UsersController < ApplicationController
|
43
|
+
load_resource
|
44
|
+
decorate_resource
|
45
|
+
|
46
|
+
def index
|
47
|
+
@users # [UserDecorator, UserDecorator, ...]
|
48
|
+
end
|
49
|
+
|
50
|
+
def show
|
51
|
+
@user # UserDecorator
|
52
|
+
end
|
53
|
+
end
|
54
|
+
```
|
55
|
+
|
56
|
+
## License
|
57
|
+
|
58
|
+
MIT
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
Gem::Specification.new do |gem|
|
3
|
+
gem.authors = ["Jason Coene"]
|
4
|
+
gem.email = ["jcoene@gmail.com"]
|
5
|
+
gem.description = %q{Automatically decorates your resourceful controller}
|
6
|
+
gem.summary = %q{Automatically decorates your resourceful controller}
|
7
|
+
gem.homepage = ""
|
8
|
+
|
9
|
+
gem.files = `git ls-files`.split($\)
|
10
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
11
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
12
|
+
gem.name = "decorate_resource"
|
13
|
+
gem.require_paths = ["lib"]
|
14
|
+
gem.version = "1.0.0"
|
15
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module DecorateResource
|
2
|
+
def self.included(base)
|
3
|
+
base.extend ClassMethods
|
4
|
+
end
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def decorate_resource
|
8
|
+
before_filter :decorate_resource
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
protected
|
13
|
+
|
14
|
+
def resource_name
|
15
|
+
self.class.name.demodulize.gsub(/Controller/, "").singularize
|
16
|
+
end
|
17
|
+
|
18
|
+
def resource_instance_variable_name
|
19
|
+
if current_action_type == :member
|
20
|
+
"@#{resource_name.constantize.name.underscore}"
|
21
|
+
else
|
22
|
+
"@#{resource_name.constantize.name.pluralize.underscore}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def current_action_type
|
27
|
+
if (params.include?(:id) || [:new, :create, :edit, :update, :destroy].include?(params[:action].to_sym)) && !([:index].include? params[:action].to_sym)
|
28
|
+
:member
|
29
|
+
else
|
30
|
+
:collection
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def decorate_resource
|
35
|
+
records = instance_variable_get resource_instance_variable_name
|
36
|
+
decorated_records = "#{resource_name}Decorator".constantize.decorate records
|
37
|
+
instance_variable_set resource_instance_variable_name, decorated_records
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
if defined? ActionController::Base
|
43
|
+
ActionController::Base.class_eval do
|
44
|
+
include DecorateResource
|
45
|
+
end
|
46
|
+
end
|
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: decorate_resource
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jason Coene
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-26 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Automatically decorates your resourceful controller
|
15
|
+
email:
|
16
|
+
- jcoene@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- decorate_resource.gemspec
|
27
|
+
- lib/decorate_resource.rb
|
28
|
+
homepage: ''
|
29
|
+
licenses: []
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
requirements: []
|
47
|
+
rubyforge_project:
|
48
|
+
rubygems_version: 1.8.23
|
49
|
+
signing_key:
|
50
|
+
specification_version: 3
|
51
|
+
summary: Automatically decorates your resourceful controller
|
52
|
+
test_files: []
|