Rails : Access view/route helpers from anywhere (Model / Services)

Normally we always need to access route and view helper methods from anywhere in our code base.

View Helpers

In your model, you can do something like the following:

ApplicationController.helpers.your_helper_method

OR

YourController.helpers.your_helper_method

 

In console this will work in Rails ~= 2.3 and >= 3.1.0

app.post_path

# If namespacing is used in routes
app.spree.product_path(Spree::Product.first)
 

Path/Route helpers

class HelperWrapper
  include Rails.application.routes.url_helpers
end
  HelperWrapper.new.admin_path

Add Additional Library to Faker :: RSpec :: TDD

Suppose you are working on a project in which you want to test a Applicant Model. Applicant may have any value among options in a drop-down. If you want the spec should fill the education field with the data you wish, then you can modify the Faker module. To do so, the best way is the following

create a folder in spec/ directory
create a file called ‘anything_you_want.rb’

# inside the file 'anything_you_want.rb'
class Faker::Occupation
  def self.education
    list = ['High School', 'College', 'School']
    list.sample
  end
end

Un-comment the line from the file ‘rails_helper.rb’

Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }