rails-render-component 5.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 645da0a46e68ef46d7b53edacc8e124b3cec5df9586e8fb55ccf062a41ce7555
4
+ data.tar.gz: 5ad018080a3cfbc308bf5937fe040ed71e55d1d356bdc7add297e2a6f50dabc4
5
+ SHA512:
6
+ metadata.gz: 90fe6727aeb85994873f1a61753681366a63329084f5d9979e228763cc843f031d1846ed4dfd09dc8e72b2ee827b8669ebac8241a1445a1c5f73a2c24e71e346
7
+ data.tar.gz: 4d9baa3a66e0476dcc7a34cef7002dd8938a3582b645aaffdec38f7784e8678b1b5119b8e1312bd22d15da484815ed90d21eb76eea7506c0ff802d356a4cf2a0
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.gitignore ADDED
@@ -0,0 +1,44 @@
1
+ # rcov generated
2
+ coverage
3
+
4
+ # rdoc generated
5
+ rdoc
6
+
7
+ # yard generated
8
+ doc
9
+ .yardoc
10
+
11
+ # bundler
12
+ .bundle
13
+
14
+ # jeweler generated
15
+ pkg
16
+
17
+ Gemfile*lock
18
+
19
+ # Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
20
+ #
21
+ # * Create a file at ~/.gitignore
22
+ # * Include files you want ignored
23
+ # * Run: git config --global core.excludesfile ~/.gitignore
24
+ #
25
+ # After doing this, these files will be ignored in all your git projects,
26
+ # saving you from having to 'pollute' every project you touch with them
27
+ #
28
+ # Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
29
+ #
30
+ # For MacOS:
31
+ #
32
+ #.DS_Store
33
+ #
34
+ # For TextMate
35
+ #*.tmproj
36
+ #tmtags
37
+ #
38
+ # For emacs:
39
+ #*~
40
+ #\#*
41
+ #.\#*
42
+ #
43
+ # For vim:
44
+ #*.swp
data/.travis.yml ADDED
@@ -0,0 +1,20 @@
1
+ language: ruby
2
+ addons:
3
+ apt:
4
+ packages:
5
+ - libgmp-dev
6
+
7
+ gemfile:
8
+ - gemfiles/Gemfile-rails.5.0.x
9
+ - gemfiles/Gemfile-rails.5.1.x
10
+ - gemfiles/Gemfile-rails.5.2.x
11
+ - gemfiles/Gemfile-rails.6.0.x
12
+ - gemfiles/Gemfile-rails.6.1.x
13
+
14
+ rvm:
15
+ - 2.5
16
+ - 2.6
17
+
18
+ script:
19
+ - cd test/example
20
+ - bundle exec rake test
data/LICENSE.txt ADDED
@@ -0,0 +1,24 @@
1
+ Copyright (c) 2007 Original creation by David Heinemeier Hansson, released under the MIT license
2
+ Copyright (c) 2008 Rebirth by Pratik Naik and Dan Powell
3
+ Copyright (c) 2010-2012 Upgrade to Rails 3.x by Volker Hochstein, Jonathan McCoy,
4
+ Copyright (c) 2013-2015 Fixes for rails 4.x by Sergio Cambra, Richard Zheng and Mathieu Jobin
5
+ Copyright (c) 2020-2021 Fix for Rails 5.0 by Mathieu Jobin
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining
8
+ a copy of this software and associated documentation files (the
9
+ "Software"), to deal in the Software without restriction, including
10
+ without limitation the rights to use, copy, modify, merge, publish,
11
+ distribute, sublicense, and/or sell copies of the Software, and to
12
+ permit persons to whom the Software is furnished to do so, subject to
13
+ the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be
16
+ included in all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # Rails::Render::Component
2
+
3
+ Legacy functionality from Rails 1.x and (maybe) 2.x days.
4
+ It execute the code in the `controller#action` before rendering the view into a string.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'rails-render-component'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle install
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install rails-render-component
21
+
22
+ ## Usage
23
+
24
+ Components allow you to call other actions for their rendered response while executing another action. You can either delegate
25
+ the entire response rendering or you can mix a partial response in with your other content.
26
+
27
+ class WeblogController < ActionController::Base
28
+ # Performs a method and then lets hello_world output its render
29
+ def delegate_action
30
+ do_other_stuff_before_hello_world
31
+ render_component :controller => "greeter", :action => "hello_world", :params => { :person => "david" }
32
+ end
33
+ end
34
+
35
+ class GreeterController < ActionController::Base
36
+ def hello_world
37
+ render :text => "#{params[:person]} says, Hello World!"
38
+ end
39
+ end
40
+
41
+ The same can be done in a view to do a partial rendering:
42
+
43
+ Let's see a greeting:
44
+ <%= render_component :controller => "greeter", :action => "hello_world" %>
45
+
46
+ It is also possible to specify the controller as a class constant, bypassing the inflector
47
+ code to compute the controller class at runtime:
48
+
49
+ <%= render_component :controller => GreeterController, :action => "hello_world" %>
50
+
51
+ == When to use components
52
+
53
+ Components should be used with care. They're significantly slower than simply splitting reusable parts into partials and
54
+ conceptually more complicated. Don't use components as a way of separating concerns inside a single application. Instead,
55
+ reserve components to those rare cases where you truly have reusable view and controller elements that can be employed
56
+ across many applications at once.
57
+
58
+ So to repeat: Components are a special-purpose approach that can often be replaced with better use of partials and filters.
59
+
60
+ ## Development
61
+
62
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
63
+
64
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
65
+
66
+ ## Contributing
67
+
68
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mathieujobin/render-component.
69
+
70
+ ## Copyright
71
+
72
+ Copyright (c) 2007 Original creation by David Heinemeier Hansson, released under the MIT license
73
+ Copyright (c) 2008 Rebirth by Pratik Naik and Dan Powell
74
+ Copyright (c) 2010-2012 Upgrade to Rails 3.x by Volker Hochstein, Jonathan McCoy,
75
+ Copyright (c) 2013-2015 Fixes for rails 4.x by Sergio Cambra, Richard Zheng and Mathieu Jobin
76
+ Copyright (c) 2020-2021 Fix for Rails 5.0 by Mathieu Jobin
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "rails/render/component"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -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,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Bundler 1.x default to insecure http:// for github: shortcut
4
+ git_source(:github){ |repo_name| "https://github.com/#{repo_name}.git" }
5
+
6
+ gemspec :path => ".."
7
+
8
+ gem "rails", "~> 5.0.7"
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Bundler 1.x default to insecure http:// for github: shortcut
4
+ git_source(:github){ |repo_name| "https://github.com/#{repo_name}.git" }
5
+
6
+ gemspec :path => ".."
7
+
8
+ gem "rails", "~> 5.1.0"
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Bundler 1.x default to insecure http:// for github: shortcut
4
+ git_source(:github){ |repo_name| "https://github.com/#{repo_name}.git" }
5
+
6
+ gemspec :path => ".."
7
+
8
+ gem "rails", "~> 5.2.4"
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Bundler 1.x default to insecure http:// for github: shortcut
4
+ git_source(:github){ |repo_name| "https://github.com/#{repo_name}.git" }
5
+
6
+ gemspec :path => ".."
7
+
8
+ gem "activerecord", "~> 6.0.0"
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Bundler 1.x default to insecure http:// for github: shortcut
4
+ git_source(:github){ |repo_name| "https://github.com/#{repo_name}.git" }
5
+
6
+ gemspec :path => ".."
7
+
8
+ gem "activerecord", "~> 6.1.0"
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'render_component/components'
4
+ require 'action_controller'
5
+ require 'action_dispatch/middleware/flash'
6
+ require_relative "component/version"
7
+ require_relative "../../render_component/components"
8
+
9
+ ActionController::Base.send :include, RenderComponent::Components
10
+
11
+ module Rails
12
+ module Render
13
+ module Component
14
+ class Error < StandardError; end
15
+ # Your code goes here...
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Render
5
+ module Component
6
+ VERSION = "5.0.1"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,162 @@
1
+ module RenderComponent
2
+ module Components
3
+ module MethodsToPrepend
4
+ def session
5
+ if parent_controller.present?
6
+ parent_controller.session
7
+ else
8
+ @_request.session
9
+ end
10
+ end
11
+
12
+ def flash(refresh = false) #:nodoc:
13
+ if @component_flash.nil? || refresh
14
+ @component_flash =
15
+ if parent_controller&.flash.is_a?(ActionDispatch::Flash::FlashHash)
16
+ parent_controller.flash
17
+ elsif session['flash'].is_a? Array
18
+ session['flash'] = ActionDispatch::Flash::FlashHash.new(session['flash'].to_h)
19
+ else
20
+ session['flash'] ||= ActionDispatch::Flash::FlashHash.new
21
+ end
22
+ end
23
+ @component_flash
24
+ end
25
+ end
26
+
27
+ def self.included(base) #:nodoc:
28
+ base.class_eval do
29
+ include InstanceMethods
30
+ extend ClassMethods
31
+ helper HelperMethods
32
+
33
+ # If this controller was instantiated to process a component request,
34
+ # +parent_controller+ points to the instantiator of this controller.
35
+ attr_accessor :parent_controller
36
+ end
37
+
38
+ base.prepend(MethodsToPrepend)
39
+ end
40
+
41
+ module ClassMethods
42
+ # Track parent controller to identify component requests
43
+ def process_with_components(request, action, parent_controller = nil) #:nodoc:
44
+ controller = new
45
+ controller.parent_controller = parent_controller
46
+ controller.dispatch(action, request, make_response!(request))
47
+ end
48
+ end
49
+
50
+ module HelperMethods
51
+ def render_component(options)
52
+ controller.send(:render_component_into_view, options)
53
+ end
54
+ end
55
+
56
+ module InstanceMethods
57
+
58
+ protected
59
+ def response_redirect(response)
60
+ if response.respond_to?(:to_path)
61
+ response.to_path
62
+ elsif response.respond_to?(:redirect_url)
63
+ response.redirect_url
64
+ else
65
+ response.instance_variable_get(:@response).redirect_url
66
+ end
67
+ end
68
+
69
+ # Renders the component specified as the response for the current method
70
+ def render_component(options) #:doc:
71
+ component_logging(options) do
72
+ component_status, component_headers, response = component_response(options)
73
+ redirect_url = response_redirect(response)
74
+ if redirect_url
75
+ redirect_to redirect_url
76
+ else
77
+ render plain: response.body, status: component_status
78
+ end
79
+ end
80
+ end
81
+
82
+ # Returns the component response as a string
83
+ def render_component_into_view(options) #:doc:
84
+ component_logging(options) do
85
+ component_status, component_headers, response = component_response(options)
86
+ redirected = response_redirect(response)
87
+ if redirected
88
+ if redirected =~ %r{://}
89
+ location = URI.parse(redirected)
90
+ redirected = location.query ? "#{location.path}?#{location.query}" : location.path
91
+ end
92
+ render_component_into_view(Rails.application.routes.recognize_path(redirected, { :method => nil }))
93
+ else
94
+ response.body.html_safe
95
+ end
96
+ end
97
+ end
98
+
99
+ private
100
+ def component_response(options)
101
+ options[:controller] = options[:controller].to_s if options[:controller] && options[:controller].is_a?(Symbol)
102
+ klass = component_class(options)
103
+ component_request = request_for_component(klass.controller_path, options)
104
+ klass.process_with_components(component_request, options[:action], self)
105
+ end
106
+
107
+ # determine the controller class for the component request
108
+ def component_class(options)
109
+ if controller = options[:controller]
110
+ controller.is_a?(Class) ? controller : "#{controller.to_s.camelize}Controller".constantize
111
+ else
112
+ self.class
113
+ end
114
+ end
115
+
116
+ # Create a new request object based on the current request.
117
+ # NOT IMPLEMENTED FOR RAILS 3 SO FAR: The new request inherits the session from the current request,
118
+ # bypassing any session options set for the component controller's class
119
+ def request_for_component(controller_path, options)
120
+ if options.is_a? Hash
121
+ old_style_params = options.delete(:params)
122
+ options.merge!(old_style_params) unless old_style_params.nil?
123
+
124
+ request_params = options.symbolize_keys
125
+ request_env = {}
126
+
127
+ request.env.select {|key, value| key == key.upcase || key == 'rack.input'}.each {|item| request_env[item[0]] = item[1]}
128
+
129
+ request_env['REQUEST_URI'] = url_for(options)
130
+ request_env["PATH_INFO"] = url_for(options.merge(:only_path => true))
131
+ request_env["action_dispatch.request.symbolized_path_parameters"] = request_params
132
+ request_env["action_dispatch.request.parameters"] = request_params.symbolize_keys
133
+ request_env["action_dispatch.request.path_parameters"] = Hash[request_params.select{|key, value| [:controller, :action].include?(key)}].symbolize_keys
134
+ request_env["warden"] = request.env["warden"] if (request.env.has_key?("warden"))
135
+ component_request = ActionDispatch::Request.new(request_env)
136
+
137
+ # its an internal request request forgery protection has to be disabled
138
+ # because otherwise forgery detection might raise an error
139
+ component_request.instance_eval do
140
+ def forgery_whitelisted?
141
+ true
142
+ end
143
+ end
144
+ component_request
145
+ else
146
+ request
147
+ end
148
+ end
149
+
150
+ def component_logging(options)
151
+ if logger
152
+ logger.info "Start rendering component (#{options.inspect}): "
153
+ result = yield
154
+ logger.info "\n\nEnd of component rendering"
155
+ result
156
+ else
157
+ yield
158
+ end
159
+ end
160
+ end
161
+ end
162
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/rails/render/component/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "rails-render-component"
7
+ spec.version = Rails::Render::Component::VERSION
8
+ spec.authors = ["David Heinemeier Hansson", "VHO", "Sergio Cambra", "Mathieu Jobin"]
9
+ spec.email = ["_", "_" "_", "mathieu@justbudget.com"]
10
+
11
+ spec.summary = "render actions in other controllers for their rendered response."
12
+ spec.description = "Components allow you to call other actions for their rendered response while executing another action"
13
+ spec.homepage = "http://github.com/mathieujobin/render_component."
14
+ spec.licenses = ["MIT"]
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = spec.homepage
21
+ spec.metadata["changelog_uri"] = spec.homepage
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_runtime_dependency("railties", ">= 5.0", "< 7")
33
+ spec.add_development_dependency("rubocop")
34
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-render-component
3
+ version: !ruby/object:Gem::Version
4
+ version: 5.0.1
5
+ platform: ruby
6
+ authors:
7
+ - David Heinemeier Hansson
8
+ - VHO
9
+ - Sergio Cambra
10
+ - Mathieu Jobin
11
+ autorequire:
12
+ bindir: exe
13
+ cert_chain: []
14
+ date: 2021-02-04 00:00:00.000000000 Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: railties
18
+ requirement: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: '5.0'
23
+ - - "<"
24
+ - !ruby/object:Gem::Version
25
+ version: '7'
26
+ type: :runtime
27
+ prerelease: false
28
+ version_requirements: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '5.0'
33
+ - - "<"
34
+ - !ruby/object:Gem::Version
35
+ version: '7'
36
+ - !ruby/object:Gem::Dependency
37
+ name: rubocop
38
+ requirement: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ description: Components allow you to call other actions for their rendered response
51
+ while executing another action
52
+ email:
53
+ - _
54
+ - __
55
+ - mathieu@justbudget.com
56
+ executables: []
57
+ extensions: []
58
+ extra_rdoc_files: []
59
+ files:
60
+ - ".document"
61
+ - ".gitignore"
62
+ - ".travis.yml"
63
+ - LICENSE.txt
64
+ - README.md
65
+ - Rakefile
66
+ - bin/console
67
+ - bin/setup
68
+ - gemfiles/Gemfile-rails.5.0.x
69
+ - gemfiles/Gemfile-rails.5.1.x
70
+ - gemfiles/Gemfile-rails.5.2.x
71
+ - gemfiles/Gemfile-rails.6.0.x
72
+ - gemfiles/Gemfile-rails.6.1.x
73
+ - lib/rails/render/component.rb
74
+ - lib/rails/render/component/version.rb
75
+ - lib/render_component/components.rb
76
+ - rails-render-component.gemspec
77
+ homepage: http://github.com/mathieujobin/render_component.
78
+ licenses:
79
+ - MIT
80
+ metadata:
81
+ allowed_push_host: https://rubygems.org
82
+ homepage_uri: http://github.com/mathieujobin/render_component.
83
+ source_code_uri: http://github.com/mathieujobin/render_component.
84
+ changelog_uri: http://github.com/mathieujobin/render_component.
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: 2.3.0
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubygems_version: 3.0.8
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: render actions in other controllers for their rendered response.
104
+ test_files: []