Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,21 @@ You can add optional interpolation modifiers using the `%{variable:modifiers}` s
- `h`: humanize (for instance, `user_name` becomes `User name`)
- `p`: format phone (for instance, `48111222333` becomes `+48 111 222 333`)

### URL Defaults

Textris uses `ActionController::Renderer` behind the scenes. Add or modify `config/initializers/application_controller_renderer.rb` in your Rails to change the default settings:

```
# Be sure to restart your server when you modify this file.

ActiveSupport::Reloader.to_prepare do
ApplicationController.renderer.defaults.merge!(
http_host: ENV['CANONICAL_HOST'], # or ActionMailer::Base.default_url_options[:host] to use the same host as ActionMailer
https: Rails.env.production?
)
end
```

## Example project

[Here](https://github.com/visualitypl/textris/tree/master/example/rails-4.2) you can find a simple example project that demonstrates **textris** usage with Rails 4.2. In order to see how it works or experiment with it, just go to project's directory and invoke:
Expand Down
29 changes: 12 additions & 17 deletions lib/textris/base.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
require 'render_anywhere'

module Textris
class Base
class RenderingController < RenderAnywhere::RenderingController
layout false

def default_url_options
ActionMailer::Base.default_url_options || {}
end
end

include RenderAnywhere
extend Textris::Delay::Sidekiq

class << self
Expand Down Expand Up @@ -53,9 +42,15 @@ def call_action
end

def render_content
set_instance_variables_for_rendering

render(:template => template_name, :formats => ['text'], :locale => @locale)
renderer = ::ApplicationController.renderer.new

@no-itsbackpack no-itsbackpack Aug 18, 2019

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danielpuglisi 👋 I just tested this PR on a rails api I am upgrading to rails 6 and renderer.render keeps returning an empty string because views are not loaded in rails apis. The controllers in rails apis inherit from ApplicationController::API instead of ApplicationController::Base. I was wondering if we could switch to using

renderer = ::ActionController::Base.renderer.new
renderer.render(
  template: template_name,
  layout: false,
  formats: [:text],
  locale: @locale,
  assigns: set_instance_variables_for_rendering
)

which works on both Rails apis and vanilla rails apps

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the input @no-itsbackpack.

True, didn't think about that. I used ApplicationController as Helpers are automatically available this way.

Not sure how to achieve this with ActionController::Base though. Going back to using...

module Textris
  class Base
    class RenderingController < ActionController::Base
      helper :all
    end

  # ...

  def render_content
    renderer = RenderingController.renderer.new

  # ...

... does not seem to load helpers defined in the application.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea I totally didnt consider loading controller helpers, back to the drawing board 🤔


renderer.render(
template: template_name,
layout: false,
formats: [:html],

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
formats: [:html],
formats: [:text],

:formats is [:text] ?

locale: @locale,
assigns: set_instance_variables_for_rendering
)
end

protected
Expand Down Expand Up @@ -84,9 +79,9 @@ def template_name
end

def set_instance_variables_for_rendering
instance_variables.each do |var|
set_instance_variable(var.to_s.sub('@', ''), instance_variable_get(var))
end
instance_variables.map do |var|
[var.to_s.sub('@', ''), instance_variable_get(var)]
end.to_h
end
end
end
23 changes: 1 addition & 22 deletions spec/textris/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def set_instance_variable(key, value)
it 'defers template rendering when :body not provided' do
render_options = {}

expect_any_instance_of(MyTexter).not_to receive(:render)
expect_any_instance_of(MyTexter).not_to receive(:render_content)

MyTexter.action_with_template
end
Expand Down Expand Up @@ -151,25 +151,4 @@ def my_action(p)
expect(MyTexter.respond_to?(:fake_action)).to eq false
end
end

describe Textris::Base::RenderingController do
before do
class Textris::Base::RenderingController
def initialize(*args)
end
end

class ActionMailer::Base
def self.default_url_options
'x'
end
end
end

it 'maps default_url_options to ActionMailer configuration' do
rendering_controller = Textris::Base::RenderingController.new

expect(rendering_controller.default_url_options).to eq 'x'
end
end
end
2 changes: 1 addition & 1 deletion spec/textris/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class SomeSampleTexter; end

describe '#content' do
before do
class Textris::Base::RenderingController
class ApplicationController < ActionController::Base
def initialize(*args)
end
end
Expand Down
4 changes: 1 addition & 3 deletions textris.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ Gem::Specification.new do |spec|
spec.description = "Implement texter classes for sending SMS messages in similar way to how e-mails are sent with ActionMailer-based mailers. Take advantage of e-mail proxying and enhanced phone number parsing, among others."

spec.files = Dir["lib/**/*.rb"]
spec.has_rdoc = false
spec.extra_rdoc_files = ["README.md"]
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_development_dependency 'bundler', '~> 1.6'
spec.add_development_dependency 'bundler', '~> 2.0'
spec.add_development_dependency 'codeclimate-test-reporter', '~> 0.4'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '~> 3.1'
Expand All @@ -36,5 +35,4 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency 'activejob', '>= 4.2'
spec.add_runtime_dependency 'activesupport', '>= 4.2'
spec.add_runtime_dependency 'phony', '~> 2.8'
spec.add_runtime_dependency 'render_anywhere', '~> 0.0'
end