frosting 0.0.5 → 0.0.6

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 CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- ZTBjNGMzM2Q2Y2ViOWM4ZDIyOWE3ZWJhYzMwMGM2NjIwYjIxMzNkMA==
5
- data.tar.gz: !binary |-
6
- ZTE1MGNhNmFhOTgwMzhhMTE2YTk3MGUxNDMyYmU3Y2JiYTg5MGUzMw==
2
+ SHA1:
3
+ metadata.gz: a3f915322b680a3177a849297f6bf97406e1868a
4
+ data.tar.gz: a51935fa917ae9d576dda07dc2d52a07f35f0b3f
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- NThlZmY5OTNmOWI2ODA1Njk3Njc4YmUwZTMwOTFmYTYxMjYxZDAwMmIyMjQ2
10
- N2FmMzdjZjgxODIxNzAyMTBhNTI1MGI0ZjhjMDk4NjE5NDljMzc3NGE5MTU2
11
- YjkxY2E2OWViY2I4M2EzNjc4NTQzZmJhOTg5ZTM2NDFjYWJjOTM=
12
- data.tar.gz: !binary |-
13
- M2I4NTM2NjgzZDQ2ZDcwNDkxNTBjN2U5N2MzNGE3MjAzMTA3NmM1YTYxZGU4
14
- MGNkZDE5NDE3MzU2NDE2MzEzODFjOWMwMTFjYmY2Y2U3M2E5N2I4N2IyYmM0
15
- ZTI1NWRmZjdjYjAyMzM5YmM2ZTU0Y2NhMDQ5NmU1NDBjODk3OWE=
6
+ metadata.gz: 8609a7b47ffaa1acc0ed0b7e721e4dbf0de7f83887db66060f00b44c6352099fa7f4697563e7bc8cc95fae4f1c65462d12fe73d6f6540c6416bdfcfd7d1dae35
7
+ data.tar.gz: c616b450eb926e2955db63272f40395781dc3f3659e45cb00ae07ce9f0d0d5166685921423e37f3d0df476cbc87a14b42e5c89899279fbca7fa611e498ad7803
data/README.md CHANGED
@@ -32,4 +32,15 @@ end
32
32
 
33
33
  You defined `#old?` in your model because it's not a presentation concern. Good job.
34
34
 
35
- `Frosting::BasePresenter` delegates to the resource you're presenting, and it also has access to the view context. It delegates `link_to` and `content_tag` by default. You should probably make your own base presenter that inherits from frosting's base. It's your life, and you should do what you want to.
35
+ `Frosting::BasePresenter` delegates to the resource you're presenting, and it also has access to the view context. It doesn't delegate anything by default, but you can delegate things like `link_to` and `content_tag` if it makes your life easier. You should probably make your own base presenter that inherits from frosting's base. It's your life, and you should do what you want to.
36
+
37
+ You can also call `present_collection @posts` should you be dealing with a collection of posts and want them all to be presented.
38
+
39
+ ## About Foraker Labs
40
+
41
+ ![Foraker Logo](http://assets.foraker.com/attribution_logo.png)
42
+
43
+ Foraker Labs builds exciting web and mobile apps in Boulder, CO. Our work powers a wide variety of businesses with many different needs. We love open source software, and we're proud to contribute where we can. Interested to learn more? [Contact us today](https://www.foraker.com/contact-us).
44
+
45
+ This project is maintained by Foraker Labs. The names and logos of Foraker Labs are fully owned and copyright Foraker Design, LLC.
46
+
data/frosting.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'frosting'
3
- s.version = '0.0.5'
3
+ s.version = '0.0.6'
4
4
  s.date = '2014-02-04'
5
5
  s.summary = "Let's make presenters easy."
6
6
  s.description = "Adds some methods to your controllers and a base presenter. Get that presentation logic out of your models."
@@ -1,18 +1,49 @@
1
1
  require "active_support/core_ext/string/inflections"
2
+ require "active_support/core_ext/module/delegation"
2
3
 
3
4
  module Frosting
4
5
  class Repository
6
+ class PresenterMissingError < StandardError; end
7
+
5
8
  def self.present(resource, options = {})
6
- begin
7
- klass = options[:presenter] || "Presenters::#{resource.class.name}".constantize
8
- klass.new(resource, options[:context])
9
- rescue LoadError
10
- raise "No such presenter: #{klass}"
11
- end
9
+ klass = options.fetch(:presenter) { infer_presenter(resource) }
10
+ klass = procify(klass).call(resource)
11
+ klass.new(resource, options[:context])
12
+ rescue LoadError, NameError
13
+ raise PresenterMissingError.new("No such presenter: #{klass}")
12
14
  end
13
15
 
14
16
  def self.present_collection(collection, options = {})
15
- collection.map { |resource| present(resource, options) }
17
+ PresentedCollection.new(collection, options)
18
+ end
19
+
20
+ def self.infer_presenter(resource)
21
+ "Presenters::#{resource.class.name}".constantize
22
+ end
23
+ private_class_method :infer_presenter
24
+
25
+ def self.procify(arg)
26
+ arg.respond_to?(:call) ? arg : lambda { |_| arg }
27
+ end
28
+ private_class_method :procify
29
+ end
30
+
31
+ class PresentedCollection < SimpleDelegator
32
+ include Enumerable
33
+
34
+ delegate :each, to: :presented_collection
35
+
36
+ def initialize(collection, options)
37
+ @options = options
38
+ super(collection)
39
+ end
40
+
41
+ private
42
+
43
+ def presented_collection
44
+ __getobj__.map do |resource|
45
+ Repository.present(resource, @options)
46
+ end
16
47
  end
17
48
  end
18
49
  end
@@ -1,3 +1,3 @@
1
1
  module Frosting
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -35,20 +35,54 @@ module Frosting
35
35
  its(:context) { should eq context }
36
36
  end
37
37
 
38
+ context "specifying a presenter class via proc" do
39
+ subject do
40
+ described_class.present(resource, {
41
+ context: context,
42
+ presenter: ->(resource) { Presenters::Test::Alternative }
43
+ })
44
+ end
45
+
46
+ it { should be_instance_of(Presenters::Test::Alternative) }
47
+ its(:resource) { should eq resource }
48
+ its(:context) { should eq context }
49
+ end
50
+
38
51
  it "throws an exception when the resource has no presenter" do
39
52
  class Test::OtherResource ; end
40
53
  resource = Test::OtherResource.new
41
- expect { described_class.present(resource, context: context) }.to raise_error(NameError)
54
+ expect {
55
+ described_class.present(resource, context: context)
56
+ }.to raise_error(Frosting::Repository::PresenterMissingError)
42
57
  end
43
58
  end
44
59
 
45
60
  describe ".present_collection" do
46
- it "presents each item in the collection with options" do
47
- resource = double
48
- described_class.should_receive(:present)
61
+ class Collection < Array
62
+ def test_method
63
+ "cats"
64
+ end
65
+ end
66
+
67
+ let(:resource) { double }
68
+ let(:presented_resource) { double }
69
+ let(:presented_collection) do
70
+ described_class.present_collection(Collection.new.push(resource), {option: :val})
71
+ end
72
+
73
+ before do
74
+ allow(described_class).to receive(:present)
49
75
  .with(resource, {option: :val})
50
- described_class.present_collection([resource], {option: :val})
76
+ .and_return(presented_resource)
77
+ end
78
+
79
+ it "presents each item in the collection with options" do
80
+ expect(presented_collection.each.to_a).to eq [presented_resource]
81
+ end
82
+
83
+ it "still acts like the original collection" do
84
+ expect(presented_collection.test_method).to eq "cats"
51
85
  end
52
86
  end
53
87
  end
54
- end
88
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: frosting
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Eddy
@@ -15,42 +15,42 @@ dependencies:
15
15
  name: activesupport
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - ! '>='
18
+ - - ">="
19
19
  - !ruby/object:Gem::Version
20
20
  version: '0'
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - ! '>='
25
+ - - ">="
26
26
  - !ruby/object:Gem::Version
27
27
  version: '0'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: bundler
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - ~>
32
+ - - "~>"
33
33
  - !ruby/object:Gem::Version
34
34
  version: '1.3'
35
35
  type: :development
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - ~>
39
+ - - "~>"
40
40
  - !ruby/object:Gem::Version
41
41
  version: '1.3'
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: rspec
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
- - - ~>
46
+ - - "~>"
47
47
  - !ruby/object:Gem::Version
48
48
  version: '2.0'
49
49
  type: :development
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
- - - ~>
53
+ - - "~>"
54
54
  - !ruby/object:Gem::Version
55
55
  version: '2.0'
56
56
  description: Adds some methods to your controllers and a base presenter. Get that
@@ -82,17 +82,17 @@ require_paths:
82
82
  - lib
83
83
  required_ruby_version: !ruby/object:Gem::Requirement
84
84
  requirements:
85
- - - ! '>='
85
+ - - ">="
86
86
  - !ruby/object:Gem::Version
87
87
  version: '0'
88
88
  required_rubygems_version: !ruby/object:Gem::Requirement
89
89
  requirements:
90
- - - ! '>='
90
+ - - ">="
91
91
  - !ruby/object:Gem::Version
92
92
  version: '0'
93
93
  requirements: []
94
94
  rubyforge_project:
95
- rubygems_version: 2.2.1
95
+ rubygems_version: 2.5.1
96
96
  signing_key:
97
97
  specification_version: 4
98
98
  summary: Let's make presenters easy.