padrino-decorator 0.0.1 → 0.0.2
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 +4 -4
- data/.travis.yml +6 -1
- data/Gemfile +5 -1
- data/README.md +44 -1
- data/lib/generators/decorator.rb +4 -1
- data/lib/generators/templates/decorator.rb.tt +3 -3
- data/lib/padrino-decorator/base.rb +11 -15
- data/lib/padrino-decorator/decorate_helpers.rb +8 -3
- data/lib/padrino-decorator/helpers.rb +4 -2
- data/lib/padrino-decorator/version.rb +1 -1
- data/padrino-decorator.gemspec +3 -4
- data/test/helpers/helper.rb +5 -0
- data/test/test_base.rb +4 -5
- data/test/test_decorate_helpers.rb +10 -13
- metadata +22 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e1b703f7ffaa93aac68495e727c6bf1d42dcb2b4
|
4
|
+
data.tar.gz: 73837985534feffac2f2279271923094d4365568
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 433c0fefa75c02a64be79bdd30aed13d606ddfa16426d8e883899b585fcace77593ce7d73f80d346385765d4a1575710b29bdcc50ed789f552416e0c7b619ec4
|
7
|
+
data.tar.gz: 13bdfd7a84bbea49ae5f2ad9af380bea0c3997bf5ffc398378184cdbda825349025832e58420a5be9b7b966274a83c46dda46c3d60cdfbb19d5e5e7227dd2347
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
@@ -3,6 +3,10 @@ source 'https://rubygems.org'
|
|
3
3
|
gemspec
|
4
4
|
gem 'rake'
|
5
5
|
|
6
|
+
group :development, :test do
|
7
|
+
gem 'coveralls', :require => false
|
8
|
+
end
|
9
|
+
|
6
10
|
group :test do
|
7
11
|
gem 'rack-test', '>= 0.5.0'
|
8
12
|
gem 'minitest', '~> 4.0'
|
@@ -12,6 +16,6 @@ end
|
|
12
16
|
|
13
17
|
group :development do
|
14
18
|
gem 'yard', '>= 0.7.2'
|
15
|
-
gem '
|
19
|
+
gem 'kramdown'
|
16
20
|
gem 'github-markup'
|
17
21
|
end
|
data/README.md
CHANGED
@@ -3,6 +3,11 @@
|
|
3
3
|
padrino-decorator is a gem for [Padrino](http://www.padrinorb.com/).
|
4
4
|
Adds an object-oriented layer of presentation logic to your Padrino application.
|
5
5
|
|
6
|
+
[](https://travis-ci.org/tyabe/padrino-decorator)
|
7
|
+
[](https://codeclimate.com/github/tyabe/padrino-decorator)
|
8
|
+
[](https://coveralls.io/r/tyabe/padrino-decorator)
|
9
|
+
[](https://gemnasium.com/tyabe/padrino-decorator)
|
10
|
+
|
6
11
|
## Installation
|
7
12
|
|
8
13
|
Add the following to your `Gemfile`:
|
@@ -17,7 +22,7 @@ And then execute:
|
|
17
22
|
$ bundle
|
18
23
|
```
|
19
24
|
|
20
|
-
##
|
25
|
+
## Decorator Generator
|
21
26
|
|
22
27
|
Padrino provides generator support for quickly creating new decorators within your Padrino application.
|
23
28
|
Note that the decorator tests are generated specifically tailored towards the testing framework chosen during application generation.
|
@@ -68,6 +73,44 @@ $ padrino-gen decorator User
|
|
68
73
|
</tbody>
|
69
74
|
</table>
|
70
75
|
|
76
|
+
## Examples
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
# app/controllers/posts.rb
|
80
|
+
SampleProject::App.controllers :posts do
|
81
|
+
|
82
|
+
get :index do
|
83
|
+
source = Post.all
|
84
|
+
@posts = decorate(source)
|
85
|
+
render 'posts/index'
|
86
|
+
end
|
87
|
+
|
88
|
+
get :show, with: :id do
|
89
|
+
source = Post.find(params[:id])
|
90
|
+
@post = decorate(source)
|
91
|
+
# or
|
92
|
+
@post = PostDecorator.new(source, context: self)
|
93
|
+
render 'posts/show'
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
|
98
|
+
# app/decorators/post_decorator.rb
|
99
|
+
class PostDecorator < Padrino::Decorator::Base
|
100
|
+
context SampleProject::App
|
101
|
+
|
102
|
+
def formated_body
|
103
|
+
h.simple_format(object.body)
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
|
108
|
+
# app/views/posts/show.slim
|
109
|
+
h1 = @post.title
|
110
|
+
div
|
111
|
+
= @post.formated_body
|
112
|
+
```
|
113
|
+
|
71
114
|
## Contributing
|
72
115
|
|
73
116
|
1. Fork it
|
data/lib/generators/decorator.rb
CHANGED
@@ -41,7 +41,10 @@ module Padrino
|
|
41
41
|
check_app_existence(app)
|
42
42
|
inject_into_file(destination_root(app, "app.rb"), " register Padrino::Decorator::Helpers\n", :after => "Padrino::Application\n")
|
43
43
|
self.behavior = :revoke if options[:destroy]
|
44
|
-
@object_name
|
44
|
+
@object_name = name.to_s.underscore
|
45
|
+
@project_name = options[:namespace].underscore.camelize
|
46
|
+
@project_name = fetch_project_name(app) if @project_name.empty?
|
47
|
+
@app_name = fetch_app_name(app)
|
45
48
|
template 'templates/decorator.rb.tt', destination_root(app, 'decorators', "#{@object_name}_decorator.rb")
|
46
49
|
if test?
|
47
50
|
choice = fetch_component_choice(:test)
|
@@ -1,11 +1,11 @@
|
|
1
1
|
class <%= @object_name.camelize %>Decorator < Padrino::Decorator::Base
|
2
|
-
|
2
|
+
context <%= @project_name %>::<%= @app_name %>
|
3
3
|
|
4
4
|
# def decorated_method
|
5
5
|
# # We can use a specified object through the accessor method.
|
6
6
|
# # Also can use a helper method in specified context through the 'h' method.
|
7
|
-
# if
|
8
|
-
# h.content_tag :span,
|
7
|
+
# if object.present?
|
8
|
+
# h.content_tag :span, object.to_s
|
9
9
|
# else
|
10
10
|
# h.content_tag :span, 'None'
|
11
11
|
# end
|
@@ -3,20 +3,12 @@ module Padrino
|
|
3
3
|
module Decorator
|
4
4
|
class Base < SimpleDelegator
|
5
5
|
|
6
|
-
|
6
|
+
attr_reader :object
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
end
|
13
|
-
|
14
|
-
end # ClassMethods
|
15
|
-
|
16
|
-
def initialize(model, context)
|
17
|
-
@model = model
|
18
|
-
@context = context
|
19
|
-
super(model)
|
8
|
+
def initialize(object, options = {})
|
9
|
+
@object = object
|
10
|
+
@context = options[:context]
|
11
|
+
super(object)
|
20
12
|
end
|
21
13
|
|
22
14
|
def to_model
|
@@ -42,8 +34,12 @@ module Padrino
|
|
42
34
|
|
43
35
|
private
|
44
36
|
|
45
|
-
def
|
46
|
-
|
37
|
+
def self.context(context)
|
38
|
+
_helper_method_name = :h
|
39
|
+
define_method(_helper_method_name) do
|
40
|
+
@context || context.to_s.camelize.constantize.new!
|
41
|
+
end
|
42
|
+
private _helper_method_name
|
47
43
|
end
|
48
44
|
|
49
45
|
end # Base
|
@@ -3,14 +3,19 @@ module Padrino
|
|
3
3
|
|
4
4
|
module DecorateHelpers
|
5
5
|
def decorate(object, options = {})
|
6
|
+
|
7
|
+
klass = options[:as]
|
8
|
+
|
6
9
|
if object.respond_to?(:first)
|
7
10
|
return [] if object.empty?
|
8
|
-
klass_name = "#{object.first.class
|
11
|
+
klass_name = "#{object.first.class}Decorator"
|
12
|
+
klass = klass_name.constantize if klass.nil?
|
13
|
+
decorator = object.map{|o| klass.new(o, context: self)}
|
9
14
|
else
|
10
15
|
klass_name = "#{object.class}Decorator"
|
16
|
+
klass = klass_name.constantize if klass.nil?
|
17
|
+
decorator = klass.new(object, context: self)
|
11
18
|
end
|
12
|
-
klass = options.fetch(:as) { klass_name.constantize }
|
13
|
-
decorator = klass.new(object, self)
|
14
19
|
|
15
20
|
yield decorator if block_given?
|
16
21
|
decorator
|
@@ -16,8 +16,10 @@ module Padrino
|
|
16
16
|
class << self
|
17
17
|
def registered(app)
|
18
18
|
app.helpers Padrino::Decorator::DecorateHelpers
|
19
|
-
app.load_paths
|
20
|
-
|
19
|
+
app.load_paths << File.join(app.settings.root, 'decorators')
|
20
|
+
decorators_path = File.join(app.settings.root, 'decorators/**/*.rb')
|
21
|
+
Padrino.dependency_paths << decorators_path
|
22
|
+
Padrino.require_dependencies(decorators_path)
|
21
23
|
end
|
22
24
|
alias :included :registered
|
23
25
|
end
|
data/padrino-decorator.gemspec
CHANGED
@@ -22,8 +22,7 @@ Gem::Specification.new do |s|
|
|
22
22
|
s.require_paths = ["lib"]
|
23
23
|
s.rdoc_options = ["--charset=UTF-8"]
|
24
24
|
|
25
|
-
s.add_dependency
|
26
|
-
s.add_dependency "padrino-
|
27
|
-
|
28
|
-
s.add_development_dependency "padrino-core", "~> 0.11.0"
|
25
|
+
s.add_dependency("padrino-core", "> 0.11")
|
26
|
+
s.add_dependency "padrino-gen", "> 0.11.0"
|
27
|
+
s.add_dependency "padrino-helpers", "> 0.11.0"
|
29
28
|
end
|
data/test/helpers/helper.rb
CHANGED
data/test/test_base.rb
CHANGED
@@ -3,16 +3,15 @@ require File.expand_path("#{File.dirname(__FILE__)}/helpers/helper")
|
|
3
3
|
describe Padrino::Decorator::Base do
|
4
4
|
let(:decorator_class) { Class.new(Padrino::Decorator::Base) }
|
5
5
|
let(:model) { Object.new }
|
6
|
-
let(:context) { Object.new }
|
7
6
|
|
8
7
|
describe "#initialize" do
|
9
|
-
subject { decorator_class.new(model
|
8
|
+
subject { decorator_class.new(model) }
|
10
9
|
|
11
10
|
it 'Reports its type as if it was the original object' do
|
12
11
|
subject.class.must_equal model.class
|
13
|
-
subject.must_be_kind_of model.class
|
14
|
-
assert subject.kind_of?(model.class),
|
15
|
-
assert subject.is_a?(model.class),
|
12
|
+
subject.must_be_kind_of model.class
|
13
|
+
assert subject.kind_of?(model.class), "The subject class (#{subject.class}) is not kind_of? the model class (#{model.class})."
|
14
|
+
assert subject.is_a?(model.class), "The subject class (#{subject.class}) is not is_a? the model class (#{model.class})."
|
16
15
|
assert subject.instance_of?(model.class), "The subject class (#{subject.class}) is not an instance_of? the model class (#{model.class})."
|
17
16
|
end
|
18
17
|
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
require File.expand_path("#{File.dirname(__FILE__)}/helpers/helper")
|
2
2
|
|
3
3
|
describe Padrino::Decorator::DecorateHelpers do
|
4
|
+
|
4
5
|
class User
|
5
6
|
attr_accessor :username, :full_name
|
6
7
|
def initialize(attributes = {})
|
@@ -9,31 +10,27 @@ describe Padrino::Decorator::DecorateHelpers do
|
|
9
10
|
end
|
10
11
|
end
|
11
12
|
end
|
13
|
+
|
12
14
|
class UserDecorator < Padrino::Decorator::Base
|
13
|
-
decorate :user
|
14
15
|
def name
|
15
|
-
|
16
|
-
end
|
17
|
-
end
|
18
|
-
class UsersDecorator < Padrino::Decorator::Base
|
19
|
-
decorate :users
|
20
|
-
def name_list
|
21
|
-
users.map{|u| h.decorate(u).name}
|
16
|
+
object.full_name.present? ? object.full_name : object.username
|
22
17
|
end
|
23
18
|
end
|
24
19
|
|
25
20
|
include Padrino::Decorator::DecorateHelpers
|
26
21
|
|
27
22
|
describe '.decorate' do
|
23
|
+
let(:dorothy) { User.new(username: 'Dorothy', full_name: 'Dorothy Gale') }
|
24
|
+
let(:toto) { User.new(username: 'Toto') }
|
25
|
+
|
28
26
|
it 'Possible to decorate the single object' do
|
29
|
-
|
30
|
-
assert_equal decorate(user).name, user.full_name
|
27
|
+
assert_equal decorate(dorothy).name, dorothy.full_name
|
31
28
|
end
|
32
29
|
it 'Possible to decorate the collections' do
|
33
30
|
users = []
|
34
|
-
users <<
|
35
|
-
users <<
|
36
|
-
assert_equal decorate(users).
|
31
|
+
users << dorothy
|
32
|
+
users << toto
|
33
|
+
assert_equal decorate(users).first.name, dorothy.full_name
|
37
34
|
end
|
38
35
|
end
|
39
36
|
|
metadata
CHANGED
@@ -1,55 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: padrino-decorator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Takeshi Yabe
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-01-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name: padrino-
|
14
|
+
name: padrino-core
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.11
|
19
|
+
version: '0.11'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.11
|
26
|
+
version: '0.11'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name: padrino-
|
28
|
+
name: padrino-gen
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: 0.11.0
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 0.11.0
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name: padrino-
|
42
|
+
name: padrino-helpers
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">"
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: 0.11.0
|
48
|
-
type: :
|
48
|
+
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 0.11.0
|
55
55
|
description: Object-Oriented layer of presentation logic to your Padrino apps.
|
@@ -59,10 +59,10 @@ executables: []
|
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
|
-
- .document
|
63
|
-
- .gitignore
|
64
|
-
- .travis.yml
|
65
|
-
- .yardopts
|
62
|
+
- ".document"
|
63
|
+
- ".gitignore"
|
64
|
+
- ".travis.yml"
|
65
|
+
- ".yardopts"
|
66
66
|
- Gemfile
|
67
67
|
- LICENSE
|
68
68
|
- README.md
|
@@ -91,22 +91,22 @@ licenses: []
|
|
91
91
|
metadata: {}
|
92
92
|
post_install_message:
|
93
93
|
rdoc_options:
|
94
|
-
- --charset=UTF-8
|
94
|
+
- "--charset=UTF-8"
|
95
95
|
require_paths:
|
96
96
|
- lib
|
97
97
|
required_ruby_version: !ruby/object:Gem::Requirement
|
98
98
|
requirements:
|
99
|
-
- -
|
99
|
+
- - ">="
|
100
100
|
- !ruby/object:Gem::Version
|
101
101
|
version: '0'
|
102
102
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
103
|
requirements:
|
104
|
-
- -
|
104
|
+
- - ">="
|
105
105
|
- !ruby/object:Gem::Version
|
106
106
|
version: 1.3.6
|
107
107
|
requirements: []
|
108
108
|
rubyforge_project: padrino-decorator
|
109
|
-
rubygems_version: 2.0
|
109
|
+
rubygems_version: 2.2.0
|
110
110
|
signing_key:
|
111
111
|
specification_version: 4
|
112
112
|
summary: View models for padrino
|