Skip to content

Instantly share code, notes, and snippets.

@sibinx7
Last active March 13, 2017 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sibinx7/782b4cd83ac86f0d30722ca16c2d211d to your computer and use it in GitHub Desktop.
Save sibinx7/782b4cd83ac86f0d30722ca16c2d211d to your computer and use it in GitHub Desktop.
My Rails help file

RAILS_ENV=<env_state> rails c

ENV or > ENV["key"]

Rails Controller without assets, helpers and views

rails generate controller home index  --no-helper --no-assets --no-controller-specs --no-view-specs --no-test-framework --skip-test-unit --skip-template-engine

Print variable inside string

  • single quotes
'Hello %{name}' %{name:"World"}

NOTE: If we have something like this

'Hello %{name} got 100% hike`

This will create error so we need to write

'Hello %{name} got 100%%' %{name:"John"}
  • double quotes
name="World"
"Hello #{name}" 

Simple Rails task testing using RSpec

  1. Task

@path lib/tasks/upwork.rake

# @author Sibin  Xavier
# @use Upwork Data Parsing
# @description RAKE TASK TO PARSE UPWORK
# RSS FIELD AND SAVE TO DATABASE TABLE
#

namespace :admin do
  desc 'Get upwork url'
  task upwork_url: :environment do
    UPWORK_URL= Rails.application.secrets.upwork_url
  end

  desc 'Parse Upwork RSS feed and save on database'
  task upwork_rss_parse: [:upwork_url]  do
    begin
      puts "UPWORK URL #{UPWORK_URL}"

    rescue => e
      puts e.inspect
    end
  end
end
  1. Testing @path : spec/lib/tasks/upwork_spec.rb
require 'spec_helper'
require 'rails_helper'
require 'rake'


describe "upwork rake task" do
  before  do
    # load File.expand_path(Rails.root.join("lib","tasks","upwork.rake"))
    MyApp::Application.load_tasks #look at config/application.rb and use module name
    Rake::Task.define_task(:environment)
  end
  it "should find secrets url" do
    Rake::Task["admin:upwork_url"].invoke
    expect(UPWORK_URL).to eq(Rails.application.secrets.upwork_url)
  end
end

Create good API Doc

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