Skip to content

Feature/no request context#486

Closed
eacaps wants to merge 16 commits into
shakacode:masterfrom
eacaps:feature/no-request-context
Closed

Feature/no request context#486
eacaps wants to merge 16 commits into
shakacode:masterfrom
eacaps:feature/no-request-context

Conversation

@eacaps

@eacaps eacaps commented Jul 21, 2016

Copy link
Copy Markdown
Contributor

I recently moved our project from browserify to webpack using your tutorial. Great job on that! However I ran into an issue when attempting to use the react_component helper inside an actionmailer. The react_on_rails_helper.rb rails_context method expects to always come from a context that has a request. Unfortunately this is not the case when creating an e-mail via an ActionMailer. I did some basic modifications of react_on_rails_helper.rb to mitigate this issue and included a full test case in your test suite to handle this. You can see what was happening earlier by checking out this commit and running the tests. I'm obviously open to some tweaks to my implementation of rails_context since right now if there's no request it will just return an empty object.

Thanks again for your great work on react_on_rails, hopefully this will help.


This change is Reviewable

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.05%) to 82.353% when pulling 6fdb73a on eacaps:feature/no-request-context into e6afa98 on shakacode:master.

@justin808

Copy link
Copy Markdown
Member

Review status: 0 of 5 files reviewed at latest revision, 4 unresolved discussions.


app/helpers/react_on_rails_helper.rb, line 348 [r1] (raw file):

    @rails_context ||= begin
      result = {}
      unless request.nil?

how about

if request.present?

spec/dummy/app/mailers/dummy_mailer.rb, line 4 [r1] (raw file):

  add_template_helper(ReactOnRailsHelper)
  default from: "nobody@nope.com"
  # layout 'mailer'

what is this comment?


spec/dummy/app/views/dummy_mailer/hello_email.html.erb, line 7 [r1] (raw file):

        name: "Mr. Mailing Server Side Rendering"
    }
}, prerender: true) %>

needs line return -- see the red symbol


spec/dummy/config/initializers/react_on_rails.rb, line 5 [r1] (raw file):

  # all calls to react_component and redux_store for rendering
  def self.custom_context(view_context)
    result = {}

could we do this without a rescue?

is there something in view_context that tells us we're in a mailer. This is the Rails API. Introspect on view_context in the debugger, like in pry.

We also need to update the docs on the railsContext for this case, and in general for this fix. We should put in some flag that we're in mailer, like

{
  mailer: true | false;
}

Comments from Reviewable

@justin808

Copy link
Copy Markdown
Member

Nice job @eacaps! Some comments!


Reviewed 5 of 5 files at r1.
Review status: all files reviewed at latest revision, 4 unresolved discussions.


Comments from Reviewable

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.05%) to 82.353% when pulling 840bc62 on eacaps:feature/no-request-context into 9a8b54f on shakacode:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.05%) to 82.353% when pulling 840bc62 on eacaps:feature/no-request-context into 9a8b54f on shakacode:master.

@eacaps

eacaps commented Jul 22, 2016

Copy link
Copy Markdown
Contributor Author

@justin808 I've made the first few tweaks you recommended. The last suggestion relates to a special custom_context that was already being added to the dummy tester. I tried several ways to get around using a rescue block but nothing really seems to work properly (.try, .respond_to, etc..). Accessing session from view_context uses a delegate from the ActionView::Helpers::ControllerHelper and since the base ActionMailer does not have a session method you end up with: undefined method `session' for #DummyMailer:0x007fe1c8e58f10. Since this is in the testing code and not the actual library I felt okay leaving the rescue block in there, but if anyone has another suggestion please feel free to make it.

As far as improving the railsContext in this particular case those changes would be made in the react_on_rails_helper.rb in the rails_context method. The first thought that comes to mind would be changing the initial result to be

result = { mailer: self.controller.kind_of?(ActionMailer::Base) }

