render_with_view 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 16c75b8652b4d928f2c02d3c22fc6b0557f05720
4
+ data.tar.gz: 946a91bd39ffc868a96eadd82a7277251e1113ba
5
+ SHA512:
6
+ metadata.gz: d3b8e0a484f0baf8578a86314c58ccc504660c1a98ac103e18f6998b87ab5af963b5279727dcbfa9da60f4f0b21f6dab1c35c1def29041c96848c5e120fc96ec
7
+ data.tar.gz: 79aa31678bcfff82a4cd358dfdeec6ad3f918389b69e554c5d333c1d9eb0f2d87fa6f7ff1a8b7fbb401218c93261fb48099561b6ab3fac715647a4e1b6e4a957
data/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ The MIT License (MIT)
2
+ Copyright (c) 2016 Mikkel Malmberg
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # render_with_view
2
+
3
+ Be explicit about the things you send from your Rails controller to the view.
4
+
5
+ ### Example
6
+
7
+ `app/controllers/application_controller.rb`:
8
+
9
+ ```ruby
10
+ class ApplicationController
11
+ include RenderWithView
12
+ end
13
+ ```
14
+
15
+ `app/controllers/home_controller.rb`:
16
+
17
+ ```ruby
18
+ class HomeController < ApplicationController
19
+ def index
20
+ render_with_view posts: Post.all
21
+ end
22
+ end
23
+ ```
24
+
25
+ `app/views/home/index.html.erb`:
26
+
27
+ ```erb
28
+ <ul>
29
+ <% view.posts.each do |post| %>
30
+ <li><%= link_to post.title, post %></li>
31
+ <% end %>
32
+ </ul>
33
+ ```
34
+
35
+ ### Why not just use instance variables?
36
+
37
+ They feel like _magic_. Instead I like how this forces me to be explicit in what I send along to my templates. It's like a small step towards having a presenter/view layer (or whatever) but not going further than just adding the convention of using a single variable.
38
+
39
+ ### What's `view`?
40
+
41
+ An object with reader methods for every key in the hash you gave it. A [HalfOpenStruct](https://gist.github.com/henrik/19c68b2a41ab4d098ce8) in a way.
42
+
43
+ ## Installation
44
+
45
+ Add `render_with_view` to your Gemfile:
46
+
47
+ ```ruby
48
+ gem 'render_with_view'
49
+ ```
50
+
51
+ Include it in your `ApplicationController`:
52
+
53
+ ```ruby
54
+ class ApplicationController
55
+ include RenderWithView
56
+ end
57
+ ```
58
+
59
+ # License
60
+
61
+ MIT
@@ -0,0 +1,42 @@
1
+ module RenderWithView
2
+ # Like OpenStruct but doesn't let you read a non-assigned value (raises
3
+ # instead of returning nil). This avoids issues where you read the wrong
4
+ # value due to a typo and don't notice.
5
+ #
6
+ # Borrowed from @henrik,
7
+ # https://gist.github.com/henrik/19c68b2a41ab4d098ce8
8
+
9
+ class HalfOpenStruct
10
+ def initialize(hash = {})
11
+ @hash = hash
12
+ end
13
+
14
+ def include?(name)
15
+ @hash.include?(name)
16
+ end
17
+
18
+ def fetch(name, fallback)
19
+ @hash.fetch(name, fallback)
20
+ end
21
+
22
+ def method_missing(name, *args)
23
+ if name.to_s.end_with?("=")
24
+ write(name.to_s.chop.to_sym, *args)
25
+ elsif args.length == 0
26
+ read_or_raise(name)
27
+ else
28
+ raise NoMethodError
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def write(name, value)
35
+ @hash[name] = value
36
+ end
37
+
38
+ def read_or_raise(name)
39
+ @hash.fetch(name) { raise "Unknown key: #{name}" }
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,31 @@
1
+ RSpec::Matchers.define :set_view_local do |key, expected = nil|
2
+ match do |block|
3
+ block.call if block && block.is_a?(Proc)
4
+
5
+ value = assigns('__view__').send(key.to_sym) rescue nil
6
+
7
+ return !!value unless expected
8
+
9
+ expect(expected).to eq(value)
10
+ end
11
+
12
+ supports_block_expectations
13
+
14
+ description do
15
+ msg = "set view local"
16
+ msg += " to equal #{expected.inspect}" if expected
17
+ msg
18
+ end
19
+
20
+ failure_message do
21
+ msg = "expected view locals to have key #{key.inspect}"
22
+ msg += " to equal #{expected.inspect}" if expected
23
+ msg
24
+ end
25
+
26
+ failure_message_when_negated do
27
+ msg = "expected view locals to not have key #{key.inspect}"
28
+ msg += " to not equal #{expected.inspect}" if expected
29
+ msg
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module RenderWithView
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,20 @@
1
+ require 'render_with_view/half_open_struct'
2
+
3
+ module RenderWithView
4
+ class View < HalfOpenStruct; end
5
+
6
+ def self.included kls
7
+ kls.class_eval do
8
+ def render_with_view *args
9
+ tmpl = args.length == 2 ? args.shift : action_name
10
+ locals = args.shift
11
+
12
+ # save to ivar for testing purposes
13
+ @__view__ = View.new(locals)
14
+
15
+ render tmpl, locals: { view: @__view__ }
16
+ end
17
+ end
18
+ end
19
+ end
20
+
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: render_with_view
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mikkel Malmberg
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - mikkel@brnbw.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE
21
+ - README.md
22
+ - lib/render_with_view.rb
23
+ - lib/render_with_view/half_open_struct.rb
24
+ - lib/render_with_view/rspec_matcher.rb
25
+ - lib/render_with_view/version.rb
26
+ homepage: https://github.com/mikker/render_with_view
27
+ licenses:
28
+ - MIT
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.5.1
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: Be explicit about what you send to your Rails views
50
+ test_files: []