rails_presenter 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +3 -0
- data/Rakefile +27 -0
- data/app/helpers/presenter_helper.rb +18 -0
- data/app/presenters/presenter.rb +180 -0
- data/app/views/shared/_show_with_attrs.html.erb +8 -0
- data/config/routes.rb +2 -0
- data/lib/rails_presenter.rb +4 -0
- data/lib/rails_presenter/engine.rb +4 -0
- data/lib/rails_presenter/version.rb +3 -0
- data/lib/tasks/rails_presenter_tasks.rake +4 -0
- metadata +126 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2013 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'RailsPresenter'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
Bundler::GemHelper.install_tasks
|
27
|
+
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module PresenterHelper
|
2
|
+
def present(object, template = self, &block)
|
3
|
+
if [Array, ActiveRecord::Relation].include? object.class
|
4
|
+
return object.map {|e| present(e)}
|
5
|
+
end
|
6
|
+
|
7
|
+
begin
|
8
|
+
presenter_class = "#{object.class}Presenter".constantize
|
9
|
+
rescue NameError
|
10
|
+
return object
|
11
|
+
end
|
12
|
+
|
13
|
+
presenter = presenter_class.new(object, template)
|
14
|
+
|
15
|
+
block.call(presenter) if block
|
16
|
+
presenter
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,180 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
|
3
|
+
class Presenter < SimpleDelegator
|
4
|
+
include PresenterHelper
|
5
|
+
|
6
|
+
@@nil_formatter = '----'
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def location(*args)
|
10
|
+
define_method(:self_location, ->(location=nil) do
|
11
|
+
location ||= args.map do |p|
|
12
|
+
p = p.to_s
|
13
|
+
if p.delete! "@"
|
14
|
+
public_send("get_#{p}")
|
15
|
+
else
|
16
|
+
p
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
super(location)
|
21
|
+
end)
|
22
|
+
end
|
23
|
+
|
24
|
+
def present(*args, &block)
|
25
|
+
module_name = "#{name}Associations"
|
26
|
+
|
27
|
+
unless const_defined? module_name
|
28
|
+
include const_set(module_name, Module.new)
|
29
|
+
end
|
30
|
+
|
31
|
+
associations_module = const_get(module_name)
|
32
|
+
|
33
|
+
block ||= proc { self }
|
34
|
+
|
35
|
+
associations_module.module_eval do
|
36
|
+
args.each do |assoc_name|
|
37
|
+
define_method(assoc_name) do
|
38
|
+
instance_variable_get("@#{assoc_name}") ||
|
39
|
+
begin
|
40
|
+
association = if super().is_a?(Array) && super().respond_to?(:scoped)
|
41
|
+
super().scoped
|
42
|
+
else
|
43
|
+
super()
|
44
|
+
end
|
45
|
+
instance_variable_set("@#{assoc_name}", present(association.instance_eval(&block)))
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
def format(*attrs)
|
54
|
+
formatter = attrs.pop.values.first
|
55
|
+
|
56
|
+
module_name = formatter.to_s.camelize
|
57
|
+
|
58
|
+
unless const_defined? module_name
|
59
|
+
include const_set(module_name, Module.new)
|
60
|
+
end
|
61
|
+
|
62
|
+
formatter_module = const_get(module_name)
|
63
|
+
|
64
|
+
formatter_module.module_eval do
|
65
|
+
attrs.each do |attr|
|
66
|
+
define_method(attr) do
|
67
|
+
h.public_send(formatter, super())
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def inherited(child_class)
|
74
|
+
child_class.instance_eval do
|
75
|
+
format_blank_attributes(*possible_blank_attributes) if based_on_active_record?
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def format_blank_attributes(*attrs)
|
80
|
+
attr_module = Module.new do
|
81
|
+
attrs.each do |attr|
|
82
|
+
define_method(attr) do
|
83
|
+
return nil_formatter if super().blank?
|
84
|
+
super()
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
include const_set("BlankAttributes", attr_module)
|
90
|
+
end
|
91
|
+
|
92
|
+
private
|
93
|
+
|
94
|
+
def base_class
|
95
|
+
@base_class ||= to_s.chomp('Presenter').constantize
|
96
|
+
end
|
97
|
+
|
98
|
+
def based_on_active_record?
|
99
|
+
base_class.ancestors.include?(ActiveRecord::Base)
|
100
|
+
end
|
101
|
+
|
102
|
+
def possible_blank_attributes
|
103
|
+
base_class.column_names.reject do |attr|
|
104
|
+
['id', 'created_at', 'updated_at'].include?(attr) ||
|
105
|
+
/_id\z/.match(attr) ||
|
106
|
+
base_class.validators_on(attr).any? {|v| !(v.options[:allow_nil] || v.options[:allow_blank])}
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def initialize(base_object, template)
|
112
|
+
@template = template
|
113
|
+
super(base_object)
|
114
|
+
end
|
115
|
+
|
116
|
+
def present(object)
|
117
|
+
super(object, h)
|
118
|
+
end
|
119
|
+
|
120
|
+
def h
|
121
|
+
@template
|
122
|
+
end
|
123
|
+
|
124
|
+
def o
|
125
|
+
__getobj__
|
126
|
+
end
|
127
|
+
|
128
|
+
def to_s
|
129
|
+
respond_to?(:name) ? name : super
|
130
|
+
end
|
131
|
+
|
132
|
+
def with_attrs(*attrs)
|
133
|
+
attrs_hash = attrs.reduce({}) do |hash, attr|
|
134
|
+
if attr.is_a? Array
|
135
|
+
hash[attr.first]= attr.last.call(self)
|
136
|
+
else
|
137
|
+
hash[attr]= public_send(attr)
|
138
|
+
end
|
139
|
+
hash
|
140
|
+
end
|
141
|
+
|
142
|
+
h.render partial: 'shared/show_with_attrs', locals: {attrs_hash: attrs_hash}
|
143
|
+
end
|
144
|
+
|
145
|
+
def self_location(location = o)
|
146
|
+
h.polymorphic_path location
|
147
|
+
end
|
148
|
+
|
149
|
+
def link_to_self(options={})
|
150
|
+
text = options[:text] || self.to_s
|
151
|
+
path = options[:path] || self_location
|
152
|
+
h.link_to text, path
|
153
|
+
end
|
154
|
+
|
155
|
+
def nil_formatter
|
156
|
+
@@nil_formatter
|
157
|
+
end
|
158
|
+
|
159
|
+
def method_missing(method_name, *args)
|
160
|
+
case method_name.to_s
|
161
|
+
when /^h_(.*)$/
|
162
|
+
get_iv_from_view($1)
|
163
|
+
when /^get_(.*)$/
|
164
|
+
return o if o.is_a? $1.camelize.constantize
|
165
|
+
get_iv_from_view($1) || o.public_send($1)
|
166
|
+
else
|
167
|
+
super
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
private
|
172
|
+
def base_object_name
|
173
|
+
o.class.to_s.underscore
|
174
|
+
end
|
175
|
+
|
176
|
+
def get_iv_from_view(iv_name)
|
177
|
+
h.instance_variable_get("@#{iv_name}")
|
178
|
+
end
|
179
|
+
|
180
|
+
end
|
data/config/routes.rb
ADDED
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_presenter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Diego Mónaco
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-02 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.2.11
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.2.11
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: sqlite3
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec-rails
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: capybara
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Presenters for Rails applications
|
79
|
+
email:
|
80
|
+
- dfmonaco@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- app/presenters/presenter.rb
|
86
|
+
- app/helpers/presenter_helper.rb
|
87
|
+
- app/views/shared/_show_with_attrs.html.erb
|
88
|
+
- config/routes.rb
|
89
|
+
- lib/tasks/rails_presenter_tasks.rake
|
90
|
+
- lib/rails_presenter.rb
|
91
|
+
- lib/rails_presenter/engine.rb
|
92
|
+
- lib/rails_presenter/version.rb
|
93
|
+
- MIT-LICENSE
|
94
|
+
- Rakefile
|
95
|
+
- README.rdoc
|
96
|
+
homepage: https://github.com/dfmonaco/rails_presenter
|
97
|
+
licenses: []
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
segments:
|
109
|
+
- 0
|
110
|
+
hash: 610965391
|
111
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
|
+
none: false
|
113
|
+
requirements:
|
114
|
+
- - ! '>='
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
segments:
|
118
|
+
- 0
|
119
|
+
hash: 610965391
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 1.8.25
|
123
|
+
signing_key:
|
124
|
+
specification_version: 3
|
125
|
+
summary: Presenters for Rails applications
|
126
|
+
test_files: []
|