viu 0.0.0 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "viu"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,2 @@
1
+ ---
2
+ BUNDLE_RETRY: "1"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 5.2.0"
6
+ gem "oj"
7
+
8
+ gemspec path: "../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 6.0.0"
6
+ gem "oj"
7
+
8
+ gemspec path: "../"
data/lib/viu.rb CHANGED
@@ -1,2 +1,17 @@
1
- class Viu
1
+ # frozen_string_literal: true
2
+
3
+ require "viu/version"
4
+
5
+ require "viu/html"
6
+ require "viu/layout"
7
+ require "viu/json"
8
+ require "viu/xml"
9
+ require "viu/test_case"
10
+
11
+ require 'viu/railtie' if defined?(Rails)
12
+
13
+ module Viu
14
+ EMPTY_HASH = {}.freeze
15
+
16
+ class Error < StandardError; end
2
17
  end
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "action_view"
4
+
5
+ module Viu
6
+ class Html < ActionView::Base
7
+
8
+ def initialize(*); end
9
+
10
+ module CompiledMethodContainer; end
11
+
12
+ def compiled_method_container
13
+ Viu::Html::CompiledMethodContainer
14
+ end
15
+
16
+ def render_in(view_context, options = EMPTY_HASH)
17
+ __setup!(view_context)
18
+
19
+ return __render_template_proc if __fetch_template!.is_a?(Proc)
20
+
21
+ layout = options.fetch(:layout, self.class.layout)
22
+
23
+ if layout.is_a?(Class)
24
+ __render_layout_class(layout)
25
+ elsif layout.is_a?(Proc)
26
+ __render_layout_proc(layout)
27
+ else
28
+ @view_renderer.render(self, template: __fetch_template!, layout: layout)
29
+ end
30
+ end
31
+
32
+ attr_reader :controller
33
+
34
+ private
35
+
36
+ attr_reader :request
37
+
38
+ delegate :protect_against_forgery?, :form_authenticity_token, :content_security_policy?, to: :helpers
39
+
40
+ def helpers
41
+ @helpers ||= controller.view_context
42
+ end
43
+
44
+ def __setup!(view_context)
45
+ @view_context = view_context
46
+ @lookup_context ||= view_context.lookup_context
47
+ @view_renderer ||= view_context.view_renderer
48
+ @controller ||= view_context.controller
49
+ @request ||= controller.request
50
+ _prepare_context
51
+ end
52
+
53
+ def __fetch_template!
54
+ return self.class.template if self.class.template.present?
55
+
56
+ self.class.template!(self.class.name.underscore)
57
+ end
58
+
59
+ def __render_template_proc
60
+ self.instance_exec(&__fetch_template!)
61
+ end
62
+
63
+ def __render_layout_class(layout)
64
+ layout.new.render_in(@view_context) do
65
+ @view_renderer.render(self, template: __fetch_template!, layout: nil)
66
+ end
67
+ end
68
+
69
+ def __render_layout_proc(layout)
70
+ self.instance_exec(&layout).render_in(@view_context) do
71
+ @view_renderer.render(self, template: __fetch_template!, layout: nil)
72
+ end
73
+ end
74
+
75
+ class << self
76
+
77
+ def inherited(child)
78
+ child.include(Viu::Html::CompiledMethodContainer)
79
+
80
+ if defined?(Rails) && child != Viu::Layout
81
+ child.include(Rails.application.routes.url_helpers) unless child < Rails.application.routes.url_helpers
82
+ end
83
+ end
84
+
85
+ def layout!(value)
86
+ @__layout = value
87
+ end
88
+
89
+ def layout
90
+ @__layout ||= nil
91
+ end
92
+
93
+ def template!(value)
94
+ @__template = value
95
+ end
96
+
97
+ def template
98
+ @__template ||= nil
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+ require 'active_support/core_ext/class/attribute'
5
+
6
+ module Viu
7
+ module Json
8
+ extend ActiveSupport::Concern
9
+
10
+ DEFAULT_JSON_ENCODER = ->(input) { input.to_json }.freeze
11
+
12
+ included do
13
+ class_attribute :__json_encoder, instance_accessor: false, instance_predicate: false,
14
+ default: DEFAULT_JSON_ENCODER
15
+
16
+ attr_reader :rendering_options
17
+
18
+ def to_json(options = EMPTY_HASH)
19
+ @rendering_options = options
20
+ self.class.__json_encoder.call(json_output)
21
+ end
22
+ end
23
+
24
+ class_methods do
25
+
26
+ def json_encoder(value)
27
+ self.__json_encoder = value
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "viu/html"
4
+
5
+ module Viu
6
+ class Layout < Html
7
+
8
+ class Renderer < ActionView::PartialRenderer
9
+
10
+ # https://github.com/rails/rails/blob/v5.2.4.3/actionview/lib/action_view/renderer/partial_renderer.rb#L421
11
+ def find_template(path, locals)
12
+ prefixes = path.include?(?/) ? [] : @lookup_context.prefixes
13
+ # override partial arg to false, so that it will look for templates without a prefixed _
14
+ @lookup_context.find_template(path, prefixes, false, locals, @details)
15
+ end
16
+ end
17
+
18
+ RAILS_6_0 = Gem::Version.new('6.0.0')
19
+ RAILS_6_1_WITH_PRERELEASES = Gem::Version.new('6.1.0.a')
20
+ private_constant :RAILS_6_0, :RAILS_6_1_WITH_PRERELEASES
21
+
22
+ def render_in(view_context, &block)
23
+ __setup!(view_context)
24
+
25
+ rails_version = ::Rails.gem_version
26
+
27
+ if rails_version >= RAILS_6_1_WITH_PRERELEASES
28
+ Renderer.new(@lookup_context, layout: nil).render(__fetch_template!, self, block).body
29
+ elsif rails_version >= RAILS_6_0
30
+ Renderer.new(@lookup_context).render(self, { partial: __fetch_template!, layout: nil }, block).body
31
+ else
32
+ Renderer.new(@lookup_context).render(self, { partial: __fetch_template!, layout: nil }, block)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails"
4
+ require "viu/rendering_helpers"
5
+
6
+ module Viu
7
+ class Railtie < Rails::Railtie
8
+
9
+ initializer "viu.include_rendering_helpers" do |app|
10
+ ActiveSupport.on_load(:action_view) do
11
+ ActionView::Base.include Viu::RenderingHelpers::ActionViewHelpers
12
+ end
13
+
14
+ ActiveSupport.on_load(:action_controller) do
15
+ ActionController::Base.include Viu::RenderingHelpers::ActionControllerHelpers
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Viu
4
+ module RenderingHelpers
5
+ module ActionViewHelpers
6
+ def render_view(view, options = EMPTY_HASH)
7
+ view.render_in(@view_context, options)
8
+ end
9
+ end
10
+
11
+ module ActionControllerHelpers
12
+ def render_view(view, options = EMPTY_HASH)
13
+ self.response_body = view.render_in(view_context, options)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "action_view/test_case"
4
+ require "rails/dom/testing/assertions"
5
+ require "viu/test_helpers"
6
+
7
+ module Viu
8
+ class TestCase < ActionView::TestCase
9
+ include Rails::Dom::Testing::Assertions
10
+ include Viu::TestHelpers
11
+ end
12
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+ require 'active_support/inflector'
5
+ require 'active_support/core_ext/class/attribute'
6
+
7
+ module Viu
8
+ module TestHelpers
9
+ extend ActiveSupport::Concern
10
+
11
+ private
12
+
13
+ attr_reader :view_instance, :raw_rendered_view, :rendered_view
14
+
15
+ def render_view(view, **args)
16
+ @rendered ||= []
17
+ @view_instance = view
18
+ @raw_rendered_view = @view_instance.render_in(__controller.view_context, **args)
19
+ @rendered << @raw_rendered_view
20
+ @rendered_view = Nokogiri::HTML::Document.parse(@raw_rendered_view)
21
+ end
22
+
23
+ def __controller
24
+ @__controller ||= __constantize(self.class.__controller_class).new.tap do |c|
25
+ c.request = request
26
+ c.action_name = 'show'
27
+ end
28
+ .extend(Rails.application.routes.url_helpers)
29
+ end
30
+
31
+ def request
32
+ @request ||= ActionDispatch::TestRequest.create
33
+ end
34
+
35
+ def __constantize(value)
36
+ return value if value.is_a?(Class)
37
+
38
+ ActiveSupport::Inflector.constantize(value)
39
+ end
40
+
41
+ included do
42
+ class_attribute :__controller_class, instance_accessor: false, instance_predicate: false,
43
+ default: 'ApplicationController'
44
+ end
45
+
46
+ class_methods do
47
+
48
+ def controller_class(klass)
49
+ self.__controller_class = klass
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Viu
4
+ VERSION = "0.0.5"
5
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+ require 'active_support/core_ext/class/attribute'
5
+
6
+ module Viu
7
+ module Xml
8
+ extend ActiveSupport::Concern
9
+
10
+ DEFAULT_XML_ENCODER = ->(input) { input.to_xml }.freeze
11
+
12
+ included do
13
+ class_attribute :__xml_encoder, instance_accessor: false, instance_predicate: false,
14
+ default: DEFAULT_XML_ENCODER
15
+
16
+ attr_reader :rendering_options
17
+
18
+ def to_xml(options = EMPTY_HASH)
19
+ @rendering_options = options
20
+ self.class.__xml_encoder.call(xml_output)
21
+ end
22
+ end
23
+
24
+ class_methods do
25
+
26
+ def xml_encoder(value)
27
+ self.__xml_encoder = value
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "viu/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "viu"
9
+ spec.version = Viu::VERSION
10
+ spec.authors = ["Ralf Schmitz Bongiolo"]
11
+ spec.email = ["mrbongiolo@hey.com"]
12
+
13
+ spec.summary = %q{Rails' missing View layer.}
14
+ spec.description = %q{Rails' missing View layer.}
15
+ spec.homepage = "https://github.com/mrbongiolo/viu"
16
+ spec.license = "MIT"
17
+
18
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
19
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
20
+ if spec.respond_to?(:metadata)
21
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
22
+
23
+ spec.metadata["homepage_uri"] = spec.homepage
24
+ spec.metadata["source_code_uri"] = "https://github.com/mrbongiolo/viu"
25
+ spec.metadata["changelog_uri"] = "https://github.com/mrbongiolo/viu/CHANGELOG.md"
26
+ else
27
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
28
+ end
29
+
30
+ # Specify which files should be added to the gem when it is released.
31
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
32
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
33
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
34
+ end
35
+ spec.require_paths = ["lib"]
36
+
37
+ spec.add_development_dependency "bundler", "~> 1.17"
38
+ spec.add_development_dependency "rake", "~> 12.3"
39
+ spec.add_development_dependency "minitest", "~> 5.0"
40
+ spec.add_development_dependency "simplecov", "~> 0.18.0"
41
+ spec.add_development_dependency "haml", "~> 5"
42
+ spec.add_development_dependency "slim", "~> 4"
43
+ spec.add_development_dependency "appraisal", "~> 2.3"
44
+ end
metadata CHANGED
@@ -1,26 +1,154 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: viu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ralf Schmitz Bongiolo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-22 00:00:00.000000000 Z
12
- dependencies: []
13
- description: Views that work with RoR and everywhere else
14
- email: mrbongiolo@hey.com
11
+ date: 2020-12-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.18.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.18.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: haml
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5'
83
+ - !ruby/object:Gem::Dependency
84
+ name: slim
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '4'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '4'
97
+ - !ruby/object:Gem::Dependency
98
+ name: appraisal
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '2.3'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '2.3'
111
+ description: Rails' missing View layer.
112
+ email:
113
+ - mrbongiolo@hey.com
15
114
  executables: []
16
115
  extensions: []
17
116
  extra_rdoc_files: []
18
117
  files:
118
+ - ".gitignore"
119
+ - ".travis.yml"
120
+ - Appraisals
121
+ - CHANGELOG.md
122
+ - CODE_OF_CONDUCT.md
123
+ - Gemfile
124
+ - Gemfile.lock
125
+ - LICENSE.txt
126
+ - README.md
127
+ - Rakefile
128
+ - bin/console
129
+ - bin/setup
130
+ - gemfiles/.bundle/config
131
+ - gemfiles/rails_5_2.gemfile
132
+ - gemfiles/rails_6_0.gemfile
19
133
  - lib/viu.rb
20
- homepage: https://rubygems.org/gems/viu
134
+ - lib/viu/html.rb
135
+ - lib/viu/json.rb
136
+ - lib/viu/layout.rb
137
+ - lib/viu/railtie.rb
138
+ - lib/viu/rendering_helpers.rb
139
+ - lib/viu/test_case.rb
140
+ - lib/viu/test_helpers.rb
141
+ - lib/viu/version.rb
142
+ - lib/viu/xml.rb
143
+ - viu.gemspec
144
+ homepage: https://github.com/mrbongiolo/viu
21
145
  licenses:
22
146
  - MIT
23
- metadata: {}
147
+ metadata:
148
+ allowed_push_host: https://rubygems.org
149
+ homepage_uri: https://github.com/mrbongiolo/viu
150
+ source_code_uri: https://github.com/mrbongiolo/viu
151
+ changelog_uri: https://github.com/mrbongiolo/viu/CHANGELOG.md
24
152
  post_install_message:
25
153
  rdoc_options: []
26
154
  require_paths:
@@ -39,5 +167,5 @@ requirements: []
39
167
  rubygems_version: 3.0.3
40
168
  signing_key:
41
169
  specification_version: 4
42
- summary: Because everyone needs views
170
+ summary: Rails' missing View layer.
43
171
  test_files: []