Using erb template and sidekiq - sinatra

I'm using sinatra and sidekiq together. I'm trying to render a erb template from within a sidekiq worker and i'm getting a undefined method 'erb'. In my head, things should work cause sidekiq is loaded up as an instance of my sinatra app, so it should have the erb method. What am i missing here?
class SomeWorker
include Sidekiq::Worker
def perform(id)
erb(:emailTemplate)
end
end
(i now realized the SomeWorker class has nothing to to do with sinatra and so of course it doesn't have the erb method. maybe i can just make a call out to helper module? in place of the erb method call?)

so i figured out 1 solution:
require 'erb'
require 'tilt'
class ForgotEmailWorker
include Sidekiq::Worker
def perform
template = tilt.new(__path_to_erb_file).render
end
end

Related

Can I use PageObject::PageFactory without cucumber?

I am wondering if it's possible to use PageObject::PageFactory without cucumber. I tried it with
World(PageObject::PageFactory)
but there is no world without cucumber. I'm not able to find an example of another way.
Why would I do something that crazy? I want to create a gem that can downloaded by testers to use against a product that we test. I love cukes but I'd also like it to be used for exploratory testing. I've been successful doing this on the rest endpoints.
I know I can do this without PageFactory, but it's so easy that to use PageObject that way that feels like I'm cheating. I want to use these patterns.
visit(NewAccountPage)
on(NewAccountPage).create_account
Maybe there is another way entirely?
Yes, you can. There are 2 things you need to do:
Include the PageObject::PageFactory in the object that you want to use the visit, on, etc methods. (This is what Cucumber's World method is doing.)
Define a #browser variable for the Watir::Browser object.
Here is an example of using the PageFactory in the main:
require 'watir-webdriver'
require 'page-object'
# A page object
class GooglePage
include PageObject
page_url 'http://www.google.com'
end
# Including the page factory methods in the main
include PageObject::PageFactory
# Assign a browser to #browser
#browser = Watir::Browser.new :chrome
# Using the page factory
visit(GooglePage)
p on(GooglePage).current_url
#=> "https://www.google.ca/?gfe_rd=cr&ei=UxfbVdnCAaeD8QeM96CACg&gws_rd=ssl"

Cucumber's AfterConfiguration can't access helper modules

I have a modular Sinatra app without a DB and in order to test memcache, I have some test files that need to be created and deleted on the file system. I would like to generate these files in an AfterConfiguration hook using some helper methods (which are in a module shared with rspec, which also needs to create/delete these files for testing). I only want to create them once at the start of Cucumber.
I do not seem to be able to access the helpers from within AfterConfiguration, which lives in "support/hooks.rb." The helpers are accessible from Cucumber's steps, so I know they have been loaded properly.
This previous post seems to have an answer: Want to load seed data before running cucumber
The second example in this answer seems to say my modules should be accessible to my AfterConfiguration block, but I get "undefined method `foo' for nil:NilClass" when attempting to call helper method "foo".
I can pull everything out into a rakefile and run it that way, but I'd like to know what I'm missing here.
After digging around in the code, it appears that AfterConfiguration not only runs before any features are loaded, but before World is instantiated. Running self.class inside of the AfterConfig block returns NilClass. Running self.class inside of any other hook, such as a Before, will return MyWorldName. In retrospect, this makes sense as every feature is run in a separate instance of World.
This is why helpers defined as instance methods (ie def method_name) are unknown. Changing my methods to module methods (ie def ModuleName.method_name) allows them to function, since they really are module methods anyway.

sinatra helper in external file

I have lot of helpers in my main Sinatra project_name.rb and I want to remove them to the external file, what is the best practice to do that ?
from ./preject_name.rb
helpers do
...#bunch of helpers
end
to for exapmple ./helpers/something.rb
thank you
The simple and recommended way:
module ApplicationHelper
# methods
end
class Main < Sinatra::Base
helpers ApplicationHelper
end
Alas, if, like me, you are building a modular Sinatra app, it's a little more complex than simply moving the helpers out into another file.
The only way I got this to work is as follows.
first up in your app (I'll call this my_modular_app.rb)
require 'sinatra/base'
require 'sinatra/some_helpers'
class MyModularApp < Sinatra::Base
helpers Sinatra::SomeHelpers
...
end
and then create the folder structure ./lib/sinatra/ and create some_helpers.rb as follows:
require 'sinatra/base'
module Sinatra
module SomeHelpers
def help_me_world
logger.debug "hello from a helper"
end
end
helpers SomeHelpers
end
doing this you can simply split all your helpers up into multiple files, affording more clarity in larger projects.
Just as you said it yourself:
Move the helpers block into another file and require it where you need.
#helpers.rb
helpers do
...
end
#project_name.rb
require 'path/to/helpers.rb'
It seems the answer #DaveSag offered miss something. Should add a line at the beginning of my_modular_app.rb:
$:.unshift File.expand_path('../lib', __FILE__) # add ./lib to $LOAD_PATH
require 'sinatra/base'
require 'sinatra/some_helpers' # this line breaks unless line 1 is added.
# more code below...
In addition, if someone prefers a "classical style" like me, the following is for you :)
In app.rb
$:.unshift File.expand_path('../lib', __FILE__)
require 'sinatra'
require 'sinatra/some_helpers'
get '/' do
hello_world
end
In lib/sinatra/some_helpers.rb
module Sinatra
module SomeHelper
def hello_world
"Hello World from Helper!!"
end
end
helpers SomeHelper
end
I just added require_relative './lib/sinatra/helpers' to my config.ru and that's all.
So it looks like this:
require_relative './config/environment'
require_relative './lib/sinatra/helpers'
use ProjectsController
run ApplicationController
and my ./lib/sinatra/helpers.rb file is not even a module and I don't use any requires or includes in it. I can just define methods straight in this file and use them all over the app.
The answer of #kgpdeveloper didn't work for me.

How do I associate a CoffeeScript file with a view?

Just installed rails 3.1 rc1 and am trying to grok the best way to manage javascript with the new asset pipeline
By default all coffeescript is compiled into a single application.js file, this is a good thing.
Each seperate coffee script file is appended to the js file and wrapped in an anonymous function which is executed via the call method
A common scenario would be to use some jquery to turn various forms into ajax forms, update UI, etc...
Many of these scripts will be specific to a controller or action, I am trying to grok the 'conventional' way to handle this,
since everything is wrapped in an anonymous function how do I only execute just
the code for a particular controller / action, by default all of the anonymous functions are being executed
I did play around with some hacks where I load the controller and action name into js variables and then in
coffeescript check those to conditionally run code, I don't like that very much
my initial thought was that each coffee file would contain a js namespace/object and I would call the specific ones from the view,
going to spike this using the default_bare = true configuration
see How can I use option "--bare" in Rails 3.1 for CoffeeScript?
EDIT
Looking around some more: this looks like it might be the correct approach - "Can't find variable" error with Rails 3.1 and Coffeescript
There are two common approaches:
Make behavior conditional on the presence of a particular element. For instance, code to run a signup sheet should be prefaced with something like
if $('#signup').length > 0
Make behavior conditional on a class on the body element. You can set the body class using ERB. This is often desirable for stylesheets as well. The code would be something like
if $('body').hasClass 'user'
gistyle is a simple gem that helps you running action-specific javascript codes.
By following its setup, you set some data attributes in your body element, representing the current controller and action names. Then it will only call that action when the corresponding view is loaded.

Can I make a single Perl module act as multiple kinds of mod_perl handlers?

I'm writing a series of related mod_perl handlers for various login-related functions in Apache, so my Apache config file looks like this (for example)
PerlAccessHandler MyApache::MyAccess
PerlAuthenHandler MyApache::MyAuthen
PerlAuthzHandler MyApache::MyAuthz
Each of the modules (MyAccess, MyAuthen, MyAuthz) defines a
sub handler() {}
Which mod_perl calls at the relevant point in the processing of the request.
What I'd like to know is whether there is a way of doing this with one Perl module rather than three (it's just tidier and less work for users to install one module instead of 3)?
Is there some way to define the name of the handler method, perhaps. Or is there a way of detecting from within the handler() code which sort of handling I'm supposed to be doing?
It appears from the mod_perl 2.0 docs that you can use the "method" syntax to do what you're wanting (I've not tested this):
PerlAccessHandler MyApache::MyLoginModule->access_handler
PerlAuthenHandler MyApache::MyLoginModule->authen_handler
PerlAuthzHandler MyApache::MyLoginModule->authz_handler
I believe this will cause mod_perl to call each of the named methods in a static way on your MyApache::MyLoginModule class.
You can also create an object to be used when calling a handler method if you want to:
<Perl>
use MyApache::MyLoginModule;
$MyApache::MyLoginModule::access = MyApache::MyLoginModule->new(phase => 'access');
$MyApache::MyLoginModule::authen = MyApache::MyLoginModule->new(phase => 'authen');
$MyApache::MyLoginModule::authz = MyApache::MyLoginModule->new(phase => 'authz');
</Perl>
PerlAccessHandler $MyApache::MyLoginModule::access->handler
PerlAuthenHandler $MyApache::MyLoginModule::authen->handler
PerlAuthzHandler $MyApache::MyLoginModule::authz->handler
This approach would allow you to have a single handler method that could have different behavior based on the properties of the object set up upon object creation.
Disclaimer: It's been a while since I've worked with this part of mod_perl configuration so your results may vary!
Looks like one possibility might be using the push_handlers() call and setting up the handlers in code rather than in the apache conf file
See here: http://tinyurl.com/bwdeew