render_with_view 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/dependabot.yml +8 -0
- data/.github/workflows/test.yml +17 -0
- data/.gitignore +1 -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 +30 -20
- data/render_with_view.gemspec +10 -7
- 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 +39 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
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/.gitignore
CHANGED
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,26 +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
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
class View < HalfOpenStruct
|
9
|
+
end
|
10
|
+
|
11
|
+
included do
|
12
|
+
helper_method :view
|
13
|
+
|
14
|
+
def view
|
15
|
+
raise ViewNotDefinedError, "View not defined" if !@__view__
|
16
|
+
|
17
|
+
@__view__
|
18
|
+
end
|
19
|
+
|
20
|
+
def render_with_view(*args)
|
21
|
+
if !args.first.is_a?(Hash)
|
22
|
+
tmpl = args.shift
|
23
|
+
else
|
24
|
+
tmpl = action_name
|
22
25
|
end
|
26
|
+
|
27
|
+
view = args.shift
|
28
|
+
opts = args.shift || {}
|
29
|
+
|
30
|
+
# save to ivar for testing purposes
|
31
|
+
@__view__ = View.new(view)
|
32
|
+
|
33
|
+
render(tmpl, opts)
|
23
34
|
end
|
24
35
|
end
|
25
36
|
end
|
26
|
-
|
data/render_with_view.gemspec
CHANGED
@@ -2,15 +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
|
+
|
14
|
+
s.add_dependency("rails")
|
15
|
+
s.add_development_dependency("rspec")
|
13
16
|
|
14
17
|
s.files = `git ls-files`.split("\n")
|
15
|
-
s.require_path
|
18
|
+
s.require_path = "lib"
|
16
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[:
|
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,23 +1,54 @@
|
|
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:
|
12
|
-
dependencies:
|
13
|
-
|
11
|
+
date: 2023-08-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description:
|
14
42
|
email:
|
15
43
|
- mikkel@brnbw.com
|
16
44
|
executables: []
|
17
45
|
extensions: []
|
18
46
|
extra_rdoc_files: []
|
19
47
|
files:
|
48
|
+
- ".github/dependabot.yml"
|
49
|
+
- ".github/workflows/test.yml"
|
20
50
|
- ".gitignore"
|
51
|
+
- CHANGELOG.md
|
21
52
|
- Gemfile
|
22
53
|
- LICENSE
|
23
54
|
- README.md
|
@@ -35,7 +66,7 @@ homepage: https://github.com/mikker/render_with_view
|
|
35
66
|
licenses:
|
36
67
|
- MIT
|
37
68
|
metadata: {}
|
38
|
-
post_install_message:
|
69
|
+
post_install_message:
|
39
70
|
rdoc_options: []
|
40
71
|
require_paths:
|
41
72
|
- lib
|
@@ -50,9 +81,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
50
81
|
- !ruby/object:Gem::Version
|
51
82
|
version: '0'
|
52
83
|
requirements: []
|
53
|
-
|
54
|
-
|
55
|
-
signing_key:
|
84
|
+
rubygems_version: 3.4.19
|
85
|
+
signing_key:
|
56
86
|
specification_version: 4
|
57
87
|
summary: Be explicit about what you send to your Rails views
|
58
88
|
test_files: []
|