render_with_view 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/dependabot.yml +8 -0
- data/.github/workflows/test.yml +17 -0
- data/CHANGELOG.md +9 -0
- data/Gemfile +1 -1
- data/Rakefile +6 -4
- data/lib/render_with_view/rspec_matcher.rb +3 -3
- data/lib/render_with_view/version.rb +1 -1
- data/lib/render_with_view.rb +26 -13
- data/render_with_view.gemspec +9 -9
- data/spec/render_with_view/half_open_struct_spec.rb +8 -8
- data/spec/render_with_view/rspec_matcher_spec.rb +9 -9
- data/spec/render_with_view_spec.rb +15 -17
- data/spec/spec_helper.rb +5 -2
- metadata +10 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8aa151fcf7f21842f19aac1b46c4e94fa525a1ba00beff1f4034042153100faf
|
4
|
+
data.tar.gz: ae2a1259739e9bc8c2de8237c0631e2385ec7a3cbef82e4afbea99016e04345c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e864e6c20b78d8e6c181a52cd1b1d6daa0d5dd12df50a44422f6757f7be5d54df4598764d6dab9a3adf9ec49c22b8a22181f67fe1a4e71881a3c97e683b8845c
|
7
|
+
data.tar.gz: 2044c39043b71feb3a41239ac802bcdd21c73a9b100c1f02180a84b067047cd1644c9f1834fa91c42e2244afc0719a57c94a0cb0cd1843132d98949cd69a7935
|
@@ -0,0 +1,17 @@
|
|
1
|
+
name: Tests
|
2
|
+
on: [push]
|
3
|
+
jobs:
|
4
|
+
test:
|
5
|
+
strategy:
|
6
|
+
fail-fast: false
|
7
|
+
matrix:
|
8
|
+
os: [ubuntu-latest]
|
9
|
+
ruby: ["3.0", "3.1", "3.2"]
|
10
|
+
runs-on: ${{ matrix.os }}
|
11
|
+
steps:
|
12
|
+
- uses: actions/checkout@v3
|
13
|
+
- uses: ruby/setup-ruby@v1
|
14
|
+
with:
|
15
|
+
ruby-version: ${{ matrix.ruby }}
|
16
|
+
bundler-cache: true
|
17
|
+
- run: bundle exec rake
|
data/CHANGELOG.md
ADDED
data/Gemfile
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
source
|
1
|
+
source "https://rubygems.org"
|
2
2
|
gemspec
|
data/Rakefile
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
-
require
|
1
|
+
require "bundler"
|
2
|
+
|
2
3
|
Bundler::GemHelper.install_tasks
|
3
4
|
|
4
|
-
require
|
5
|
+
require "rspec/core/rake_task"
|
6
|
+
|
5
7
|
RSpec::Core::RakeTask.new(:spec) do |spec|
|
6
|
-
spec.pattern =
|
8
|
+
spec.pattern = "spec/**/*_spec.rb"
|
7
9
|
end
|
8
|
-
task :default => :spec
|
9
10
|
|
11
|
+
task(:default => :spec)
|
@@ -1,12 +1,12 @@
|
|
1
|
-
RSpec::Matchers.define
|
1
|
+
RSpec::Matchers.define(:set_view_local) do |key, expected = nil|
|
2
2
|
match do |block|
|
3
3
|
block.call if block && block.is_a?(Proc)
|
4
4
|
|
5
|
-
value = assigns(
|
5
|
+
value = assigns("__view__").send(key.to_sym) rescue nil
|
6
6
|
|
7
7
|
return !!value unless expected
|
8
8
|
|
9
|
-
expect(expected).to
|
9
|
+
expect(expected).to(eq(value))
|
10
10
|
end
|
11
11
|
|
12
12
|
supports_block_expectations
|
data/lib/render_with_view.rb
CHANGED
@@ -1,23 +1,36 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "active_support/concern"
|
2
|
+
require "render_with_view/version"
|
3
|
+
require "render_with_view/half_open_struct"
|
3
4
|
|
4
5
|
module RenderWithView
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
5
8
|
class View < HalfOpenStruct
|
6
9
|
end
|
7
10
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
11
|
+
included do
|
12
|
+
helper_method :view
|
13
|
+
|
14
|
+
def view
|
15
|
+
raise ViewNotDefinedError, "View not defined" if !@__view__
|
16
|
+
|
17
|
+
@__view__
|
13
18
|
end
|
14
|
-
locals = args.shift
|
15
|
-
opts = args.shift || {}
|
16
19
|
|
17
|
-
|
18
|
-
|
20
|
+
def render_with_view(*args)
|
21
|
+
if !args.first.is_a?(Hash)
|
22
|
+
tmpl = args.shift
|
23
|
+
else
|
24
|
+
tmpl = action_name
|
25
|
+
end
|
26
|
+
|
27
|
+
view = args.shift
|
28
|
+
opts = args.shift || {}
|
19
29
|
|
20
|
-
|
30
|
+
# save to ivar for testing purposes
|
31
|
+
@__view__ = View.new(view)
|
32
|
+
|
33
|
+
render(tmpl, opts)
|
34
|
+
end
|
21
35
|
end
|
22
36
|
end
|
23
|
-
|
data/render_with_view.gemspec
CHANGED
@@ -2,18 +2,18 @@
|
|
2
2
|
require File.expand_path("../lib/render_with_view/version", __FILE__)
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
|
-
s.name =
|
6
|
-
s.summary =
|
5
|
+
s.name = "render_with_view"
|
6
|
+
s.summary = "Be explicit about what you send to your Rails views"
|
7
7
|
s.version = RenderWithView::VERSION
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
|
-
s.authors = [
|
10
|
-
s.email = [
|
11
|
-
s.homepage =
|
12
|
-
s.license =
|
9
|
+
s.authors = ["Mikkel Malmberg"]
|
10
|
+
s.email = ["mikkel@brnbw.com"]
|
11
|
+
s.homepage = "https://github.com/mikker/render_with_view"
|
12
|
+
s.license = "MIT"
|
13
13
|
|
14
|
-
s.add_dependency
|
15
|
-
s.add_development_dependency
|
14
|
+
s.add_dependency("rails")
|
15
|
+
s.add_development_dependency("rspec")
|
16
16
|
|
17
17
|
s.files = `git ls-files`.split("\n")
|
18
|
-
s.require_path
|
18
|
+
s.require_path = "lib"
|
19
19
|
end
|
@@ -1,13 +1,13 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
module RenderWithView
|
4
4
|
RSpec.describe HalfOpenStruct do
|
5
5
|
it "can be initialized with a hash, and values read and written" do
|
6
6
|
s = HalfOpenStruct.new(name: "Joe")
|
7
|
-
expect(s.name).to eq
|
7
|
+
expect(s.name).to eq("Joe")
|
8
8
|
|
9
9
|
s.age = 42
|
10
|
-
expect(s.age).to eq
|
10
|
+
expect(s.age).to eq(42)
|
11
11
|
end
|
12
12
|
|
13
13
|
it "raises trying to read an unknown value" do
|
@@ -16,7 +16,7 @@ module RenderWithView
|
|
16
16
|
expect { s.age }.to raise_error("Unknown key: age")
|
17
17
|
|
18
18
|
s.age = 42
|
19
|
-
expect(s.age).to eq
|
19
|
+
expect(s.age).to eq(42)
|
20
20
|
end
|
21
21
|
|
22
22
|
it "raises for unknown methods" do
|
@@ -28,18 +28,18 @@ module RenderWithView
|
|
28
28
|
# Considered an API like "s.age?" but that's ambiguous with undefined vs. falsy values.
|
29
29
|
it "lets you query for values being defined" do
|
30
30
|
s = HalfOpenStruct.new
|
31
|
-
expect(s.include?(:age)).to be
|
31
|
+
expect(s.include?(:age)).to be(false)
|
32
32
|
|
33
33
|
s.age = 42
|
34
|
-
expect(s.include?(:age)).to be
|
34
|
+
expect(s.include?(:age)).to be(true)
|
35
35
|
end
|
36
36
|
|
37
37
|
it "lets you fetch values with a fallback" do
|
38
38
|
s = HalfOpenStruct.new
|
39
|
-
expect(s.fetch(:age, :fallback)).to eq
|
39
|
+
expect(s.fetch(:age, :fallback)).to eq(:fallback)
|
40
40
|
|
41
41
|
s.age = 42
|
42
|
-
expect(s.fetch(:age, :fallback)).to eq
|
42
|
+
expect(s.fetch(:age, :fallback)).to eq(42)
|
43
43
|
end
|
44
44
|
end
|
45
45
|
end
|
@@ -1,21 +1,21 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "spec_helper"
|
2
|
+
require "render_with_view/rspec_matcher"
|
3
3
|
|
4
|
-
RSpec.describe
|
4
|
+
RSpec.describe "RSpec matcher" do
|
5
5
|
let(:ctrl) { FakeController.new }
|
6
|
-
subject { ctrl.render_with_view
|
6
|
+
subject { ctrl.render_with_view(:index, thing: 1) }
|
7
7
|
|
8
8
|
# short form
|
9
|
-
it { should
|
10
|
-
it { should_not
|
9
|
+
it { should(set_view_local(:thing)) }
|
10
|
+
it { should_not(set_view_local(:other_thing)) }
|
11
11
|
|
12
12
|
# long form
|
13
13
|
it "takes and matches arguments" do
|
14
|
-
expect(subject).to
|
15
|
-
expect(subject).to_not
|
14
|
+
expect(subject).to(set_view_local(:thing, 1))
|
15
|
+
expect(subject).to_not(set_view_local(:thing, 2))
|
16
16
|
end
|
17
17
|
|
18
|
-
def assigns
|
18
|
+
def assigns(key)
|
19
19
|
ctrl.instance_variable_get("@#{key}".to_sym)
|
20
20
|
end
|
21
21
|
end
|
@@ -1,50 +1,48 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
RSpec.describe RenderWithView do
|
4
4
|
|
5
5
|
describe "#render_with_view" do
|
6
6
|
it "calls render with template and assigned view" do
|
7
|
-
user = {
|
7
|
+
user = {id: 1}
|
8
8
|
ctrl = FakeController.new
|
9
9
|
|
10
|
-
ctrl.render_with_view
|
10
|
+
ctrl.render_with_view(:index, user: user)
|
11
11
|
|
12
12
|
tmpl, args = ctrl.calls.last
|
13
|
-
expect(tmpl).to
|
14
|
-
expect(args[:locals][:view].user).to eq(user)
|
13
|
+
expect(tmpl).to(eq(:index))
|
15
14
|
end
|
16
15
|
|
17
16
|
it "defaults to action_name template" do
|
18
|
-
user = {
|
17
|
+
user = {id: 1}
|
19
18
|
ctrl = FakeController.new
|
20
19
|
|
21
|
-
ctrl.render_with_view
|
20
|
+
ctrl.render_with_view(user: user)
|
22
21
|
|
23
22
|
tmpl, * = ctrl.calls.last
|
24
|
-
expect(tmpl).to
|
23
|
+
expect(tmpl).to(eq(:new))
|
25
24
|
end
|
26
25
|
|
27
26
|
it "saves to an instance var behind the scenes" do
|
28
|
-
user = {
|
27
|
+
user = {id: 1}
|
29
28
|
ctrl = FakeController.new
|
30
29
|
|
31
|
-
ctrl.render_with_view
|
30
|
+
ctrl.render_with_view(:index, user: user)
|
32
31
|
|
33
32
|
ivar = ctrl.instance_variable_get(:@__view__)
|
34
|
-
expect(ivar).to
|
35
|
-
expect(ivar.user).to
|
33
|
+
expect(ivar).to(be_a(RenderWithView::View))
|
34
|
+
expect(ivar.user).to(eq(user))
|
36
35
|
end
|
37
36
|
|
38
37
|
it "includes options" do
|
39
|
-
user = {
|
40
|
-
opts = {
|
38
|
+
user = {id: 1}
|
39
|
+
opts = {status: 401}
|
41
40
|
ctrl = FakeController.new
|
42
41
|
|
43
42
|
ctrl.render_with_view(:edit, {user: user}, opts)
|
44
43
|
tmpl, opts = ctrl.calls.last
|
45
|
-
expect(tmpl).to
|
46
|
-
expect(opts[:status]).to
|
44
|
+
expect(tmpl).to(eq(:edit))
|
45
|
+
expect(opts[:status]).to(eq(401))
|
47
46
|
end
|
48
47
|
end
|
49
|
-
|
50
48
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: render_with_view
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mikkel Malmberg
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-08-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -38,14 +38,17 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
-
description:
|
41
|
+
description:
|
42
42
|
email:
|
43
43
|
- mikkel@brnbw.com
|
44
44
|
executables: []
|
45
45
|
extensions: []
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
|
+
- ".github/dependabot.yml"
|
49
|
+
- ".github/workflows/test.yml"
|
48
50
|
- ".gitignore"
|
51
|
+
- CHANGELOG.md
|
49
52
|
- Gemfile
|
50
53
|
- LICENSE
|
51
54
|
- README.md
|
@@ -63,7 +66,7 @@ homepage: https://github.com/mikker/render_with_view
|
|
63
66
|
licenses:
|
64
67
|
- MIT
|
65
68
|
metadata: {}
|
66
|
-
post_install_message:
|
69
|
+
post_install_message:
|
67
70
|
rdoc_options: []
|
68
71
|
require_paths:
|
69
72
|
- lib
|
@@ -78,8 +81,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
78
81
|
- !ruby/object:Gem::Version
|
79
82
|
version: '0'
|
80
83
|
requirements: []
|
81
|
-
rubygems_version: 3.
|
82
|
-
signing_key:
|
84
|
+
rubygems_version: 3.4.19
|
85
|
+
signing_key:
|
83
86
|
specification_version: 4
|
84
87
|
summary: Be explicit about what you send to your Rails views
|
85
88
|
test_files: []
|