Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make this gem even better by adding docs for integrating with rails #7

Closed
pierre-pretorius opened this issue Apr 25, 2016 · 7 comments

Comments

@pierre-pretorius
Copy link

This looks like the best facebook messenger bot api for ruby. To make it even better I suggest you include examples of how to use it within Rails. Perhaps how to mount the rack application within your rails app and where to place your ruby files that handle bot events (even though they can place it anywhere, they want to know what the standard most elegant place is). A simple controller as used in this gem seems nice: https://github.com/jun85664396/messenger-bot-rails

@jgorset
Copy link
Owner

jgorset commented Apr 26, 2016

Hey, thanks! That's a great idea. So I did it!

@pierre-pretorius
Copy link
Author

pierre-pretorius commented Apr 26, 2016

Excellent thank you! Two notes:

  1. The recommended bot file should probably be in a sub-directory of app such as app/bot/example.rb instead of app/bot.rb.
  2. In production mode the app/bot.rb file will be loaded due to eager loading, but in development mode it won't be loaded. Place a raise 'test' in the top of bot.rb and start the rails app. You will see it doesn't trigger when running rails s but it does with rails s -e production. Solving common problems like this is quite important for larger gem adoption. One (perhaps too elaborate) option is to place code similar to what is shown below in a Railtie. It doesn't explicitly require the files, it rather just references the constants and let ActiveSupport autoload it.
if Rails.env.development?
  Rails.application.config.to_prepare do
    Dir[File.join(Rails.root, 'app', 'bots', '*.rb')].each do |f|
        bot_name = File.basename(f, File.extname(f)).camelize
        bot_name.constantize unless Object.const_defined?(bot_name)
      end
    end
  end
end

@jgorset
Copy link
Owner

jgorset commented Apr 26, 2016

The recommended bot file should probably be in a sub-directory of app such as app/bot/example.rb instead of app/bot.rb.

Right – I was thinking app/bot.rb for the core, and then files app/bot/*.rb for the different concerns of the bot. Using app/bot/example.rb is a better way to suggest that. Fixed in 3dd1f00.

In production mode the app/bot.rb file will be loaded due to eager loading, but in development mode it won't be loaded. Place a raise 'test' in the top of bot.rb and start the rails app. You will see it doesn't trigger when running rails s but it does with rails s -e production. Solving common problems like this is quite important for larger gem adoption. One (perhaps too elaborate) option is to place code similar to what is shown below in a Railtie. It doesn't explicitly require the files, it rather just references the constants and let ActiveSupport autoload it.

Woa, yeah! You're right, of course. That would be really difficult and annoying to debug for anyone who isn't familiar with how eager loading in Rails (like me, apparently). That initializer is pretty elaborate, but unless there's an easier way to tell Rails to eager load only a given path in development I think it's probably the best we can do, right?

@pierre-pretorius
Copy link
Author

I've spent quite some time looking at how ActiveSupport works writing this gem. To date I haven't found a better way to do eager loading in development mode. Sometimes there are sneaky more elegant ways of doing it like what we did here.

I'll make a pull request that loads a Railtie only if Rails is present and not in production mode. It's just going to take a while as I can only start in a week or so.

@jgorset
Copy link
Owner

jgorset commented Apr 26, 2016

Cool, thanks! I might even beat you to it, then.

@jgorset jgorset reopened this Apr 29, 2016
@jgorset
Copy link
Owner

jgorset commented Apr 30, 2016

ee237c7 should do the trick, right?

@jgorset jgorset closed this as completed Apr 30, 2016
@JonasHelmoe
Copy link

Are there some guide on how to do this with Rails?
I follow the readme but as I'm no Rails expert yet, I have no idea where to put all the code...
I keep getting endless errors.

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants