simple-presenters 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.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +37 -0
- data/Rakefile +44 -0
- data/lib/simple-presenters/version.rb +3 -0
- data/lib/simple-presenters.rb +53 -0
- data/lib/tasks/mantra-presenters_tasks.rake +4 -0
- metadata +84 -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
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
= simple-presenters
|
|
2
|
+
|
|
3
|
+
http://fury-badge.herokuapp.com/rb/simple-presenters.png
|
|
4
|
+
|
|
5
|
+
{<img src="https://travis-ci.org/aosmith/simple-presenters.png?branch=master" />}[https://travis-ci.org/aosmith/simple-presenters?branch=master]
|
|
6
|
+
|
|
7
|
+
== Usage
|
|
8
|
+
|
|
9
|
+
<b>Include SimplePresenters::Presenter in your presenter:</b>
|
|
10
|
+
|
|
11
|
+
module UserPresenter
|
|
12
|
+
include SimplePresenters::Presenter
|
|
13
|
+
|
|
14
|
+
def default
|
|
15
|
+
[:name, :email]
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
<b>Include your presenter in your model:</b>
|
|
20
|
+
|
|
21
|
+
class User < ActiveRecord::Base
|
|
22
|
+
include UserPresenter
|
|
23
|
+
...
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
== Protecting Attributes
|
|
28
|
+
|
|
29
|
+
<b>Add a class method on your model called filtered_parameters:</b>
|
|
30
|
+
|
|
31
|
+
class User < ActiveRecord::Base
|
|
32
|
+
include UserPresenter
|
|
33
|
+
|
|
34
|
+
def self.filtered_parameters
|
|
35
|
+
[:authentication_token, :password_digest, :salt]
|
|
36
|
+
end
|
|
37
|
+
end
|
data/Rakefile
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
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 = 'SimplePresenters'
|
|
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
|
+
|
|
28
|
+
|
|
29
|
+
require 'rspec/core/rake_task'
|
|
30
|
+
|
|
31
|
+
RSpec::Core::RakeTask.new(:spec)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
#require 'rake/testtask'
|
|
35
|
+
|
|
36
|
+
#Rake::TestTask.new(:test) do |t|
|
|
37
|
+
# t.libs << 'lib'
|
|
38
|
+
# t.libs << 'test'
|
|
39
|
+
# t.pattern = 'spec/**/*_test.rb'
|
|
40
|
+
# t.verbose = false
|
|
41
|
+
#end
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
task :default => :spec
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
module SimplePresenters
|
|
2
|
+
module Presenter
|
|
3
|
+
|
|
4
|
+
def default
|
|
5
|
+
all
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def all
|
|
9
|
+
attributes.symbolize_keys.keys - filtered_params
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def present_as format
|
|
13
|
+
@format = format
|
|
14
|
+
attributes = self.send(@format)
|
|
15
|
+
attributes.inject({}) do |object_hash, attribute|
|
|
16
|
+
if self.respond_to? attribute.to_sym
|
|
17
|
+
value = self.public_send(attribute)
|
|
18
|
+
object_hash[attribute.to_s.gsub(/[?|!]/, '').to_sym] = process_value(value)
|
|
19
|
+
end
|
|
20
|
+
object_hash
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def process_value value
|
|
27
|
+
value = if defined?(ActiveRecord) and value.is_a? ActiveRecord::Base
|
|
28
|
+
if value.respond_to? @format.to_sym and value.respond_to? :present_as
|
|
29
|
+
value.present_as format.to_sym
|
|
30
|
+
elsif value.respond_to? :default and value.respond_to? :present_as
|
|
31
|
+
value.present_as :default
|
|
32
|
+
else
|
|
33
|
+
value.class.send(:include, Mantra::Presenters::Presenter)
|
|
34
|
+
value.present_as :default
|
|
35
|
+
end
|
|
36
|
+
else
|
|
37
|
+
value
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def filtered_params
|
|
42
|
+
params = []
|
|
43
|
+
# unless Rails.nil?
|
|
44
|
+
# params += Rails.application.config.filter_parameters
|
|
45
|
+
# end
|
|
46
|
+
if self.class.respond_to? :filtered_parameters
|
|
47
|
+
params += self.class.filtered_parameters
|
|
48
|
+
end
|
|
49
|
+
params
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
end
|
|
53
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: simple-presenters
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Alex Smith
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2013-06-13 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.13
|
|
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.13
|
|
30
|
+
- !ruby/object:Gem::Dependency
|
|
31
|
+
name: rspec
|
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
|
33
|
+
none: false
|
|
34
|
+
requirements:
|
|
35
|
+
- - ~>
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: 2.13.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: 2.13.0
|
|
46
|
+
description: TLDR
|
|
47
|
+
email:
|
|
48
|
+
- aosmith@gmail.com
|
|
49
|
+
executables: []
|
|
50
|
+
extensions: []
|
|
51
|
+
extra_rdoc_files: []
|
|
52
|
+
files:
|
|
53
|
+
- lib/simple-presenters/version.rb
|
|
54
|
+
- lib/simple-presenters.rb
|
|
55
|
+
- lib/tasks/mantra-presenters_tasks.rake
|
|
56
|
+
- MIT-LICENSE
|
|
57
|
+
- Rakefile
|
|
58
|
+
- README.rdoc
|
|
59
|
+
homepage: http://alexsmith.io
|
|
60
|
+
licenses: []
|
|
61
|
+
post_install_message:
|
|
62
|
+
rdoc_options: []
|
|
63
|
+
require_paths:
|
|
64
|
+
- lib
|
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
66
|
+
none: false
|
|
67
|
+
requirements:
|
|
68
|
+
- - ! '>='
|
|
69
|
+
- !ruby/object:Gem::Version
|
|
70
|
+
version: '0'
|
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
|
+
none: false
|
|
73
|
+
requirements:
|
|
74
|
+
- - ! '>='
|
|
75
|
+
- !ruby/object:Gem::Version
|
|
76
|
+
version: '0'
|
|
77
|
+
requirements: []
|
|
78
|
+
rubyforge_project:
|
|
79
|
+
rubygems_version: 1.8.25
|
|
80
|
+
signing_key:
|
|
81
|
+
specification_version: 3
|
|
82
|
+
summary: A Simple Presenter Gem
|
|
83
|
+
test_files: []
|
|
84
|
+
has_rdoc:
|