I haven't actually implemented that yet, but let me know if that all makes sense.

@eacaps

eacaps commented Jul 22, 2016

Copy link
Copy Markdown
Contributor Author

Also I found this ancient rails issue related to ActionMailer delegation: rails/rails#15613. No action since 2014 but it describes the issue.

@justin808

Copy link
Copy Markdown
Member

Let's go with a property called inMailer, as mailer sounds like more than a boolean.

Maybe to avoid the exception, we can have the customization of the railsContext check this inMailer flag?

happy hacking! this is looking good!


Reviewed 3 of 3 files at r2.
Review status: all files reviewed at latest revision, 4 unresolved discussions.


spec/dummy/config/initializers/react_on_rails.rb, line 5 [r1] (raw file):

Previously, justin808 (Justin Gordon) wrote…

could we do this without a rescue?

is there something in view_context that tells us we're in a mailer. This is the Rails API. Introspect on view_context in the debugger, like in pry.

We also need to update the docs on the railsContext for this case, and in general for this fix. We should put in some flag that we're in mailer, like

{
  mailer: true | false;
}
@eacaps Did you get a chance to hack on this, to figure out from the view_context as to when we're in a mailer? This is something we should document.

Comments from Reviewable

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.05%) to 82.353% when pulling 52f0cbb on eacaps:feature/no-request-context into 8ca86ed on shakacode:master.

@justin808

Copy link
Copy Markdown
Member

Looking good!

Please squash your commits and address the following issues.

be sure that rake lint passes, as well as the full test suite rake


Reviewed 3 of 3 files at r4.
Review status: all files reviewed at latest revision, 7 unresolved discussions.


app/helpers/react_on_rails_helper.rb, line 347 [r4] (raw file):

  def rails_context(server_side:)
    @rails_context ||= begin
      result = { inMailer: controller.present? && controller.is_a?(ActionMailer::Base) }

Did this pass the linter? I'd probably rather see this on 3 lines.


app/helpers/react_on_rails_helper.rb, line 365 [r4] (raw file):

          # Locale settings
          i18nLocale: I18n.locale,

can we move the I18n parts to outside the request.present? block?


spec/dummy/app/mailers/dummy_mailer.rb, line 4 [r1] (raw file):

Previously, justin808 (Justin Gordon) wrote…

what is this comment?

OK

spec/dummy/app/views/dummy_mailer/hello_email.html.erb, line 7 [r1] (raw file):

Previously, justin808 (Justin Gordon) wrote…

needs line return -- see the red symbol

OK

spec/dummy/config/initializers/react_on_rails.rb, line 12 [r4] (raw file):

result = {}

Quoted 4 lines of code…

unless view_context.controller.is_a?(ActionMailer::Base)
  result = {
    somethingUseful: view_context.session[:something_useful]
  }
    end result

how about

if view_context.controller.is_a?(ActionMailer::Base)
  {}
else
    {
        somethingUseful: view_context.session[:something_useful]
      }
    end

Comments from Reviewable

@justin808

Copy link
Copy Markdown
Member

@eacaps Any update?

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.04%) to 82.338% when pulling 3c0e6d9 on eacaps:feature/no-request-context into 63436ab on shakacode:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.04%) to 82.338% when pulling 3c0e6d9 on eacaps:feature/no-request-context into 63436ab on shakacode:master.

eacaps added 2 commits August 1, 2016 11:46
allow custom_context even without request

added broken test

added fixes to allow actionmailer test to pass

cleaned up rubocop warnings

resolved some reviewed suggestions

updated with proper handles for inMailer

added inMailer to test

added inMailer checks for test

cleaned up rubocop suggestions

a few final minor tweaks
@justin808

justin808 commented Sep 15, 2016

Copy link
Copy Markdown
Member

@Xeboc Big thanks! My team is slammed with helping our paying clients. We are available for modestly priced coaching packs if anybody is interested: http://www.shakacode.com/work/.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants