render_anywhere_lxv 0.0.12

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8eacbbdc35d179125219e17109dc332cefdc0b86
4
+ data.tar.gz: 941ed26d3de783a597b526a971c68d01e1b72e21
5
+ SHA512:
6
+ metadata.gz: ed8e87dbe26c083bd8918f738d2aeda92da44fe6d12c3a2d5e41dd5d55a7b9eda95c61260065dd7a1d3ba1285fbaf828af97e99008780ae454990717e29e6348
7
+ data.tar.gz: d38f1b98c899bcd4e22087edd2b92aa598ee3c654a4dde2df9db4ad1c82bea9c82f47f7faa5cbc68a8152937d8c0beb38ce2fc00f3aabd27dcfd5403ebda3daa
@@ -0,0 +1,7 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ Gemfile.lock
5
+ log/
6
+
7
+ gemfiles/*.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
@@ -0,0 +1,14 @@
1
+ appraise "3.2" do
2
+ gem "rails", "~> 3.2.15"
3
+ gemspec
4
+ end
5
+
6
+ appraise "4.0" do
7
+ gem "rails", "~> 4.0.0"
8
+ gemspec
9
+ end
10
+
11
+ appraise "4.1" do
12
+ gem "rails", "~> 4.1.0"
13
+ gemspec
14
+ end
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in render_anywhere.gemspec
4
+ gemspec
data/LICENCE ADDED
@@ -0,0 +1,19 @@
1
+ The MIT License (MIT)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,83 @@
1
+ render_anywhere
2
+ ====================
3
+
4
+ Out of the box, Rails can render templates in a controller context only. This little gem allows for calling "render" from anywhere: models, background jobs, rake tasks, you name it.
5
+
6
+ Installation
7
+ ------------------
8
+
9
+ ```bash
10
+ gem install render_anywhere
11
+ ```
12
+
13
+ Usage
14
+ --------------------
15
+
16
+ Put render_anywhere in your Gemfile:
17
+
18
+ ```ruby
19
+ gem 'render_anywhere', :require => false
20
+ ```
21
+
22
+ In your Rails app, in a rake task, model, background job, or where ever you like, require render_anywhere, include the module and call render with the same arguments as ActionController::Base#render takes. It will return a string.
23
+
24
+ ```ruby
25
+ require 'render_anywhere'
26
+
27
+ class AnyClass
28
+ include RenderAnywhere
29
+
30
+ def build_html
31
+ html = render :template => 'normal/template/reference',
32
+ :layout => 'application'
33
+ html
34
+ end
35
+ # Include an additional helper
36
+ # If being used in a rake task, you may need to require the file(s)
37
+ # Ex: require Rails.root.join('app', 'helpers', 'blog_pages_helper')
38
+ def include_helper(helper_name)
39
+ set_render_anywhere_helpers(helper_name)
40
+ end
41
+
42
+ # Apply an instance variable to the controller
43
+ # If you need to use instance variables instead of locals, just call this method as many times as you need.
44
+ def set_instance_variable(var, value)
45
+ set_instance_variable(var, value)
46
+ end
47
+
48
+ class RenderingController < RenderAnywhere::RenderingController
49
+ # include custom modules here, define accessors, etc. For example:
50
+ attr_accessor :current_user
51
+ helper_method :current_user
52
+ end
53
+
54
+ # If you define custom RenderingController, don't forget to override this method
55
+ def rendering_controller
56
+ @rendering_controller ||= self.class.const_get("RenderingController").new
57
+ end
58
+ end
59
+ ```
60
+
61
+ Thanks
62
+ --------------------
63
+
64
+ [Yapp](http://yapp.us), whose CTO (me) kindly agreed to open source this library. App yourself!
65
+
66
+ The basic approach used here came from [this gist](https://gist.github.com/977181) by [Julien Guimont aka juggy](https://github.com/juggy). Thanks!
67
+
68
+ Contributing
69
+ --------------------
70
+
71
+ Run tests with `rake` or `rspec`.
72
+
73
+ Run tests against different versions of rails with `appraisal rake` or `appraisal rspec`.
74
+
75
+ Author
76
+ --------------------
77
+
78
+ Luke Melia, [@lukemelia](https://twitter.com/lukemelia), [lukemelia.com](http://lukemelia.com)
79
+
80
+ License
81
+ --------------------
82
+
83
+ The MIT License. Copyright (c) 2011, Yapp, Inc.
@@ -0,0 +1,11 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ task :default => [:spec]
7
+
8
+ desc "Run the specs."
9
+ RSpec::Core::RakeTask.new do |t|
10
+ t.pattern = "spec/**/*_spec.rb"
11
+ end
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rails", "~> 3.2.15"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rails", "~> 4.0.0"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rails", "~> 4.1.0"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,30 @@
1
+ require 'abstract_controller'
2
+ require 'action_view'
3
+
4
+ require 'render_anywhere/version'
5
+ require 'render_anywhere/rendering_controller'
6
+
7
+ module RenderAnywhere
8
+ def render(*args)
9
+ rendering_controller.render_to_string(*args)
10
+ end
11
+
12
+ def set_render_anywhere_helpers(*args)
13
+ args.each do |helper_name|
14
+ rendering_controller.class_eval do
15
+ helper helper_name.to_s.constantize
16
+ end
17
+ end
18
+ end
19
+
20
+ def set_instance_variable(var, value)
21
+ rendering_controller.class_eval do
22
+ attr_accessor :"#{var}"
23
+ end
24
+ rendering_controller.public_send("#{var}=", value)
25
+ end
26
+
27
+ def rendering_controller
28
+ @rendering_controller ||= self.class.const_get("RenderingController").new
29
+ end
30
+ end
@@ -0,0 +1,70 @@
1
+ require 'ostruct'
2
+
3
+ module RenderAnywhere
4
+ class RenderingController < ActionController::Metal
5
+ # Include all the concerns we need to make this work
6
+ include AbstractController::Logger
7
+ include AbstractController::Rendering
8
+ include ActionView::Layouts if defined?(ActionView::Layouts) # Rails 4.1.x
9
+ include AbstractController::Layouts if defined?(AbstractController::Layouts) # Rails 3.2.x, 4.0.x
10
+ include CacheDigests::ViewCacheDependency if defined?(CacheDigests)
11
+ include AbstractController::Helpers
12
+ include AbstractController::Translation
13
+ include AbstractController::AssetPaths
14
+ include ActionController::Caching
15
+
16
+ # Define additional helpers, this one is for csrf_meta_tag
17
+ helper_method :protect_against_forgery?
18
+
19
+ # override the layout in your subclass if needed.
20
+ layout 'application'
21
+
22
+ # configure the different paths correctly
23
+ def initialize(*args)
24
+ super()
25
+
26
+ self.class.send :include, Rails.application.routes.url_helpers
27
+
28
+ # this is you normal rails application helper
29
+ if defined?(ApplicationHelper)
30
+ self.class.send :helper, ApplicationHelper
31
+ end
32
+
33
+ lookup_context.view_paths = ApplicationController.view_paths
34
+ config.javascripts_dir = Rails.root.join('public', 'javascripts')
35
+ config.stylesheets_dir = Rails.root.join('public', 'stylesheets')
36
+ config.assets_dir = Rails.root.join('public')
37
+ config.cache_store = ActionController::Base.cache_store
38
+ config.relative_url_root = ActionController::Base.relative_url_root
39
+
40
+ # same asset host as the controllers
41
+ self.asset_host = ActionController::Base.asset_host
42
+ end
43
+
44
+ # we are not in a browser, no need for this
45
+ def protect_against_forgery?
46
+ false
47
+ end
48
+
49
+ # so that your flash calls still work
50
+ def flash
51
+ {}
52
+ end
53
+
54
+ # and nil request to differentiate between live and offline
55
+ def request
56
+ OpenStruct.new
57
+ end
58
+
59
+ # and params will be accessible
60
+ def params
61
+ {}
62
+ end
63
+
64
+ # so that your cookies calls still work
65
+ def cookies
66
+ {}
67
+ end
68
+
69
+ end
70
+ end
@@ -0,0 +1,3 @@
1
+ module RenderAnywhere
2
+ VERSION = "0.0.12"
3
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "render_anywhere/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "render_anywhere_lxv"
7
+ s.version = RenderAnywhere::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Luke Melia", "Will Schreiber"]
10
+ s.email = ["w@lxv.io"]
11
+ s.homepage = ""
12
+ s.summary = %q{Render Rails templates to a string from anywhere.}
13
+ s.description = %q{Out of the box, Rails will render templates in a controller context only. This gem allows for calling "render" from anywhere: models, background jobs, rake tasks, you name it.}
14
+ s.licenses = ['MIT']
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency('rails', '>= 3.0.7')
22
+
23
+ s.add_development_dependency 'rspec'
24
+ s.add_development_dependency 'appraisal'
25
+ end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ describe RenderAnywhere do
4
+
5
+ class UserRenderer
6
+ include RenderAnywhere
7
+
8
+ def initialize(user)
9
+ @user = user
10
+ end
11
+
12
+ def render_ivar
13
+ set_instance_variable(:user, @user)
14
+ render 'spec/views/user_ivar', :layout => false
15
+ end
16
+
17
+ def render_current_user
18
+ rendering_controller.action_name = 'show'
19
+ rendering_controller.controller_name = 'user'
20
+ rendering_controller.current_user = @user
21
+ render 'spec/views/current_user', :layout => false
22
+ end
23
+
24
+ class RenderingController < RenderAnywhere::RenderingController
25
+ attr_accessor :controller_name
26
+ attr_accessor :current_user
27
+ helper_method :current_user
28
+ end
29
+ end
30
+
31
+ class OtherRenderer
32
+ include RenderAnywhere
33
+ end
34
+
35
+ User = Struct.new(:name)
36
+
37
+ let(:me) { User.new("Guzman") }
38
+ subject { UserRenderer.new(me) }
39
+
40
+ context "#rendering_controller" do
41
+ it "creates a RenderingController of the class nested inside the class using RenderAnywhere" do
42
+ expect(subject.rendering_controller).to be_a(UserRenderer::RenderingController)
43
+ end
44
+
45
+ it "creates a RenderingController of default class" do
46
+ expect(OtherRenderer.new.rendering_controller).to be_a(RenderAnywhere::RenderingController)
47
+ end
48
+ end
49
+
50
+ context "#render" do
51
+ it "renders with ivar" do
52
+ expect(subject.render_ivar).to eq("The current user is Guzman.\n")
53
+ end
54
+
55
+ it "renders with render_current_user" do
56
+ expect(subject.render_current_user).to eq("The current user is Guzman.\nThe controller name is user.\nThe controller action is show.\n")
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,42 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+
9
+ require 'bundler/setup'
10
+
11
+ $:.unshift File.expand_path("../../lib", __FILE__)
12
+
13
+ require 'rails'
14
+ require 'action_controller'
15
+
16
+ require 'render_anywhere'
17
+
18
+
19
+ RSpec.configure do |config|
20
+ config.treat_symbols_as_metadata_keys_with_true_values = true
21
+ config.run_all_when_everything_filtered = true
22
+ config.filter_run :focus
23
+
24
+ # Run specs in random order to surface order dependencies. If you find an
25
+ # order dependency and want to debug it, you can fix the order by providing
26
+ # the seed, which is printed after each run.
27
+ # --seed 1234
28
+ config.order = 'random'
29
+ end
30
+
31
+ class TestRailsApp < Rails::Application
32
+ config.secret_key_base = '1234'
33
+ config.eager_load = false if Rails::VERSION::MAJOR == 4
34
+
35
+ initialize!
36
+ end
37
+
38
+ module ApplicationHelper
39
+ end
40
+
41
+ class ApplicationController < ActionController::Base
42
+ end
@@ -0,0 +1,3 @@
1
+ The current user is <%= current_user.name %>.
2
+ The controller name is <%= controller.controller_name %>.
3
+ The controller action is <%= controller.action_name %>.
@@ -0,0 +1 @@
1
+ The current user is <%= @user.name %>.
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: render_anywhere_lxv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.12
5
+ platform: ruby
6
+ authors:
7
+ - Luke Melia
8
+ - Will Schreiber
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2017-06-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 3.0.7
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 3.0.7
28
+ - !ruby/object:Gem::Dependency
29
+ name: rspec
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: appraisal
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ description: 'Out of the box, Rails will render templates in a controller context
57
+ only. This gem allows for calling "render" from anywhere: models, background jobs,
58
+ rake tasks, you name it.'
59
+ email:
60
+ - w@lxv.io
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".gitignore"
66
+ - ".rspec"
67
+ - Appraisals
68
+ - Gemfile
69
+ - LICENCE
70
+ - README.md
71
+ - Rakefile
72
+ - gemfiles/3.2.gemfile
73
+ - gemfiles/4.0.gemfile
74
+ - gemfiles/4.1.gemfile
75
+ - lib/render_anywhere.rb
76
+ - lib/render_anywhere/rendering_controller.rb
77
+ - lib/render_anywhere/version.rb
78
+ - render_anywhere.gemspec
79
+ - spec/lib/render_anywhere_spec.rb
80
+ - spec/spec_helper.rb
81
+ - spec/views/current_user.html.erb
82
+ - spec/views/user_ivar.html.erb
83
+ homepage: ''
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.6.8
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Render Rails templates to a string from anywhere.
107
+ test_files:
108
+ - spec/lib/render_anywhere_spec.rb
109
+ - spec/spec_helper.rb
110
+ - spec/views/current_user.html.erb
111
+ - spec/views/user_ivar.html.erb